Skip to content

Commit 14d412b

Browse files
committed
Migrate check_second_toc_level to BookTest
1 parent 707319e commit 14d412b

File tree

15 files changed

+91
-25
lines changed

15 files changed

+91
-25
lines changed

tests/rendered_output.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ use crate::dummy_book::{assert_contains_strings, DummyBook};
55
use anyhow::Context;
66
use mdbook::config::Config;
77
use mdbook::errors::*;
8-
use mdbook::utils::fs::write_file;
98
use mdbook::MDBook;
109
use pretty_assertions::assert_eq;
1110
use select::document::Document;
1211
use select::predicate::{Attr, Class, Name, Predicate};
1312
use std::ffi::OsStr;
1413
use std::fs;
1514
use std::path::Path;
16-
use std::str::FromStr;
17-
use tempfile::Builder as TempFileBuilder;
1815
use walkdir::{DirEntry, WalkDir};
1916

2017
const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book");
@@ -172,28 +169,6 @@ fn toc_fallback_html() -> Result<Document> {
172169
Ok(Document::from(html.as_str()))
173170
}
174171

175-
#[test]
176-
fn check_second_toc_level() {
177-
let doc = toc_js_html().unwrap();
178-
let mut should_be = Vec::from(TOC_SECOND_LEVEL);
179-
should_be.sort_unstable();
180-
181-
let pred = descendants!(
182-
Class("chapter"),
183-
Name("li"),
184-
Name("li"),
185-
Name("a").and(Class("toggle").not())
186-
);
187-
188-
let mut children_of_children: Vec<_> = doc
189-
.find(pred)
190-
.map(|elem| elem.text().trim().to_string())
191-
.collect();
192-
children_of_children.sort();
193-
194-
assert_eq!(children_of_children, should_be);
195-
}
196-
197172
#[test]
198173
fn check_first_toc_level() {
199174
let doc = toc_js_html().unwrap();

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod rendering;
1919
mod search;
2020
mod test;
2121
mod theme;
22+
mod toc;
2223

2324
mod prelude {
2425
pub use crate::book_test::BookTest;

tests/testsuite/toc.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Tests for table of contents (sidebar).
2+
3+
use crate::prelude::*;
4+
use select::document::Document;
5+
use select::predicate::{Class, Name, Predicate};
6+
7+
const TOC_SECOND_LEVEL: &[&str] = &[
8+
"1.1. Nested Index",
9+
"1.2. Nested two",
10+
"3.1. Deep Nest 2",
11+
"3.1.1. Deep Nest 3",
12+
];
13+
14+
/// Apply a series of predicates to some root predicate, where each
15+
/// successive predicate is the descendant of the last one. Similar to how you
16+
/// might do `ul.foo li a` in CSS to access all anchor tags in the `foo` list.
17+
macro_rules! descendants {
18+
($root:expr, $($child:expr),*) => {
19+
$root
20+
$(
21+
.descendant($child)
22+
)*
23+
};
24+
}
25+
26+
/// Read the TOC (`book/toc.js`) nested HTML and expose it as a DOM which we
27+
/// can search with the `select` crate
28+
fn toc_js_html() -> Document {
29+
let mut test = BookTest::from_dir("toc/basic_toc");
30+
test.build();
31+
let html = test.toc_js_html();
32+
Document::from(html.as_str())
33+
}
34+
35+
#[test]
36+
fn check_second_toc_level() {
37+
let doc = toc_js_html();
38+
let mut should_be = Vec::from(TOC_SECOND_LEVEL);
39+
should_be.sort_unstable();
40+
41+
let pred = descendants!(
42+
Class("chapter"),
43+
Name("li"),
44+
Name("li"),
45+
Name("a").and(Class("toggle").not())
46+
);
47+
48+
let mut children_of_children: Vec<_> = doc
49+
.find(pred)
50+
.map(|elem| elem.text().trim().to_string())
51+
.collect();
52+
children_of_children.sort();
53+
54+
assert_eq!(children_of_children, should_be);
55+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[book]
2+
title = "basic_toc"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# With Readme
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Summary
2+
3+
[Prefix 1](prefix1.md)
4+
[Prefix 2](prefix2.md)
5+
6+
- [With Readme](README.md)
7+
- [Nested Index](nested/index.md)
8+
- [Nested two](nested/two.md)
9+
- [Draft]()
10+
11+
---
12+
13+
# Deep Nest
14+
15+
- [Deep Nest 1](deep/index.md)
16+
- [Deep Nest 2](deep/a/index.md)
17+
- [Deep Nest 3](deep/a/b/index.md)
18+
[Deep Nest 4](deep/a/b/c/index.md)
19+
20+
---
21+
22+
[Suffix 1](suffix1.md)
23+
[Suffix 2](suffix2.md)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Deep Nest 3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Deep Nest 2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Deep Nest 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Nested Index

0 commit comments

Comments
 (0)