Migrate check_second_toc_level to BookTest

This commit is contained in:
Eric Huss 2025-04-22 11:17:40 -07:00
parent 707319e004
commit 14d412b279
15 changed files with 91 additions and 25 deletions

View file

@ -5,7 +5,6 @@ use crate::dummy_book::{assert_contains_strings, DummyBook};
use anyhow::Context;
use mdbook::config::Config;
use mdbook::errors::*;
use mdbook::utils::fs::write_file;
use mdbook::MDBook;
use pretty_assertions::assert_eq;
use select::document::Document;
@ -13,8 +12,6 @@ use select::predicate::{Attr, Class, Name, Predicate};
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::str::FromStr;
use tempfile::Builder as TempFileBuilder;
use walkdir::{DirEntry, WalkDir};
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()))
}
#[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]
fn check_first_toc_level() {
let doc = toc_js_html().unwrap();

View file

@ -19,6 +19,7 @@ mod rendering;
mod search;
mod test;
mod theme;
mod toc;
mod prelude {
pub use crate::book_test::BookTest;

55
tests/testsuite/toc.rs Normal file
View 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);
}

View file

@ -0,0 +1,2 @@
[book]
title = "basic_toc"

View file

@ -0,0 +1 @@
# With Readme

View 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)

View file

@ -0,0 +1 @@
# Deep Nest 3

View file

@ -0,0 +1 @@
# Deep Nest 2

View file

@ -0,0 +1 @@
# Deep Nest 1

View file

@ -0,0 +1 @@
# Nested Index

View file

@ -0,0 +1 @@
# Nested two

View file

@ -0,0 +1 @@
# Prefix 1

View file

@ -0,0 +1 @@
# Prefix 2

View file

@ -0,0 +1 @@
# Suffix 1

View file

@ -0,0 +1 @@
# Suffix 2