diff --git a/book-example/book.toml b/book-example/book.toml index 996e7309..6bab0772 100644 --- a/book-example/book.toml +++ b/book-example/book.toml @@ -1,5 +1,3 @@ title = "mdBook Documentation" description = "Create books from markdown files. Like Gitbook but implemented in Rust." - -[[authors]] -name = "Mathieu David" +author = "Mathieu David" diff --git a/data/book-init/book.toml b/data/book-init/book.toml index 628d6842..d56e9637 100644 --- a/data/book-init/book.toml +++ b/data/book-init/book.toml @@ -1,2 +1,2 @@ -title = "An Empty Canvas" +title = "The Title" author = "The Author" diff --git a/src/tests/hbs_renderer_multilang.rs b/src/tests/hbs_renderer_multilang.rs index c79abdcc..5cbff3e4 100644 --- a/src/tests/hbs_renderer_multilang.rs +++ b/src/tests/hbs_renderer_multilang.rs @@ -38,7 +38,22 @@ fn it_renders_multilanguage_book() { assert!(s.contains("")); assert!(s.contains("

The Pool of Tears

")); assert!(s.contains("")); - assert!(s.contains("li>2. The Pool of Tears")); + + let does_it_contain = |check_str: &str, fmt_str: &str, path_parts: Vec<&str>| -> bool { + let mut p = PathBuf::from(""); + for i in path_parts.iter() { + p = p.join(i); + } + let ps = p.to_str().unwrap(); + // Handmade formatting with replace. For anything more, it'll need Handlebars. + let text = fmt_str.replace("{}", ps); + check_str.contains(&text) + }; + + assert!(does_it_contain( + &s, "
  • 2. The Pool of Tears
  • ", + vec!["en", "tears.html"] + )); book_path = proj.translations.get("fr").unwrap().config.get_dest(); chapter_path = book_path.join("tears.html"); @@ -63,9 +78,10 @@ fn it_renders_multilanguage_book() { book_path = proj.translations.get("hu").unwrap().config.get_dest(); chapter_path = book_path.join("tarka-farka.html"); s = utils::fs::file_to_string(&chapter_path).unwrap(); - assert!(s.contains("en")); - assert!(s.contains("hu")); - assert!(s.contains("fr")); + + assert!(does_it_contain(&s, "en", vec!["en", "index.html"])); + assert!(does_it_contain(&s, "hu", vec!["hu", "index.html"])); + assert!(does_it_contain(&s, "fr", vec!["fr", "index.html"])); // Test if translation links are found @@ -73,21 +89,24 @@ fn it_renders_multilanguage_book() { chapter_path = book_path.join("rabbit-hole.html"); s = utils::fs::file_to_string(&chapter_path).unwrap(); - assert!(s.contains("en")); - assert!(s.contains("hu")); - assert!(s.contains("fr")); + + assert!(does_it_contain(&s, "en", vec!["en", "rabbit-hole.html"])); + assert!(does_it_contain(&s, "hu", vec!["hu", "nyuszi.html"])); + assert!(does_it_contain(&s, "fr", vec!["fr", "terrier.html"])); chapter_path = book_path.join("tears.html"); s = utils::fs::file_to_string(&chapter_path).unwrap(); - assert!(s.contains("en")); - assert!(s.contains("fr")); - assert!(s.contains("hu")); + + assert!(does_it_contain(&s, "en", vec!["en", "tears.html"])); + assert!(does_it_contain(&s, "fr", vec!["fr", "tears.html"])); + assert!(does_it_contain(&s, "hu", vec!["hu", "tears.html"])); chapter_path = book_path.join("long-tale.html"); s = utils::fs::file_to_string(&chapter_path).unwrap(); - assert!(s.contains("en")); - assert!(s.contains("fr")); - assert!(s.contains("hu")); + + assert!(does_it_contain(&s, "en", vec!["en", "long-tale.html"])); + assert!(does_it_contain(&s, "fr", vec!["fr", "cocasse.html"])); + assert!(does_it_contain(&s, "hu", vec!["hu", "tarka-farka.html"])); // Test if print.html is produced for each translations diff --git a/src/tests/hbs_renderer_test.rs b/src/tests/hbs_renderer_test.rs index ea3105ce..0a22da1d 100644 --- a/src/tests/hbs_renderer_test.rs +++ b/src/tests/hbs_renderer_test.rs @@ -54,14 +54,11 @@ fn it_copies_local_assets_when_found() { let path = PathBuf::from(".").join("src").join("tests").join("book-minimal-with-assets"); let renderer = HtmlHandlebars::new(); - if let Err(e) = renderer.build(&path, &None) { - println!("{:#?}", e); - } - - let mut proj = MDBook::new(&path); - proj.read_config(); - proj.parse_books(); + let proj = match renderer.build(&path, &None) { + Ok(x) => x, + Err(e) => { panic!("{:#?}", e); }, + }; let book_path: &Path = proj.translations.get("en").unwrap().config.get_dest(); @@ -72,6 +69,8 @@ fn it_copies_local_assets_when_found() { assert!(s.contains("The Library of Babel")); assert_eq!(book_path.join("css").join("book.css").exists(), true); - assert_eq!(book_path.join("css").join("highlight.css").exists(), false); + + // we left this out from the local assets for testing + assert_eq!(book_path.join("css").join("font-awesome.min.css").exists(), false); } diff --git a/src/tests/summary_test.rs b/src/tests/summary_test.rs index 16d49eb2..0911f5ac 100644 --- a/src/tests/summary_test.rs +++ b/src/tests/summary_test.rs @@ -1,5 +1,8 @@ #![cfg(test)] +use std::path::PathBuf; + +use book::toc::TocItem; use parse::summary::parse_level; #[test] @@ -29,494 +32,36 @@ fn it_parses_summary_to_tocitems() { [Contributors](misc/contributors.md) "#; - let result = parse_level(&mut text.split('\n').collect(), 0, vec![0], true).unwrap(); + let result: Vec = parse_level(&mut text.split('\n').collect(), 0, vec![0], true).unwrap(); - let expected = r#"[ - Unnumbered( - TocContent { - chapter: Chapter { - title: "Introduction", - content: None, - src_path: Some( - "misc/introduction.md" - ), - dest_path: Some( - "index.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: None - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "mdBook", - content: None, - src_path: Some( - "README.md" - ), - dest_path: Some( - "README.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 1 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "Command Line Tool", - content: None, - src_path: Some( - "cli/cli-tool.md" - ), - dest_path: Some( - "cli/cli-tool.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: Some( - [ - Numbered( - TocContent { - chapter: Chapter { - title: "init", - content: None, - src_path: Some( - "cli/init.md" - ), - dest_path: Some( - "cli/init.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 2, - 1 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "build", - content: None, - src_path: Some( - "cli/build.md" - ), - dest_path: Some( - "cli/build.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 2, - 2 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "watch", - content: None, - src_path: Some( - "cli/watch.md" - ), - dest_path: Some( - "cli/watch.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 2, - 3 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "serve", - content: None, - src_path: Some( - "cli/serve.md" - ), - dest_path: Some( - "cli/serve.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 2, - 4 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "test", - content: None, - src_path: Some( - "cli/test.md" - ), - dest_path: Some( - "cli/test.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 2, - 5 - ] - ) - } - ) - ] - ), - section: Some( - [ - 2 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "Format", - content: None, - src_path: Some( - "format/format.md" - ), - dest_path: Some( - "format/format.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: Some( - [ - Numbered( - TocContent { - chapter: Chapter { - title: "SUMMARY.md", - content: None, - src_path: Some( - "format/summary.md" - ), - dest_path: Some( - "format/summary.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 3, - 1 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "Configuration", - content: None, - src_path: Some( - "format/config.md" - ), - dest_path: Some( - "format/config.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 3, - 2 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "Theme", - content: None, - src_path: Some( - "format/theme/theme.md" - ), - dest_path: Some( - "format/theme/theme.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: Some( - [ - Numbered( - TocContent { - chapter: Chapter { - title: "index.hbs", - content: None, - src_path: Some( - "format/theme/index-hbs.md" - ), - dest_path: Some( - "format/theme/index-hbs.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 3, - 3, - 1 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "Syntax highlighting", - content: None, - src_path: Some( - "format/theme/syntax-highlighting.md" - ), - dest_path: Some( - "format/theme/syntax-highlighting.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 3, - 3, - 2 - ] - ) - } - ) - ] - ), - section: Some( - [ - 3, - 3 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "MathJax Support", - content: None, - src_path: Some( - "format/mathjax.md" - ), - dest_path: Some( - "format/mathjax.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 3, - 4 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "Rust code specific features", - content: None, - src_path: Some( - "format/rust.md" - ), - dest_path: Some( - "format/rust.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 3, - 5 - ] - ) - } - ) - ] - ), - section: Some( - [ - 3 - ] - ) - } - ), - Numbered( - TocContent { - chapter: Chapter { - title: "Rust Library", - content: None, - src_path: Some( - "lib/lib.md" - ), - dest_path: Some( - "lib/lib.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: Some( - [ - 4 - ] - ) - } - ), - Spacer, - Unnumbered( - TocContent { - chapter: Chapter { - title: "Contributors", - content: None, - src_path: Some( - "misc/contributors.md" - ), - dest_path: Some( - "misc/contributors.html" - ), - translation_links: None, - translation_id: None, - authors: None, - translators: None, - description: None, - css_class: None - }, - sub_items: None, - section: None - } - ) -]"#; + assert_eq!(result.iter().count(), 7); - assert_eq!(expected, format!("{:#?}", result)); + { + let a = match result[0].clone() { + TocItem::Unnumbered(x) => x, + _ => { panic!("{:#?}", result[0]); }, + }; + + assert_eq!(a.chapter.title, "Introduction".to_string()); + assert_eq!(a.chapter.get_src_path().unwrap().as_os_str(), + PathBuf::from("misc").join("introduction.md").as_os_str()); + } + + { + let a = match result[2].clone() { + TocItem::Numbered(x) => x, + _ => { panic!("{:#?}", result[0]); }, + }; + + assert_eq!(a.chapter.title, "Command Line Tool".to_string()); + assert_eq!(a.chapter.get_src_path().unwrap().as_os_str(), + PathBuf::from("cli").join("cli-tool.md").as_os_str()); + + let b = match a.sub_items { + Some(x) => x, + None => { panic!("No sub items! {:#?}", a); } + }; + + assert_eq!(b.iter().count(), 5); + } }