1
Fork 0

Convert all tests into input/output files

This commit is contained in:
Jan-Erik Rediger 2021-06-23 12:30:55 +02:00
parent ddbe8125cd
commit 1b3dceae42
27 changed files with 516 additions and 525 deletions

16
tests/adds_toc.in.md Normal file
View file

@ -0,0 +1,16 @@
# Chapter
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

20
tests/adds_toc.out.md Normal file
View file

@ -0,0 +1,20 @@
# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

17
tests/github_marker.in.md Normal file
View file

@ -0,0 +1,17 @@
# Chapter
* auto-gen TOC:
{:toc}
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -0,0 +1,21 @@
# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

16
tests/gitlab_marker.in.md Normal file
View file

@ -0,0 +1,16 @@
# Chapter
[[_TOC_]]
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -0,0 +1,21 @@
# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -0,0 +1,17 @@
# Chapter
<!-- toc -->
# Header 1
## Header 1.1
### Header 1.1.1
#### Header 1.1.1.1
##### Header 1.1.1.1.1
# Another header `with inline` code

View file

@ -0,0 +1,19 @@
# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 1.1.1](#header-111)
* [Header 1.1.1.1](#header-1111)
* [Another header `with inline` code](#another-header-with-inline-code)
# Header 1
## Header 1.1
### Header 1.1.1
#### Header 1.1.1.1
##### Header 1.1.1.1.1
# Another header `with inline` code

View file

@ -0,0 +1,16 @@
# Chapter
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -0,0 +1,21 @@
# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -1,7 +1,6 @@
use mdbook_toc::{Toc, Config};
use pretty_assertions::assert_eq;
use mdbook::book::Chapter; use mdbook::book::Chapter;
use mdbook::errors::Result; use mdbook_toc::{Config, Toc};
use pretty_assertions::assert_eq;
fn default<T: Default>() -> T { fn default<T: Default>() -> T {
Default::default() Default::default()
@ -20,544 +19,129 @@ fn with_max_level(level: u32) -> Config {
} }
trait FromContent { trait FromContent {
fn from_content(content: &str) -> Self; fn from_content(content: String) -> Self;
} }
impl FromContent for Chapter { impl FromContent for Chapter {
fn from_content(content: &str) -> Self { fn from_content(content: String) -> Self {
Self { Self {
name: "chapter".into(), name: "chapter".into(),
content: content.into(), content,
number: None, number: None,
sub_items: vec![], sub_items: vec![],
path: None, path: None,
source_path: None, source_path: None,
parent_names: vec![] parent_names: vec![],
} }
} }
} }
fn add_toc(content: &str, config: &Config) -> Result<String> { /// Assert the Table of Content generation for an input file against the expected output file.
let chapter = Chapter::from_content(content); ///
Toc::add_toc(&chapter, config) /// Reads `tests/$name.in.md` and checks the generated ToC code against `tests/$name.out.md`.
macro_rules! assert_toc {
($name:expr) => {
assert_toc!($name, default())
};
($name:expr, $config:expr) => {
let _ = env_logger::builder().is_test(true).try_init();
let config = $config;
let content = ::std::fs::read_to_string(format!("tests/{}.in.md", $name)).expect(concat!(
"Can't read ",
$name,
".in.md"
));
let expected = ::std::fs::read_to_string(format!("tests/{}.out.md", $name))
.expect(concat!("Can't read ", $name, ".out.md"));
let chapter = Chapter::from_content(content);
let result = Toc::add_toc(&chapter, &config);
match result {
Ok(result) => assert_eq!(expected.trim_end(), result),
Err(e) => panic!("{} failed. Error: {}", $name, e),
}
};
} }
#[test] #[test]
fn adds_toc() { fn adds_toc() {
let content = r#"# Chapter assert_toc!("adds_toc", default());
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test]
#[test] fn adds_toc_with_inline_code() {
fn adds_toc_with_inline_code() { assert_toc!("with_inline_code", default());
let content = r#"# Chapter }
<!-- toc --> #[test]
fn leaves_tables_untouched() {
# Header 1 // Regression test.
// Previously we forgot to enable the same markdwon extensions as mdbook itself.
## `Header 1.1` // Markdown roundtripping removes some insignificant whitespace
assert_toc!("tables_untouched");
# Header 2 }
## Header 2.1 #[test]
"#; fn handles_inline_code() {
// Regression test.
let expected = r#"# Chapter // Inline code in a header was broken up into multiple items.
// Also test for deeply nested headers.
* [Header 1](#header-1) assert_toc!("handles_inline_code");
* [`Header 1.1`](#header-11) }
* [Header 2](#header-2)
* [Header 2.1](#header-21) #[test]
fn multi_header_regression() {
# Header 1 assert_toc!("multi_header");
}
## `Header 1.1`
#[test]
# Header 2 fn multi_header_linear_regression_3() {
assert_toc!("multi_header_linear");
## Header 2.1"#; }
assert_eq!(expected, add_toc(content, &default()).unwrap()); #[test]
} fn add_toc_with_gitlab_marker() {
let marker = "[[_TOC_]]".to_owned();
#[test]
fn leaves_tables_untouched() { assert_toc!("gitlab_marker", with_marker(marker));
// Regression test. }
// Previously we forgot to enable the same markdwon extensions as mdbook itself.
#[test]
let content = r#"# Heading fn unique_slugs() {
assert_toc!("unique_slugs");
| Head 1 | Head 2 | }
|--------|--------|
| Row 1 | Row 2 | #[test]
"#; fn add_toc_with_github_marker() {
let marker = "* auto-gen TOC:\n{:toc}".to_owned();
// Markdown roundtripping removes some insignificant whitespace assert_toc!("github_marker", with_marker(marker));
let expected = r#"# Heading }
|Head 1|Head 2| #[test]
|------|------| fn lower_max_level() {
|Row 1|Row 2|"#; assert_toc!("lower_max_level", with_max_level(2));
}
assert_eq!(expected, add_toc(content, &default()).unwrap());
} #[test]
fn higher_max_level() {
#[test] assert_toc!("higher_max_level", with_max_level(7));
fn handles_inline_code() { }
let _ = env_logger::builder().is_test(true).try_init();
// Regression test for [#13](https://github.com/badboy/mdbook-toc/issues/13).
// Regression test. // Choosing a non-HTML TOC marker breaks sites that don't use it at all,
// Inline code in a header was broken up into multiple items. // removed the header and first paragraph.
// Also test for deeply nested headers. #[test]
fn nonhtml_marker_no_toc_in_page() {
let content = r#"# Chapter let marker = "[[_TOC_]]".to_owned();
assert_toc!("nonhtml_marker_no_use", with_marker(marker));
<!-- toc --> }
# Header 1 #[test]
fn similar_heading_different_casing() {
## Header 1.1 // Regression test #15
// Previously we didn't use the normalized header ("slug") to decide whether to use
### Header 1.1.1 // different link anchors.
#### Header 1.1.1.1 assert_toc!("similar_heading_different_casing");
}
##### Header 1.1.1.1.1
# Another header `with inline` code
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 1.1.1](#header-111)
* [Header 1.1.1.1](#header-1111)
* [Another header `with inline` code](#another-header-with-inline-code)
# Header 1
## Header 1.1
### Header 1.1.1
#### Header 1.1.1.1
##### Header 1.1.1.1.1
# Another header `with inline` code"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
}
#[test]
fn multi_header_regression() {
let content = r#"# Main Summary
<!-- toc -->
# Introduction
### Contents
### Background and Caveats
#### Test
### Accessing the Data
# Adding New Fields
## User Preferences"#;
let expected = r#"# Main Summary
* [Introduction](#introduction)
* [Contents](#contents)
* [Background and Caveats](#background-and-caveats)
* [Test](#test)
* [Accessing the Data](#accessing-the-data)
* [Adding New Fields](#adding-new-fields)
* [User Preferences](#user-preferences)
# Introduction
### Contents
### Background and Caveats
#### Test
### Accessing the Data
# Adding New Fields
## User Preferences"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
}
#[test]
fn multi_header_linear_regression_3() {
let content = r#"# Heading
<!-- toc -->
## Level 1.1
### Level 1.1.1
### Level 1.1.2
## Level 1.2
### Level 1.2.1
text"#;
let expected = r#"# Heading
* [Level 1.1](#level-11)
* [Level 1.1.1](#level-111)
* [Level 1.1.2](#level-112)
* [Level 1.2](#level-12)
* [Level 1.2.1](#level-121)
## Level 1.1
### Level 1.1.1
### Level 1.1.2
## Level 1.2
### Level 1.2.1
text"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
}
#[test]
fn add_toc_with_gitlab_marker() {
let marker = "[[_TOC_]]".to_owned();
let content = r#"# Chapter
[[_TOC_]]
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap());
}
#[test]
fn unique_slugs() {
let content = r#"# Chapter
<!-- toc -->
## Duplicate
### Duplicate
#### Duplicate
##### Duplicate
## Duplicate"#;
let expected = r#"# Chapter
* [Duplicate](#duplicate)
* [Duplicate](#duplicate-1)
* [Duplicate](#duplicate-2)
* [Duplicate](#duplicate-4)
## Duplicate
### Duplicate
#### Duplicate
##### Duplicate
## Duplicate"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
}
#[test]
fn add_toc_with_github_marker() {
let marker = "* auto-gen TOC:\n{:toc}".to_owned();
let content = r#"# Chapter
* auto-gen TOC:
{:toc}
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap());
}
#[test]
fn lower_max_level() {
let content = r#"# Chapter
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_max_level(2)).unwrap());
}
#[test]
fn higher_max_level() {
let content = r#"# Chapter
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_max_level(7)).unwrap());
}
// Regression test for [#13](https://github.com/badboy/mdbook-toc/issues/13).
// Choosing a non-HTML TOC marker breaks sites that don't use it at all,
// removed the header and first paragraph.
#[test]
fn nonhtml_marker_no_toc_in_page() {
let _ = env_logger::builder().is_test(true).try_init();
let marker = "[[_TOC_]]".to_owned();
let content = r#"# Chapter
First paragraph
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
First paragraph
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap());
}
#[test]
fn similar_heading_different_casing() {
// Regression test #15
// Previously we didn't use the normalized header ("slug") to decide whether to use
// different link anchors.
//
let content = r#"# Chapter
<!-- toc -->
# Tag
## tag
"#;
let expected = r#"# Chapter
* [Tag](#tag)
* [tag](#tag-1)
# Tag
## tag"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
}

View file

@ -0,0 +1,16 @@
# Chapter
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -0,0 +1,20 @@
# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

17
tests/multi_header.in.md Normal file
View file

@ -0,0 +1,17 @@
# Main Summary
<!-- toc -->
# Introduction
### Contents
### Background and Caveats
#### Test
### Accessing the Data
# Adding New Fields
## User Preferences

23
tests/multi_header.out.md Normal file
View file

@ -0,0 +1,23 @@
# Main Summary
* [Introduction](#introduction)
* [Contents](#contents)
* [Background and Caveats](#background-and-caveats)
* [Test](#test)
* [Accessing the Data](#accessing-the-data)
* [Adding New Fields](#adding-new-fields)
* [User Preferences](#user-preferences)
# Introduction
### Contents
### Background and Caveats
#### Test
### Accessing the Data
# Adding New Fields
## User Preferences

View file

@ -0,0 +1,11 @@
# Heading
<!-- toc -->
## Level 1.1
### Level 1.1.1
### Level 1.1.2
## Level 1.2
### Level 1.2.1
text

View file

@ -0,0 +1,20 @@
# Heading
* [Level 1.1](#level-11)
* [Level 1.1.1](#level-111)
* [Level 1.1.2](#level-112)
* [Level 1.2](#level-12)
* [Level 1.2.1](#level-121)
## Level 1.1
### Level 1.1.1
### Level 1.1.2
## Level 1.2
### Level 1.2.1
text

View file

@ -0,0 +1,16 @@
# Chapter
First paragraph
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -0,0 +1,16 @@
# Chapter
First paragraph
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1

View file

@ -0,0 +1,8 @@
# Chapter
<!-- toc -->
# Tag
## tag

View file

@ -0,0 +1,9 @@
# Chapter
* [Tag](#tag)
* [tag](#tag-1)
# Tag
## tag

View file

@ -0,0 +1,6 @@
# Heading
| Head 1 | Head 2 |
|--------|--------|
| Row 1 | Row 2 |

View file

@ -0,0 +1,5 @@
# Heading
|Head 1|Head 2|
|------|------|
|Row 1|Row 2|

14
tests/unique_slugs.in.md Normal file
View file

@ -0,0 +1,14 @@
# Chapter
<!-- toc -->
## Duplicate
### Duplicate
#### Duplicate
##### Duplicate
## Duplicate

17
tests/unique_slugs.out.md Normal file
View file

@ -0,0 +1,17 @@
# Chapter
* [Duplicate](#duplicate)
* [Duplicate](#duplicate-1)
* [Duplicate](#duplicate-2)
* [Duplicate](#duplicate-4)
## Duplicate
### Duplicate
#### Duplicate
##### Duplicate
## Duplicate

View file

@ -0,0 +1,11 @@
# Chapter
<!-- toc -->
# Header 1
## `Header 1.1`
# Header 2
## Header 2.1

View file

@ -0,0 +1,14 @@
# Chapter
* [Header 1](#header-1)
* [`Header 1.1`](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
# Header 1
## `Header 1.1`
# Header 2
## Header 2.1