Skip to content

Commit 74544f0

Browse files
Add toc fold tests
1 parent 2188f30 commit 74544f0

File tree

6 files changed

+410
-0
lines changed

6 files changed

+410
-0
lines changed

tests/testsuite/toc.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use anyhow::Context;
55
use mdbook::errors::*;
66
use select::document::Document;
77
use select::predicate::{Attr, Class, Name, Predicate};
8+
use snapbox::{file, IntoData};
89
use std::fs;
910

1011
const TOC_TOP_LEVEL: &[&str] = &[
@@ -185,3 +186,66 @@ fn summary_with_markdown_formatting() {
185186
"#]],
186187
);
187188
}
189+
190+
/// Helper function to test the table of contents with different fold settings
191+
fn toc_folds_helper(fold_enable: bool, fold_level: usize, expected: impl IntoData) {
192+
BookTest::from_dir("toc/basic_toc")
193+
.change_file(
194+
"book.toml",
195+
format!(
196+
r##"
197+
[output.html.fold]
198+
enable = {}
199+
level = {}
200+
"##,
201+
fold_enable, fold_level
202+
)
203+
.as_str(),
204+
)
205+
.build()
206+
.check_toc_js(expected);
207+
}
208+
209+
#[test]
210+
fn toc_folds_disabled() {
211+
// Folds disabled from level 0
212+
toc_folds_helper(
213+
false,
214+
0,
215+
file!["toc/fold_tests_expected/fold_disabled.html"],
216+
);
217+
// Folds disabled from any level should be the same as level 0
218+
toc_folds_helper(
219+
false,
220+
2,
221+
file!["toc/fold_tests_expected/fold_disabled.html"],
222+
);
223+
}
224+
225+
#[test]
226+
fn toc_folds_enabled() {
227+
// Folds enabled from level 0
228+
toc_folds_helper(
229+
true,
230+
0,
231+
file!["toc/fold_tests_expected/fold_enabled_level_0.html"],
232+
);
233+
// Folds enabled from level 1
234+
toc_folds_helper(
235+
true,
236+
1,
237+
file!["toc/fold_tests_expected/fold_enabled_level_1.html"],
238+
);
239+
// Folds enabled from level 2
240+
toc_folds_helper(
241+
true,
242+
2,
243+
file!["toc/fold_tests_expected/fold_enabled_level_2.html"],
244+
);
245+
// Folds enabled from level 3
246+
toc_folds_helper(
247+
true,
248+
3,
249+
file!["toc/fold_tests_expected/fold_enabled_level_3.html"],
250+
);
251+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<ol class="chapter">
2+
<li class="chapter-item expanded affix ">
3+
<a href="prefix1.html">Prefix 1</a>
4+
</li>
5+
<li class="chapter-item expanded affix ">
6+
<a href="prefix2.html">Prefix 2</a>
7+
</li>
8+
<li class="chapter-item expanded ">
9+
<a href="index.html">
10+
<strong aria-hidden="true">1.</strong> With Readme</a>
11+
</li>
12+
<li>
13+
<ol class="section">
14+
<li class="chapter-item expanded ">
15+
<a href="nested/index.html">
16+
<strong aria-hidden="true">1.1.</strong> Nested Index</a>
17+
</li>
18+
<li class="chapter-item expanded ">
19+
<a href="nested/two.html">
20+
<strong aria-hidden="true">1.2.</strong> Nested two</a>
21+
</li>
22+
</ol>
23+
</li>
24+
<li class="chapter-item expanded ">
25+
<div>
26+
<strong aria-hidden="true">2.</strong> Draft</div>
27+
</li>
28+
<li class="chapter-item expanded affix ">
29+
<li class="spacer">
30+
</li>
31+
<li class="chapter-item expanded affix ">
32+
<li class="part-title">Deep Nest</li>
33+
<li class="chapter-item expanded ">
34+
<a href="deep/index.html">
35+
<strong aria-hidden="true">3.</strong> Deep Nest 1</a>
36+
</li>
37+
<li>
38+
<ol class="section">
39+
<li class="chapter-item expanded ">
40+
<a href="deep/a/index.html">
41+
<strong aria-hidden="true">3.1.</strong> Deep Nest 2</a>
42+
</li>
43+
<li>
44+
<ol class="section">
45+
<li class="chapter-item expanded ">
46+
<a href="deep/a/b/index.html">
47+
<strong aria-hidden="true">3.1.1.</strong> Deep Nest 3</a>
48+
</li>
49+
</ol>
50+
</li>
51+
</ol>
52+
</li>
53+
<li class="chapter-item expanded ">
54+
<li class="spacer">
55+
</li>
56+
<li class="chapter-item expanded affix ">
57+
<a href="suffix1.html">Suffix 1</a>
58+
</li>
59+
<li class="chapter-item expanded affix ">
60+
<a href="suffix2.html">Suffix 2</a>
61+
</li>
62+
</ol>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<ol class="chapter">
2+
<li class="chapter-item affix ">
3+
<a href="prefix1.html">Prefix 1</a>
4+
</li>
5+
<li class="chapter-item affix ">
6+
<a href="prefix2.html">Prefix 2</a>
7+
</li>
8+
<li class="chapter-item ">
9+
<a href="index.html">
10+
<strong aria-hidden="true">1.</strong> With Readme</a>
11+
<a class="toggle">
12+
<div></div>
13+
</a>
14+
</li>
15+
<li>
16+
<ol class="section">
17+
<li class="chapter-item ">
18+
<a href="nested/index.html">
19+
<strong aria-hidden="true">1.1.</strong> Nested Index</a>
20+
</li>
21+
<li class="chapter-item ">
22+
<a href="nested/two.html">
23+
<strong aria-hidden="true">1.2.</strong> Nested two</a>
24+
</li>
25+
</ol>
26+
</li>
27+
<li class="chapter-item ">
28+
<div>
29+
<strong aria-hidden="true">2.</strong> Draft</div>
30+
</li>
31+
<li class="chapter-item affix ">
32+
<li class="spacer">
33+
</li>
34+
<li class="chapter-item affix ">
35+
<li class="part-title">Deep Nest</li>
36+
<li class="chapter-item ">
37+
<a href="deep/index.html">
38+
<strong aria-hidden="true">3.</strong> Deep Nest 1</a>
39+
<a class="toggle">
40+
<div></div>
41+
</a>
42+
</li>
43+
<li>
44+
<ol class="section">
45+
<li class="chapter-item ">
46+
<a href="deep/a/index.html">
47+
<strong aria-hidden="true">3.1.</strong> Deep Nest 2</a>
48+
<a class="toggle">
49+
<div></div>
50+
</a>
51+
</li>
52+
<li>
53+
<ol class="section">
54+
<li class="chapter-item ">
55+
<a href="deep/a/b/index.html">
56+
<strong aria-hidden="true">3.1.1.</strong> Deep Nest 3</a>
57+
</li>
58+
</ol>
59+
</li>
60+
</ol>
61+
</li>
62+
<li class="chapter-item ">
63+
<li class="spacer">
64+
</li>
65+
<li class="chapter-item affix ">
66+
<a href="suffix1.html">Suffix 1</a>
67+
</li>
68+
<li class="chapter-item affix ">
69+
<a href="suffix2.html">Suffix 2</a>
70+
</li>
71+
</ol>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<ol class="chapter">
2+
<li class="chapter-item expanded affix ">
3+
<a href="prefix1.html">Prefix 1</a>
4+
</li>
5+
<li class="chapter-item expanded affix ">
6+
<a href="prefix2.html">Prefix 2</a>
7+
</li>
8+
<li class="chapter-item expanded ">
9+
<a href="index.html">
10+
<strong aria-hidden="true">1.</strong> With Readme</a>
11+
<a class="toggle">
12+
<div></div>
13+
</a>
14+
</li>
15+
<li>
16+
<ol class="section">
17+
<li class="chapter-item ">
18+
<a href="nested/index.html">
19+
<strong aria-hidden="true">1.1.</strong> Nested Index</a>
20+
</li>
21+
<li class="chapter-item ">
22+
<a href="nested/two.html">
23+
<strong aria-hidden="true">1.2.</strong> Nested two</a>
24+
</li>
25+
</ol>
26+
</li>
27+
<li class="chapter-item expanded ">
28+
<div>
29+
<strong aria-hidden="true">2.</strong> Draft</div>
30+
</li>
31+
<li class="chapter-item expanded affix ">
32+
<li class="spacer">
33+
</li>
34+
<li class="chapter-item expanded affix ">
35+
<li class="part-title">Deep Nest</li>
36+
<li class="chapter-item expanded ">
37+
<a href="deep/index.html">
38+
<strong aria-hidden="true">3.</strong> Deep Nest 1</a>
39+
<a class="toggle">
40+
<div></div>
41+
</a>
42+
</li>
43+
<li>
44+
<ol class="section">
45+
<li class="chapter-item ">
46+
<a href="deep/a/index.html">
47+
<strong aria-hidden="true">3.1.</strong> Deep Nest 2</a>
48+
<a class="toggle">
49+
<div></div>
50+
</a>
51+
</li>
52+
<li>
53+
<ol class="section">
54+
<li class="chapter-item ">
55+
<a href="deep/a/b/index.html">
56+
<strong aria-hidden="true">3.1.1.</strong> Deep Nest 3</a>
57+
</li>
58+
</ol>
59+
</li>
60+
</ol>
61+
</li>
62+
<li class="chapter-item expanded ">
63+
<li class="spacer">
64+
</li>
65+
<li class="chapter-item expanded affix ">
66+
<a href="suffix1.html">Suffix 1</a>
67+
</li>
68+
<li class="chapter-item expanded affix ">
69+
<a href="suffix2.html">Suffix 2</a>
70+
</li>
71+
</ol>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<ol class="chapter">
2+
<li class="chapter-item expanded affix ">
3+
<a href="prefix1.html">Prefix 1</a>
4+
</li>
5+
<li class="chapter-item expanded affix ">
6+
<a href="prefix2.html">Prefix 2</a>
7+
</li>
8+
<li class="chapter-item expanded ">
9+
<a href="index.html">
10+
<strong aria-hidden="true">1.</strong> With Readme</a>
11+
<a class="toggle">
12+
<div></div>
13+
</a>
14+
</li>
15+
<li>
16+
<ol class="section">
17+
<li class="chapter-item expanded ">
18+
<a href="nested/index.html">
19+
<strong aria-hidden="true">1.1.</strong> Nested Index</a>
20+
</li>
21+
<li class="chapter-item expanded ">
22+
<a href="nested/two.html">
23+
<strong aria-hidden="true">1.2.</strong> Nested two</a>
24+
</li>
25+
</ol>
26+
</li>
27+
<li class="chapter-item expanded ">
28+
<div>
29+
<strong aria-hidden="true">2.</strong> Draft</div>
30+
</li>
31+
<li class="chapter-item expanded affix ">
32+
<li class="spacer">
33+
</li>
34+
<li class="chapter-item expanded affix ">
35+
<li class="part-title">Deep Nest</li>
36+
<li class="chapter-item expanded ">
37+
<a href="deep/index.html">
38+
<strong aria-hidden="true">3.</strong> Deep Nest 1</a>
39+
<a class="toggle">
40+
<div></div>
41+
</a>
42+
</li>
43+
<li>
44+
<ol class="section">
45+
<li class="chapter-item expanded ">
46+
<a href="deep/a/index.html">
47+
<strong aria-hidden="true">3.1.</strong> Deep Nest 2</a>
48+
<a class="toggle">
49+
<div></div>
50+
</a>
51+
</li>
52+
<li>
53+
<ol class="section">
54+
<li class="chapter-item ">
55+
<a href="deep/a/b/index.html">
56+
<strong aria-hidden="true">3.1.1.</strong> Deep Nest 3</a>
57+
</li>
58+
</ol>
59+
</li>
60+
</ol>
61+
</li>
62+
<li class="chapter-item expanded ">
63+
<li class="spacer">
64+
</li>
65+
<li class="chapter-item expanded affix ">
66+
<a href="suffix1.html">Suffix 1</a>
67+
</li>
68+
<li class="chapter-item expanded affix ">
69+
<a href="suffix2.html">Suffix 2</a>
70+
</li>
71+
</ol>

0 commit comments

Comments
 (0)