summaryrefslogtreecommitdiffstats
path: root/tests/it.rs
blob: 4ee922c02cbc529af2d3f3f0f29046b4427f5279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use mdbook::book::Chapter;
use mdbook_toc::{Config, Toc};
use pretty_assertions::assert_eq;

fn default<T: Default>() -> T {
    Default::default()
}

fn with_marker<S: Into<String>>(marker: S) -> Config {
    let mut cfg = Config::default();
    cfg.marker = marker.into();
    cfg
}

fn with_max_level(level: u32) -> Config {
    let mut cfg = Config::default();
    cfg.max_level = level;
    cfg
}

trait FromContent {
    fn from_content(content: String) -> Self;
}

impl FromContent for Chapter {
    fn from_content(content: String) -> Self {
        Self {
            name: "chapter".into(),
            content,
            number: None,
            sub_items: vec![],
            path: None,
            source_path: None,
            parent_names: vec![],
        }
    }
}

/// 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);
        let result = Toc::add_toc(&chapter, &config);
        match result {
            Ok(result) => assert_eq!(expected, result),
            Err(e) => panic!("{} failed. Error: {}", $name, e),
        }
    };
}

#[test]
fn adds_toc() {
    assert_toc!("adds_toc", default());
}

#[test]
fn adds_toc_with_inline_code() {
    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.
    // Markdown roundtripping removes some insignificant whitespace
    assert_toc!("tables_untouched");
}

#[test]
fn handles_inline_code() {
    // Regression test.
    // Inline code in a header was broken up into multiple items.
    // Also test for deeply nested headers.
    assert_toc!("handles_inline_code");
}

#[test]
fn multi_header_regression() {
    assert_toc!("multi_header");
}

#[test]
fn multi_header_linear_regression_3() {
    assert_toc!("multi_header_linear");
}

#[test]
fn add_toc_with_gitlab_marker() {
    let marker = "[[_TOC_]]".to_owned();

    assert_toc!("gitlab_marker", with_marker(marker));
}

#[test]
fn unique_slugs() {
    assert_toc!("unique_slugs");
}

#[test]
fn add_toc_with_github_marker() {
    let marker = "* auto-gen TOC:\n{:toc}\n".to_owned();
    assert_toc!("github_marker", with_marker(marker));
}

#[test]
fn lower_max_level() {
    assert_toc!("lower_max_level", with_max_level(2));
}

#[test]
fn higher_max_level() {
    assert_toc!("higher_max_level", with_max_level(7));
}

// 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 marker = "[[_TOC_]]".to_owned();
    assert_toc!("nonhtml_marker_no_use", with_marker(marker));
}

#[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.

    assert_toc!("similar_heading_different_casing");
}

#[test]
fn tables_with_html() {
    assert_toc!("tables_with_html");
}

#[test]
fn backslash_escapes() {
    // Regression test #21
    // Backslash-escaped elements should still be escaped.
    assert_toc!("backslash_escapes");
}

#[test]
fn empty_document() {
    // Regression test #31
    // Empty documents should not fail
    assert_toc!("empty_document");
}

#[test]
fn crlf() {
    assert_toc!("crlf");
}