Unit tests now all pass

This commit is contained in:
Michael Bryan 2017-08-21 10:46:29 +08:00
parent c4da845974
commit d39352aa45
6 changed files with 30 additions and 18 deletions

View File

@ -40,10 +40,6 @@ iron = { version = "0.5", optional = true }
staticfile = { version = "0.4", optional = true } staticfile = { version = "0.4", optional = true }
ws = { version = "0.7", optional = true} ws = { version = "0.7", optional = true}
# Tests
[dev-dependencies]
tempdir = "0.3.4"
[build-dependencies] [build-dependencies]
error-chain = "0.11" error-chain = "0.11"
@ -59,3 +55,6 @@ serve = ["iron", "staticfile", "ws"]
doc = false doc = false
name = "mdbook" name = "mdbook"
path = "src/bin/mdbook.rs" path = "src/bin/mdbook.rs"
[dev-dependencies]
pretty_assertions = "0.2.1"

View File

@ -1,4 +1,4 @@
use std::path::Path; use std::path::{Path, PathBuf};
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -67,14 +67,17 @@ pub struct Chapter {
pub number: Option<SectionNumber>, pub number: Option<SectionNumber>,
/// Nested items. /// Nested items.
pub sub_items: Vec<BookItem>, pub sub_items: Vec<BookItem>,
/// The chapter's location, relative to the `SUMMARY.md` file.
pub path: PathBuf,
} }
impl Chapter { impl Chapter {
/// Create a new chapter with the provided content. /// Create a new chapter with the provided content.
pub fn new(name: &str, content: String) -> Chapter { pub fn new<P: Into<PathBuf>>(name: &str, content: String, path: P) -> Chapter {
Chapter { Chapter {
name: name.to_string(), name: name.to_string(),
content: content, content: content,
path: path.into(),
..Default::default() ..Default::default()
} }
} }
@ -119,14 +122,17 @@ fn load_chapter<P: AsRef<Path>>(link: &Link, src_dir: P) -> Result<Chapter> {
src_dir.join(&link.location) src_dir.join(&link.location)
}; };
let mut f = File::open(location).chain_err(|| { let mut f = File::open(&location).chain_err(|| {
format!("Chapter file not found, {}", link.location.display()) format!("Chapter file not found, {}", link.location.display())
})?; })?;
let mut content = String::new(); let mut content = String::new();
f.read_to_string(&mut content)?; f.read_to_string(&mut content)?;
let mut ch = Chapter::new(&link.name, content); let stripped = location.strip_prefix(&src_dir).expect("Chapters are always inside a book");
println!("{} {} => {}", src_dir.display(), location.display(), stripped.display());
let mut ch = Chapter::new(&link.name, content, stripped);
ch.number = link.number.clone(); ch.number = link.number.clone();
let sub_items = link.nested_items let sub_items = link.nested_items
@ -223,7 +229,7 @@ And here is some more text.
#[test] #[test]
fn load_a_single_chapter_from_disk() { fn load_a_single_chapter_from_disk() {
let (link, temp_dir) = dummy_link(); let (link, temp_dir) = dummy_link();
let should_be = Chapter::new("Chapter 1", DUMMY_SRC.to_string()); let should_be = Chapter::new("Chapter 1", DUMMY_SRC.to_string(), "chapter_1.md");
let got = load_chapter(&link, temp_dir.path()).unwrap(); let got = load_chapter(&link, temp_dir.path()).unwrap();
assert_eq!(got, should_be); assert_eq!(got, should_be);
@ -239,18 +245,20 @@ And here is some more text.
#[test] #[test]
fn load_recursive_link_with_separators() { fn load_recursive_link_with_separators() {
let (root, _temp) = nested_links(); let (root, temp) = nested_links();
let nested = Chapter { let nested = Chapter {
name: String::from("Nested Chapter 1"), name: String::from("Nested Chapter 1"),
content: String::from("Hello World!"), content: String::from("Hello World!"),
number: Some(SectionNumber(vec![1, 2])), number: Some(SectionNumber(vec![1, 2])),
path: PathBuf::from("second.md"),
sub_items: Vec::new(), sub_items: Vec::new(),
}; };
let should_be = BookItem::Chapter(Chapter { let should_be = BookItem::Chapter(Chapter {
name: String::from("Chapter 1"), name: String::from("Chapter 1"),
content: String::from(DUMMY_SRC), content: String::from(DUMMY_SRC),
number: None, number: None,
path: PathBuf::from("chapter_1.md"),
sub_items: vec![ sub_items: vec![
BookItem::Chapter(nested.clone()), BookItem::Chapter(nested.clone()),
BookItem::Separator, BookItem::Separator,
@ -258,13 +266,13 @@ And here is some more text.
], ],
}); });
let got = load_summary_item(&SummaryItem::Link(root), "").unwrap(); let got = load_summary_item(&SummaryItem::Link(root), temp.path()).unwrap();
assert_eq!(got, should_be); assert_eq!(got, should_be);
} }
#[test] #[test]
fn load_a_book_with_a_single_chapter() { fn load_a_book_with_a_single_chapter() {
let (link, _temp) = dummy_link(); let (link, temp) = dummy_link();
let summary = Summary { let summary = Summary {
numbered_chapters: vec![SummaryItem::Link(link)], numbered_chapters: vec![SummaryItem::Link(link)],
..Default::default() ..Default::default()
@ -274,12 +282,13 @@ And here is some more text.
BookItem::Chapter(Chapter { BookItem::Chapter(Chapter {
name: String::from("Chapter 1"), name: String::from("Chapter 1"),
content: String::from(DUMMY_SRC), content: String::from(DUMMY_SRC),
path: PathBuf::from("chapter_1.md"),
..Default::default() ..Default::default()
}), }),
], ],
}; };
let got = load_book_from_disk(&summary, "").unwrap(); let got = load_book_from_disk(&summary, temp.path()).unwrap();
assert_eq!(got, should_be); assert_eq!(got, should_be);
} }
@ -312,10 +321,11 @@ And here is some more text.
name: String::from("Chapter 1"), name: String::from("Chapter 1"),
content: String::from(DUMMY_SRC), content: String::from(DUMMY_SRC),
number: None, number: None,
path: PathBuf::from("Chapter_1/index.md"),
sub_items: vec![ sub_items: vec![
BookItem::Chapter(Chapter::new("Hello World", String::new())), BookItem::Chapter(Chapter::new("Hello World", String::new(), "Chapter_1/hello.md")),
BookItem::Separator, BookItem::Separator,
BookItem::Chapter(Chapter::new("Goodbye World", String::new())), BookItem::Chapter(Chapter::new("Goodbye World", String::new(), "Chapter_1/goodbye.md")),
], ],
}), }),
BookItem::Separator, BookItem::Separator,

