Migrate check_second_toc_level to BookTest
This commit is contained in:
parent
707319e004
commit
14d412b279
15 changed files with 91 additions and 25 deletions
|
|
@ -5,7 +5,6 @@ use crate::dummy_book::{assert_contains_strings, DummyBook};
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use mdbook::config::Config;
|
use mdbook::config::Config;
|
||||||
use mdbook::errors::*;
|
use mdbook::errors::*;
|
||||||
use mdbook::utils::fs::write_file;
|
|
||||||
use mdbook::MDBook;
|
use mdbook::MDBook;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use select::document::Document;
|
use select::document::Document;
|
||||||
|
|
@ -13,8 +12,6 @@ use select::predicate::{Attr, Class, Name, Predicate};
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
|
||||||
use tempfile::Builder as TempFileBuilder;
|
|
||||||
use walkdir::{DirEntry, WalkDir};
|
use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book");
|
const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book");
|
||||||
|
|
@ -172,28 +169,6 @@ fn toc_fallback_html() -> Result<Document> {
|
||||||
Ok(Document::from(html.as_str()))
|
Ok(Document::from(html.as_str()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_second_toc_level() {
|
|
||||||
let doc = toc_js_html().unwrap();
|
|
||||||
let mut should_be = Vec::from(TOC_SECOND_LEVEL);
|
|
||||||
should_be.sort_unstable();
|
|
||||||
|
|
||||||
let pred = descendants!(
|
|
||||||
Class("chapter"),
|
|
||||||
Name("li"),
|
|
||||||
Name("li"),
|
|
||||||
Name("a").and(Class("toggle").not())
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut children_of_children: Vec<_> = doc
|
|
||||||
.find(pred)
|
|
||||||
.map(|elem| elem.text().trim().to_string())
|
|
||||||
.collect();
|
|
||||||
children_of_children.sort();
|
|
||||||
|
|
||||||
assert_eq!(children_of_children, should_be);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_first_toc_level() {
|
fn check_first_toc_level() {
|
||||||
let doc = toc_js_html().unwrap();
|
let doc = toc_js_html().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ mod rendering;
|
||||||
mod search;
|
mod search;
|
||||||
mod test;
|
mod test;
|
||||||
mod theme;
|
mod theme;
|
||||||
|
mod toc;
|
||||||
|
|
||||||
mod prelude {
|
mod prelude {
|
||||||
pub use crate::book_test::BookTest;
|
pub use crate::book_test::BookTest;
|
||||||
|
|
|
||||||
55
tests/testsuite/toc.rs
Normal file
55
tests/testsuite/toc.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
//! Tests for table of contents (sidebar).
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
use select::document::Document;
|
||||||
|
use select::predicate::{Class, Name, Predicate};
|
||||||
|
|
||||||
|
const TOC_SECOND_LEVEL: &[&str] = &[
|
||||||
|
"1.1. Nested Index",
|
||||||
|
"1.2. Nested two",
|
||||||
|
"3.1. Deep Nest 2",
|
||||||
|
"3.1.1. Deep Nest 3",
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Apply a series of predicates to some root predicate, where each
|
||||||
|
/// successive predicate is the descendant of the last one. Similar to how you
|
||||||
|
/// might do `ul.foo li a` in CSS to access all anchor tags in the `foo` list.
|
||||||
|
macro_rules! descendants {
|
||||||
|
($root:expr, $($child:expr),*) => {
|
||||||
|
$root
|
||||||
|
$(
|
||||||
|
.descendant($child)
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read the TOC (`book/toc.js`) nested HTML and expose it as a DOM which we
|
||||||
|
/// can search with the `select` crate
|
||||||
|
fn toc_js_html() -> Document {
|
||||||
|
let mut test = BookTest::from_dir("toc/basic_toc");
|
||||||
|
test.build();
|
||||||
|
let html = test.toc_js_html();
|
||||||
|
Document::from(html.as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_second_toc_level() {
|
||||||
|
let doc = toc_js_html();
|
||||||
|
let mut should_be = Vec::from(TOC_SECOND_LEVEL);
|
||||||
|
should_be.sort_unstable();
|
||||||
|
|
||||||
|
let pred = descendants!(
|
||||||
|
Class("chapter"),
|
||||||
|
Name("li"),
|
||||||
|
Name("li"),
|
||||||
|
Name("a").and(Class("toggle").not())
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut children_of_children: Vec<_> = doc
|
||||||
|
.find(pred)
|
||||||
|
.map(|elem| elem.text().trim().to_string())
|
||||||
|
.collect();
|
||||||
|
children_of_children.sort();
|
||||||
|
|
||||||
|
assert_eq!(children_of_children, should_be);
|
||||||
|
}
|
||||||
2
tests/testsuite/toc/basic_toc/book.toml
Normal file
2
tests/testsuite/toc/basic_toc/book.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
[book]
|
||||||
|
title = "basic_toc"
|
||||||
1
tests/testsuite/toc/basic_toc/src/README.md
Normal file
1
tests/testsuite/toc/basic_toc/src/README.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# With Readme
|
||||||
23
tests/testsuite/toc/basic_toc/src/SUMMARY.md
Normal file
23
tests/testsuite/toc/basic_toc/src/SUMMARY.md
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Summary
|
||||||
|
|
||||||
|
[Prefix 1](prefix1.md)
|
||||||
|
[Prefix 2](prefix2.md)
|
||||||
|
|
||||||
|
- [With Readme](README.md)
|
||||||
|
- [Nested Index](nested/index.md)
|
||||||
|
- [Nested two](nested/two.md)
|
||||||
|
- [Draft]()
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Deep Nest
|
||||||
|
|
||||||
|
- [Deep Nest 1](deep/index.md)
|
||||||
|
- [Deep Nest 2](deep/a/index.md)
|
||||||
|
- [Deep Nest 3](deep/a/b/index.md)
|
||||||
|
[Deep Nest 4](deep/a/b/c/index.md)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
[Suffix 1](suffix1.md)
|
||||||
|
[Suffix 2](suffix2.md)
|
||||||
1
tests/testsuite/toc/basic_toc/src/deep/a/b/index.md
Normal file
1
tests/testsuite/toc/basic_toc/src/deep/a/b/index.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Deep Nest 3
|
||||||
1
tests/testsuite/toc/basic_toc/src/deep/a/index.md
Normal file
1
tests/testsuite/toc/basic_toc/src/deep/a/index.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Deep Nest 2
|
||||||
1
tests/testsuite/toc/basic_toc/src/deep/index.md
Normal file
1
tests/testsuite/toc/basic_toc/src/deep/index.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Deep Nest 1
|
||||||
1
tests/testsuite/toc/basic_toc/src/nested/index.md
Normal file
1
tests/testsuite/toc/basic_toc/src/nested/index.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Nested Index
|
||||||
1
tests/testsuite/toc/basic_toc/src/nested/two.md
Normal file
1
tests/testsuite/toc/basic_toc/src/nested/two.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Nested two
|
||||||
1
tests/testsuite/toc/basic_toc/src/prefix1.md
Normal file
1
tests/testsuite/toc/basic_toc/src/prefix1.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Prefix 1
|
||||||
1
tests/testsuite/toc/basic_toc/src/prefix2.md
Normal file
1
tests/testsuite/toc/basic_toc/src/prefix2.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Prefix 2
|
||||||
1
tests/testsuite/toc/basic_toc/src/suffix1.md
Normal file
1
tests/testsuite/toc/basic_toc/src/suffix1.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Suffix 1
|
||||||
1
tests/testsuite/toc/basic_toc/src/suffix2.md
Normal file
1
tests/testsuite/toc/basic_toc/src/suffix2.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Suffix 2
|
||||||
Loading…
Add table
Reference in a new issue