|
| 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 | +} |
0 commit comments