View File

@ -108,7 +108,7 @@ impl MDBook {
/// ``` /// ```
pub fn iter(&self) -> BookItems { pub fn iter(&self) -> BookItems {
self.content.expect("Trying to iterate over a book before it is loaded. This is a bug") self.content.as_ref().expect("Trying to iterate over a book before it is loaded. This is a bug")
.iter() .iter()
} }

View File

@ -89,6 +89,9 @@ extern crate tempdir;
#[cfg(test)] #[cfg(test)]
extern crate tempdir; extern crate tempdir;
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;
mod preprocess; mod preprocess;
pub mod book; pub mod book;

View File

@ -411,7 +411,7 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
match *item { match *item {
BookItem::Chapter(ref ch) => { BookItem::Chapter(ref ch) => {
chapter.insert("section".to_owned(), json!(s)); chapter.insert("section".to_owned(), json!(ch.number.clone()));
chapter.insert("name".to_owned(), json!(ch.name)); chapter.insert("name".to_owned(), json!(ch.name));
let path = ch.path.to_str().ok_or_else(|| { let path = ch.path.to_str().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "Could not convert path to str") io::Error::new(io::ErrorKind::Other, "Could not convert path to str")

View File

@ -5,7 +5,7 @@ extern crate env_logger;
use std::path::PathBuf; use std::path::PathBuf;
use mdbook::loader::load_book; use mdbook::book::book::load_book;
#[test] #[test]