Migrate check_link_target_fallback to BookTest
This commit is contained in:
parent
5f2453e446
commit
69972080f0
2 changed files with 32 additions and 55 deletions
|
|
@ -2,37 +2,15 @@ mod dummy_book;
|
||||||
|
|
||||||
use crate::dummy_book::{assert_contains_strings, DummyBook};
|
use crate::dummy_book::{assert_contains_strings, DummyBook};
|
||||||
|
|
||||||
use anyhow::Context;
|
|
||||||
use mdbook::config::Config;
|
use mdbook::config::Config;
|
||||||
use mdbook::errors::*;
|
|
||||||
use mdbook::MDBook;
|
use mdbook::MDBook;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use select::document::Document;
|
|
||||||
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 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");
|
||||||
const TOC_TOP_LEVEL: &[&str] = &[
|
|
||||||
"1. First Chapter",
|
|
||||||
"2. Second Chapter",
|
|
||||||
"Conclusion",
|
|
||||||
"Dummy Book",
|
|
||||||
"Introduction",
|
|
||||||
];
|
|
||||||
const TOC_SECOND_LEVEL: &[&str] = &[
|
|
||||||
"1.1. Nested Chapter",
|
|
||||||
"1.2. Includes",
|
|
||||||
"1.3. Recursive",
|
|
||||||
"1.4. Markdown",
|
|
||||||
"1.5. Unicode",
|
|
||||||
"1.6. No Headers",
|
|
||||||
"1.7. Duplicate Headers",
|
|
||||||
"1.8. Heading Attributes",
|
|
||||||
"2.1. Nested Chapter",
|
|
||||||
];
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
|
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
|
||||||
|
|
@ -120,39 +98,6 @@ fn entry_ends_with(entry: &DirEntry, ending: &str) -> bool {
|
||||||
entry.file_name().to_string_lossy().ends_with(ending)
|
entry.file_name().to_string_lossy().ends_with(ending)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we
|
|
||||||
/// can search with the `select` crate
|
|
||||||
fn toc_fallback_html() -> Result<Document> {
|
|
||||||
let temp = DummyBook::new()
|
|
||||||
.build()
|
|
||||||
.with_context(|| "Couldn't create the dummy book")?;
|
|
||||||
MDBook::load(temp.path())?
|
|
||||||
.build()
|
|
||||||
.with_context(|| "Book building failed")?;
|
|
||||||
|
|
||||||
let toc_path = temp.path().join("book").join("toc.html");
|
|
||||||
let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?;
|
|
||||||
Ok(Document::from(html.as_str()))
|
|
||||||
}
|
|
||||||
|
|
||||||
// don't use target="_parent" in IFRAME
|
|
||||||
#[test]
|
|
||||||
fn check_link_target_fallback() {
|
|
||||||
let doc = toc_fallback_html().unwrap();
|
|
||||||
|
|
||||||
let num_parent_links = doc
|
|
||||||
.find(
|
|
||||||
Class("chapter")
|
|
||||||
.descendant(Name("li"))
|
|
||||||
.descendant(Name("a").and(Attr("target", "_parent"))),
|
|
||||||
)
|
|
||||||
.count();
|
|
||||||
assert_eq!(
|
|
||||||
num_parent_links,
|
|
||||||
TOC_TOP_LEVEL.len() + TOC_SECOND_LEVEL.len()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn example_book_can_build() {
|
fn example_book_can_build() {
|
||||||
let example_book_dir = dummy_book::new_copy_of_example_book().unwrap();
|
let example_book_dir = dummy_book::new_copy_of_example_book().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
//! Tests for table of contents (sidebar).
|
//! Tests for table of contents (sidebar).
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use anyhow::Context;
|
||||||
|
use mdbook::errors::*;
|
||||||
use select::document::Document;
|
use select::document::Document;
|
||||||
use select::predicate::{Attr, Class, Name, Predicate};
|
use select::predicate::{Attr, Class, Name, Predicate};
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
const TOC_TOP_LEVEL: &[&str] = &[
|
const TOC_TOP_LEVEL: &[&str] = &[
|
||||||
"1. With Readme",
|
"1. With Readme",
|
||||||
|
|
@ -40,6 +43,17 @@ fn toc_js_html() -> Document {
|
||||||
Document::from(html.as_str())
|
Document::from(html.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we
|
||||||
|
/// can search with the `select` crate
|
||||||
|
fn toc_fallback_html() -> Result<Document> {
|
||||||
|
let mut test = BookTest::from_dir("toc/basic_toc");
|
||||||
|
test.build();
|
||||||
|
|
||||||
|
let toc_path = test.dir.join("book").join("toc.html");
|
||||||
|
let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?;
|
||||||
|
Ok(Document::from(html.as_str()))
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_second_toc_level() {
|
fn check_second_toc_level() {
|
||||||
let doc = toc_js_html();
|
let doc = toc_js_html();
|
||||||
|
|
@ -110,3 +124,21 @@ fn check_link_target_js() {
|
||||||
.count();
|
.count();
|
||||||
assert_eq!(num_parent_links, 0);
|
assert_eq!(num_parent_links, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// don't use target="_parent" in IFRAME
|
||||||
|
#[test]
|
||||||
|
fn check_link_target_fallback() {
|
||||||
|
let doc = toc_fallback_html().unwrap();
|
||||||
|
|
||||||
|
let num_parent_links = doc
|
||||||
|
.find(
|
||||||
|
Class("chapter")
|
||||||
|
.descendant(Name("li"))
|
||||||
|
.descendant(Name("a").and(Attr("target", "_parent"))),
|
||||||
|
)
|
||||||
|
.count();
|
||||||
|
assert_eq!(
|
||||||
|
num_parent_links,
|
||||||
|
TOC_TOP_LEVEL.len() + TOC_SECOND_LEVEL.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue