Fix mdbook-core book tests
These tests were moved in https://github.com/rust-lang/mdBook/pull/2766, but the `mod tests` was missing. This fixes this missing `mod`, and updates the tests so that they pass.
This commit is contained in:
parent
6be8e526d6
commit
24c7ffcd62
2 changed files with 100 additions and 98 deletions
|
|
@ -6,6 +6,9 @@ use std::fmt::{self, Display, Formatter};
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
/// A tree structure representing a book.
|
/// A tree structure representing a book.
|
||||||
///
|
///
|
||||||
/// For the moment a book is just a collection of [`BookItems`] which are
|
/// For the moment a book is just a collection of [`BookItems`] which are
|
||||||
|
|
|
||||||
|
|
@ -14,111 +14,110 @@ fn section_number_has_correct_dotted_representation() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn book_iter_iterates_over_sequential_items() {
|
fn book_iter_iterates_over_sequential_items() {
|
||||||
let sections = vec![
|
let items = vec![
|
||||||
BookItem::Chapter(Chapter {
|
BookItem::Chapter(Chapter {
|
||||||
name: String::from("Chapter 1"),
|
name: String::from("Chapter 1"),
|
||||||
content: String::from("# Chapter 1"),
|
content: String::from("# Chapter 1"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
|
BookItem::Separator,
|
||||||
|
];
|
||||||
|
let book = Book::new_with_items(items);
|
||||||
|
|
||||||
|
let should_be: Vec<_> = book.sections.iter().collect();
|
||||||
|
|
||||||
|
let got: Vec<_> = book.iter().collect();
|
||||||
|
|
||||||
|
assert_eq!(got, should_be);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn for_each_mut_visits_all_items() {
|
||||||
|
let items = vec![
|
||||||
|
BookItem::Chapter(Chapter {
|
||||||
|
name: String::from("Chapter 1"),
|
||||||
|
content: String::from("# Chapter 1"),
|
||||||
|
number: None,
|
||||||
|
path: Some(PathBuf::from("Chapter_1/index.md")),
|
||||||
|
source_path: Some(PathBuf::from("Chapter_1/index.md")),
|
||||||
|
parent_names: Vec::new(),
|
||||||
|
sub_items: vec![
|
||||||
|
BookItem::Chapter(Chapter::new(
|
||||||
|
"Hello World",
|
||||||
|
String::new(),
|
||||||
|
"Chapter_1/hello.md",
|
||||||
|
Vec::new(),
|
||||||
|
)),
|
||||||
BookItem::Separator,
|
BookItem::Separator,
|
||||||
];
|
BookItem::Chapter(Chapter::new(
|
||||||
let book = Book::new_with_sections(sections);
|
"Goodbye World",
|
||||||
|
String::new(),
|
||||||
|
"Chapter_1/goodbye.md",
|
||||||
|
Vec::new(),
|
||||||
|
)),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
BookItem::Separator,
|
||||||
|
];
|
||||||
|
let mut book = Book::new_with_items(items);
|
||||||
|
|
||||||
let should_be: Vec<_> = book.sections.iter().collect();
|
let num_items = book.iter().count();
|
||||||
|
let mut visited = 0;
|
||||||
|
|
||||||
let got: Vec<_> = book.iter().collect();
|
book.for_each_mut(|_| visited += 1);
|
||||||
|
|
||||||
assert_eq!(got, should_be);
|
assert_eq!(visited, num_items);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn for_each_mut_visits_all_items() {
|
fn iterate_over_nested_book_items() {
|
||||||
let sections = vec![
|
let items = vec![
|
||||||
BookItem::Chapter(Chapter {
|
BookItem::Chapter(Chapter {
|
||||||
name: String::from("Chapter 1"),
|
name: String::from("Chapter 1"),
|
||||||
content: String::from("# Chapter 1"),
|
content: String::from("# Chapter 1"),
|
||||||
number: None,
|
number: None,
|
||||||
path: Some(PathBuf::from("Chapter_1/index.md")),
|
path: Some(PathBuf::from("Chapter_1/index.md")),
|
||||||
source_path: Some(PathBuf::from("Chapter_1/index.md")),
|
source_path: Some(PathBuf::from("Chapter_1/index.md")),
|
||||||
parent_names: Vec::new(),
|
parent_names: Vec::new(),
|
||||||
sub_items: vec![
|
sub_items: vec![
|
||||||
BookItem::Chapter(Chapter::new(
|
BookItem::Chapter(Chapter::new(
|
||||||
"Hello World",
|
"Hello World",
|
||||||
String::new(),
|
String::new(),
|
||||||
"Chapter_1/hello.md",
|
"Chapter_1/hello.md",
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
)),
|
)),
|
||||||
BookItem::Separator,
|
|
||||||
BookItem::Chapter(Chapter::new(
|
|
||||||
"Goodbye World",
|
|
||||||
String::new(),
|
|
||||||
"Chapter_1/goodbye.md",
|
|
||||||
Vec::new(),
|
|
||||||
)),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
BookItem::Separator,
|
BookItem::Separator,
|
||||||
];
|
BookItem::Chapter(Chapter::new(
|
||||||
let mut book = Book::new_with_sections(sections);
|
"Goodbye World",
|
||||||
|
String::new(),
|
||||||
|
"Chapter_1/goodbye.md",
|
||||||
|
Vec::new(),
|
||||||
|
)),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
BookItem::Separator,
|
||||||
|
];
|
||||||
|
let book = Book::new_with_items(items);
|
||||||
|
|
||||||
let num_items = book.iter().count();
|
let got: Vec<_> = book.iter().collect();
|
||||||
let mut visited = 0;
|
|
||||||
|
|
||||||
book.for_each_mut(|_| visited += 1);
|
assert_eq!(got.len(), 5);
|
||||||
|
|
||||||
assert_eq!(visited, num_items);
|
// checking the chapter names are in the order should be sufficient here...
|
||||||
}
|
let chapter_names: Vec<String> = got
|
||||||
|
.into_iter()
|
||||||
#[test]
|
.filter_map(|i| match *i {
|
||||||
fn iterate_over_nested_book_items() {
|
BookItem::Chapter(ref ch) => Some(ch.name.clone()),
|
||||||
let sections = vec![
|
_ => None,
|
||||||
BookItem::Chapter(Chapter {
|
})
|
||||||
name: String::from("Chapter 1"),
|
.collect();
|
||||||
content: String::from("# Chapter 1"),
|
let should_be: Vec<_> = vec![
|
||||||
number: None,
|
String::from("Chapter 1"),
|
||||||
path: Some(PathBuf::from("Chapter_1/index.md")),
|
String::from("Hello World"),
|
||||||
source_path: Some(PathBuf::from("Chapter_1/index.md")),
|
String::from("Goodbye World"),
|
||||||
parent_names: Vec::new(),
|
];
|
||||||
sub_items: vec![
|
|
||||||
BookItem::Chapter(Chapter::new(
|
|
||||||
"Hello World",
|
|
||||||
String::new(),
|
|
||||||
"Chapter_1/hello.md",
|
|
||||||
Vec::new(),
|
|
||||||
)),
|
|
||||||
BookItem::Separator,
|
|
||||||
BookItem::Chapter(Chapter::new(
|
|
||||||
"Goodbye World",
|
|
||||||
String::new(),
|
|
||||||
"Chapter_1/goodbye.md",
|
|
||||||
Vec::new(),
|
|
||||||
)),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
BookItem::Separator,
|
|
||||||
];
|
|
||||||
let book = Book::new_with_sections(sections);
|
|
||||||
|
|
||||||
let got: Vec<_> = book.iter().collect();
|
|
||||||
|
|
||||||
assert_eq!(got.len(), 5);
|
|
||||||
|
|
||||||
// checking the chapter names are in the order should be sufficient here...
|
|
||||||
let chapter_names: Vec<String> = got
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|i| match *i {
|
|
||||||
BookItem::Chapter(ref ch) => Some(ch.name.clone()),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
let should_be: Vec<_> = vec![
|
|
||||||
String::from("Chapter 1"),
|
|
||||||
String::from("Hello World"),
|
|
||||||
String::from("Goodbye World"),
|
|
||||||
];
|
|
||||||
|
|
||||||
assert_eq!(chapter_names, should_be);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
assert_eq!(chapter_names, should_be);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue