use crate::sample;
use mbook::{Book, Error, Landmark, Problem, ProblemKind, Result, SourceError, Toc, TocItem};
fn problems(book: &Book) -> Vec<(Option<String>, ProblemKind)> {
match book.check() {
Ok(()) => Vec::new(),
Err(problems) => problems
.iter()
.map(|Problem { path, kind, .. }| (path.clone(), kind.clone()))
.collect(),
}
}
fn with_chapter(text: &str) -> Result<Book> {
let mut book = sample::book()?;
book.add_chapter("extra.md", text)?;
Ok(book)
}
fn in_extra(kind: ProblemKind) -> Vec<(Option<String>, ProblemKind)> {
vec![(Some("extra.md".to_owned()), kind)]
}
#[test]
fn the_example_book_has_no_problems() -> Result<()> {
assert!(sample::book()?.check().is_ok());
Ok(())
}
#[test]
fn a_link_to_a_missing_chapter_is_reported() -> Result<()> {
let book = with_chapter("See [nine](ch09.md).\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::UnresolvedLink("ch09.md".into()))
);
Ok(())
}
#[test]
fn a_link_to_a_missing_anchor_is_reported() -> Result<()> {
let book = with_chapter("See [it](ch01.md#no-such-heading).\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::UnresolvedLink(
"ch01.md#no-such-heading".into()
))
);
Ok(())
}
#[test]
fn a_link_to_a_picture_is_reported() -> Result<()> {
let book = with_chapter("Open [full size](img/fig-1.avif).\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::UnresolvedLink("img/fig-1.avif".into()))
);
Ok(())
}
#[test]
fn a_link_into_the_same_chapter_resolves() -> Result<()> {
let book = with_chapter("# Top\n\nBack to [top](#top).\n")?;
assert_eq!(problems(&book), []);
Ok(())
}
#[test]
fn external_links_are_allowed() -> Result<()> {
let book = with_chapter("[web](https://example.org) and [mail](mailto:a@example.org).\n")?;
assert_eq!(problems(&book), []);
Ok(())
}
#[test]
fn a_link_with_another_scheme_is_reported() -> Result<()> {
let book = with_chapter("[x](ftp://example.org/file)\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::ForbiddenScheme(
"ftp://example.org/file".into()
))
);
Ok(())
}
#[test]
fn a_picture_that_is_not_in_the_book_is_reported() -> Result<()> {
let book = with_chapter("\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::UnresolvedPicture("img/none.avif".into()))
);
Ok(())
}
#[test]
fn a_picture_that_names_a_chapter_is_reported() -> Result<()> {
let book = with_chapter("\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::UnresolvedPicture("ch01.md".into()))
);
Ok(())
}
#[test]
fn a_picture_with_a_fragment_is_reported() -> Result<()> {
let book = with_chapter("\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::UnresolvedPicture("img/fig-1.avif#x".into()))
);
Ok(())
}
#[test]
fn a_picture_from_the_network_is_reported() -> Result<()> {
let book = with_chapter("\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::ForbiddenScheme(
"https://example.org/pixel.avif".into()
))
);
Ok(())
}
#[test]
fn raw_html_is_reported_with_its_place() -> Result<()> {
let book = with_chapter("Text.\n\n<div>block</div>\n")?;
let Err(found) = book.check() else {
panic!("raw HTML must be reported");
};
let places: Vec<_> = found.iter().map(|p| (p.offset, p.kind.clone())).collect();
assert_eq!(places, [(Some(7), ProblemKind::RawHtml)]);
Ok(())
}
#[test]
fn a_cover_that_is_not_a_picture_of_the_book_is_reported() -> Result<()> {
let mut book = sample::book()?;
book.meta_mut().set_cover("img/gone.avif")?;
assert_eq!(problems(&book), [(None, ProblemKind::MissingCover)]);
Ok(())
}
#[test]
fn a_removed_picture_leaves_a_reported_reference() -> Result<()> {
let mut book = sample::book()?;
book.remove_picture("img/fig-1.avif");
assert_eq!(
problems(&book),
[(
Some("ch01.md".to_owned()),
ProblemKind::UnresolvedPicture("img/fig-1.avif".into())
)]
);
Ok(())
}
#[test]
fn a_removed_chapter_leaves_a_reported_link() -> Result<()> {
let mut book = sample::book()?;
book.remove_chapter("ch01.md");
assert_eq!(
problems(&book),
[(
Some("ch02.md".to_owned()),
ProblemKind::UnresolvedLink("ch01.md#the-station".into())
)]
);
Ok(())
}
#[test]
fn every_problem_is_reported_not_only_the_first() -> Result<()> {
let mut book = with_chapter("[a](none.md) <b>x</b> \n")?;
book.meta_mut().set_cover("nope.avif")?;
let kinds: Vec<_> = problems(&book).into_iter().map(|(_, kind)| kind).collect();
assert_eq!(kinds.len(), 5, "{kinds:?}");
Ok(())
}
fn contents_problems(items: Vec<TocItem>) -> Result<Vec<ProblemKind>> {
let mut book = sample::book()?;
book.set_toc(Toc::Explicit(items));
Ok(problems(&book).into_iter().map(|(_, kind)| kind).collect())
}
#[test]
fn a_contents_item_to_a_missing_chapter_is_reported() -> Result<()> {
let found = contents_problems(vec![TocItem::new(1, "Nine", "ch09.md")])?;
assert_eq!(found, [ProblemKind::DanglingTocItem("Nine".into())]);
Ok(())
}
#[test]
fn a_contents_item_to_a_missing_anchor_is_reported() -> Result<()> {
let found = contents_problems(vec![TocItem::new(1, "Lost", "ch01.md#lost")])?;
assert_eq!(found, [ProblemKind::DanglingTocItem("Lost".into())]);
Ok(())
}
#[test]
fn contents_that_start_below_the_top_level_are_reported() -> Result<()> {
let found = contents_problems(vec![TocItem::new(2, "Deep", "ch01.md")])?;
assert_eq!(found, [ProblemKind::TocLevel("Deep".into())]);
Ok(())
}
#[test]
fn contents_that_skip_a_level_are_reported() -> Result<()> {
let found = contents_problems(vec![
TocItem::new(1, "One", "ch01.md"),
TocItem::new(3, "Three", "ch01.md#the-station"),
])?;
assert_eq!(found, [ProblemKind::TocLevel("Three".into())]);
Ok(())
}
#[test]
fn contents_out_of_reading_order_are_reported() -> Result<()> {
let found = contents_problems(vec![
TocItem::new(1, "Two", "ch02.md"),
TocItem::new(1, "One", "ch01.md"),
])?;
assert_eq!(found, [ProblemKind::TocOrder("One".into())]);
Ok(())
}
#[test]
fn a_contents_item_without_a_title_is_reported() -> Result<()> {
let found = contents_problems(vec![TocItem::new(1, "", "ch01.md")])?;
assert_eq!(found, [ProblemKind::EmptyTocTitle]);
Ok(())
}
#[test]
fn an_invalid_book_is_not_written() -> Result<()> {
let book = with_chapter("[a](none.md)\n")?;
let mut bytes = Vec::new();
let result = book.write(&mut bytes);
assert!(matches!(result, Err(Error::Invalid(found)) if found.len() == 1));
assert_eq!(bytes.len(), 0);
Ok(())
}
#[test]
fn links_to_extra_anchors_pass_the_check() -> Result<()> {
assert!(sample::annotated()?.check().is_ok());
Ok(())
}
#[test]
fn a_link_to_a_name_that_is_no_longer_defined_is_reported() -> Result<()> {
let mut book = sample::annotated()?;
if let Some(chapter) = book.chapter_mut("ch01.md") {
chapter.set_text(chapter.text().replace("[fig-1]: #fig-1\n", ""))?;
}
assert_eq!(
problems(&book),
[(
Some("ch03.md".to_owned()),
ProblemKind::UnresolvedLink("ch01.md#fig-1".into())
)]
);
Ok(())
}
#[test]
fn an_edit_that_keeps_the_anchor_definitions_keeps_the_links() -> Result<()> {
let mut book = sample::annotated()?;
if let Some(chapter) = book.chapter_mut("ch01.md") {
chapter.set_text(chapter.text().replace("late", "very late"))?;
}
assert_eq!(problems(&book), []);
Ok(())
}
#[test]
fn a_reference_link_through_an_anchor_definition_resolves() -> Result<()> {
let book = with_chapter("[here]: #here\nA line.\n\nBack to [that line][here].\n")?;
assert_eq!(problems(&book), []);
Ok(())
}
#[test]
fn a_contents_item_cannot_lead_to_an_extra_anchor() -> Result<()> {
let mut book = sample::annotated()?;
book.set_toc(Toc::Explicit(vec![TocItem::new(
1,
"The figure",
"ch01.md#fig-1",
)]));
assert_eq!(
problems(&book),
[(None, ProblemKind::DanglingTocItem("The figure".into()))]
);
Ok(())
}
#[test]
fn landmarks_that_lead_somewhere_pass_the_check() -> Result<()> {
assert!(sample::with_landmarks()?.check().is_ok());
Ok(())
}
#[test]
fn a_landmark_that_leads_nowhere_is_reported() -> Result<()> {
let mut book = sample::annotated()?;
book.set_landmarks(vec![
Landmark::new("start", "", "ch09.md"),
Landmark::new("index", "", "ch01.md#no-such-anchor"),
Landmark::new("glossary", "", "img/fig-1.avif"),
Landmark::new("preface", "", "ch02.md"),
])?;
assert_eq!(
problems(&book),
[
(None, ProblemKind::DanglingLandmark("start".into())),
(None, ProblemKind::DanglingLandmark("index".into())),
(None, ProblemKind::DanglingLandmark("glossary".into())),
]
);
Ok(())
}
#[test]
fn a_landmark_follows_an_anchor_definition_that_is_removed() -> Result<()> {
let mut book = sample::with_landmarks()?;
if let Some(chapter) = book.chapter_mut("ch01.md") {
chapter.set_text(crate::texts::CH01)?;
}
let kinds: Vec<_> = problems(&book).into_iter().map(|(_, kind)| kind).collect();
assert!(kinds.contains(&ProblemKind::DanglingLandmark("list-of-figures".into())));
assert!(!kinds.contains(&ProblemKind::DanglingLandmark("preface".into())));
Ok(())
}
#[test]
fn a_book_that_shows_a_diagram_and_a_formula_has_no_problems() -> Result<()> {
assert!(sample::illustrated()?.check().is_ok());
Ok(())
}
#[test]
fn a_diagram_and_a_formula_are_shown_from_another_folder() -> Result<()> {
let mut book = sample::illustrated()?;
book.add_chapter(
"part/extra.md",
" and \n",
)?;
assert_eq!(problems(&book), []);
Ok(())
}
#[test]
fn a_link_to_a_diagram_or_to_a_formula_is_reported() -> Result<()> {
let mut book = sample::illustrated()?;
book.add_chapter(
"extra.md",
"[way](dia/way.mmd) and [speed](math/speed.tex)\n",
)?;
assert_eq!(
problems(&book),
[
ProblemKind::UnresolvedLink("dia/way.mmd".into()),
ProblemKind::UnresolvedLink("math/speed.tex".into())
]
.map(|kind| (Some("extra.md".to_owned()), kind))
);
Ok(())
}
#[test]
fn a_diagram_with_a_fragment_is_reported() -> Result<()> {
let mut book = sample::illustrated()?;
book.add_chapter("extra.md", "\n")?;
assert_eq!(
problems(&book),
in_extra(ProblemKind::UnresolvedPicture("dia/way.mmd#station".into()))
);
Ok(())
}
#[test]
fn a_removed_diagram_and_a_removed_formula_leave_reported_references() -> Result<()> {
let mut book = sample::illustrated()?;
assert!(book.remove_diagram("dia/way.mmd").is_some());
assert!(book.remove_formula("math/speed.tex").is_some());
assert_eq!(
problems(&book),
[
ProblemKind::UnresolvedPicture("dia/way.mmd".into()),
ProblemKind::UnresolvedPicture("math/speed.tex".into())
]
.map(|kind| (Some("ch04.md".to_owned()), kind))
);
Ok(())
}
#[test]
fn a_cover_that_is_a_diagram_is_reported() -> Result<()> {
let mut book = sample::illustrated()?;
book.meta_mut().set_cover("dia/way.mmd")?;
assert_eq!(problems(&book), [(None, ProblemKind::MissingCover)]);
Ok(())
}
#[test]
fn a_diagram_that_nothing_shows_is_no_problem() -> Result<()> {
let mut book = sample::book()?;
book.add_diagram("dia/unused.mmd", "flowchart TD\n A --> B\n")?;
book.add_formula("math/unused.tex", "x")?;
assert_eq!(problems(&book), []);
Ok(())
}
#[test]
fn an_empty_source_is_refused_when_it_is_given() -> Result<()> {
let mut book = sample::book()?;
assert!(matches!(
book.add_diagram("dia/empty.mmd", "\n"),
Err(Error::InvalidDiagram(SourceError::Empty))
));
assert!(matches!(
book.add_formula("math/empty.tex", " "),
Err(Error::InvalidFormula(SourceError::Empty))
));
assert!(book.diagrams().is_empty() && book.formulas().is_empty());
Ok(())
}