Skip to content

Commit 6997208

Browse files
committed
Migrate check_link_target_fallback to BookTest
1 parent 5f2453e commit 6997208

File tree

2 files changed

+32
-55
lines changed

2 files changed

+32
-55
lines changed

tests/rendered_output.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,15 @@ mod dummy_book;
22

33
use crate::dummy_book::{assert_contains_strings, DummyBook};
44

5-
use anyhow::Context;
65
use mdbook::config::Config;
7-
use mdbook::errors::*;
86
use mdbook::MDBook;
97
use pretty_assertions::assert_eq;
10-
use select::document::Document;
11-
use select::predicate::{Attr, Class, Name, Predicate};
128
use std::ffi::OsStr;
139
use std::fs;
1410
use std::path::Path;
1511
use walkdir::{DirEntry, WalkDir};
1612

1713
const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book");
18-
const TOC_TOP_LEVEL: &[&str] = &[
19-
"1. First Chapter",
20-
"2. Second Chapter",
21-
"Conclusion",
22-
"Dummy Book",
23-
"Introduction",
24-
];
25-
const TOC_SECOND_LEVEL: &[&str] = &[
26-
"1.1. Nested Chapter",
27-
"1.2. Includes",
28-
"1.3. Recursive",
29-
"1.4. Markdown",
30-
"1.5. Unicode",
31-
"1.6. No Headers",
32-
"1.7. Duplicate Headers",
33-
"1.8. Heading Attributes",
34-
"2.1. Nested Chapter",
35-
];
3614

3715
#[test]
3816
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
@@ -120,39 +98,6 @@ fn entry_ends_with(entry: &DirEntry, ending: &str) -> bool {
12098
entry.file_name().to_string_lossy().ends_with(ending)
12199
}
122100

123-
/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we
124-
/// can search with the `select` crate
125-
fn toc_fallback_html() -> Result<Document> {
126-
let temp = DummyBook::new()
127-
.build()
128-
.with_context(|| "Couldn't create the dummy book")?;
129-
MDBook::load(temp.path())?
130-
.build()
131-
.with_context(|| "Book building failed")?;
132-
133-
let toc_path = temp.path().join("book").join("toc.html");
134-
let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?;
135-
Ok(Document::from(html.as_str()))
136-
}
137-
138-
// don't use target="_parent" in IFRAME
139-
#[test]
140-
fn check_link_target_fallback() {
141-
let doc = toc_fallback_html().unwrap();
142-
143-
let num_parent_links = doc
144-
.find(
145-
Class("chapter")
146-
.descendant(Name("li"))
147-
.descendant(Name("a").and(Attr("target", "_parent"))),
148-
)
149-
.count();
150-
assert_eq!(
151-
num_parent_links,
152-
TOC_TOP_LEVEL.len() + TOC_SECOND_LEVEL.len()
153-
);
154-
}
155-
156101
#[test]
157102
fn example_book_can_build() {
158103
let example_book_dir = dummy_book::new_copy_of_example_book().unwrap();

tests/testsuite/toc.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! Tests for table of contents (sidebar).
22
33
use crate::prelude::*;
4+
use anyhow::Context;
5+
use mdbook::errors::*;
46
use select::document::Document;
57
use select::predicate::{Attr, Class, Name, Predicate};
8+
use std::fs;
69

710
const TOC_TOP_LEVEL: &[&str] = &[
811
"1. With Readme",
@@ -40,6 +43,17 @@ fn toc_js_html() -> Document {
4043
Document::from(html.as_str())
4144
}
4245

46+
/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we
47+
/// can search with the `select` crate
48+
fn toc_fallback_html() -> Result<Document> {
49+
let mut test = BookTest::from_dir("toc/basic_toc");
50+
test.build();
51+
52+
let toc_path = test.dir.join("book").join("toc.html");
53+
let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?;
54+
Ok(Document::from(html.as_str()))
55+
}
56+
4357
#[test]
4458
fn check_second_toc_level() {
4559
let doc = toc_js_html();
@@ -110,3 +124,21 @@ fn check_link_target_js() {
110124
.count();
111125
assert_eq!(num_parent_links, 0);
112126
}
127+
128+
// don't use target="_parent" in IFRAME
129+
#[test]
130+
fn check_link_target_fallback() {
131+
let doc = toc_fallback_html().unwrap();
132+
133+
let num_parent_links = doc
134+
.find(
135+
Class("chapter")
136+
.descendant(Name("li"))
137+
.descendant(Name("a").and(Attr("target", "_parent"))),
138+
)
139+
.count();
140+
assert_eq!(
141+
num_parent_links,
142+
TOC_TOP_LEVEL.len() + TOC_SECOND_LEVEL.len()
143+
);
144+
}

0 commit comments

Comments
 (0)