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::errors::Result;
use mdbook_toc::{Config, Toc};
use pretty_assertions::assert_eq;
fn default<T: Default>() -> T {
Default::default()
@ -20,473 +19,113 @@ fn with_max_level(level: u32) -> Config {
}
trait FromContent {
fn from_content(content: &str) -> Self;
fn from_content(content: String) -> Self;
}
impl FromContent for Chapter {
fn from_content(content: &str) -> Self {
fn from_content(content: String) -> Self {
Self {
name: "chapter".into(),
content: content.into(),
content,
number: None,
sub_items: vec![],
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.
///
/// 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);
Toc::add_toc(&chapter, config)
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]
fn adds_toc() {
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, &default()).unwrap());
assert_toc!("adds_toc", default());
}
#[test]
fn adds_toc_with_inline_code() {
let content = r#"# Chapter
<!-- toc -->
# Header 1
## `Header 1.1`
# Header 2
## Header 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 1
## `Header 1.1`
# Header 2
## Header 2.1"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
assert_toc!("with_inline_code", default());
}
#[test]
fn leaves_tables_untouched() {
// Regression test.
// Previously we forgot to enable the same markdwon extensions as mdbook itself.
let content = r#"# Heading
| Head 1 | Head 2 |
|--------|--------|
| Row 1 | Row 2 |
"#;
// Markdown roundtripping removes some insignificant whitespace
let expected = r#"# Heading
|Head 1|Head 2|
|------|------|
|Row 1|Row 2|"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
assert_toc!("tables_untouched");
}
#[test]
fn handles_inline_code() {
let _ = env_logger::builder().is_test(true).try_init();
// Regression test.
// Inline code in a header was broken up into multiple items.
// Also test for deeply nested headers.
let content = r#"# 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
"#;
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());
assert_toc!("handles_inline_code");
}
#[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());
assert_toc!("multi_header");
}
#[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());
assert_toc!("multi_header_linear");
}
#[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());
assert_toc!("gitlab_marker", with_marker(marker));
}
#[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());
assert_toc!("unique_slugs");
}
#[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());
assert_toc!("github_marker", with_marker(marker));
}
#[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());
assert_toc!("lower_max_level", with_max_level(2));
}
#[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());
assert_toc!("higher_max_level", with_max_level(7));
}
// Regression test for [#13](https://github.com/badboy/mdbook-toc/issues/13).
@ -494,44 +133,8 @@ text"#;
// 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());
assert_toc!("nonhtml_marker_no_use", with_marker(marker));
}
#[test]
@ -539,25 +142,6 @@ First paragraph
// 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());
assert_toc!("similar_heading_different_casing");
}

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