From cee3296a32fba93b1761b053445fadbb62fbf7e0 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sat, 30 Sep 2017 21:54:25 +0800 Subject: [PATCH] main library tests pass --- src/book/mod.rs | 2 +- src/config/mod.rs | 15 +- src/renderer/html_handlebars/hbs_renderer.rs | 1 - tests/jsonconfig.rs | 87 -------- tests/tomlconfig.rs | 209 ------------------- 5 files changed, 14 insertions(+), 300 deletions(-) delete mode 100644 tests/jsonconfig.rs delete mode 100644 tests/tomlconfig.rs diff --git a/src/book/mod.rs b/src/book/mod.rs index edd268dc..8d184e22 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -4,7 +4,7 @@ pub use self::bookitem::{BookItem, BookItems}; use std::path::{Path, PathBuf}; use std::fs::{self, File}; -use std::io::{Read, Write}; +use std::io::Write; use std::process::Command; use tempdir::TempDir; diff --git a/src/config/mod.rs b/src/config/mod.rs index 6d5bb97f..ceff068f 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -159,12 +159,17 @@ mod tests { build-dir = "outputs" [output.html] + theme = "./themedir" curly-quotes = true google-analytics = "123456" additional-css = ["./foo/bar/baz.css"] + + [output.html.playpen] + editable = true + editor = "ace" "#; - let book_should_be = BookConfig_ { + let book_should_be = BookConfig { title: Some(String::from("Some Book")), authors: vec![String::from("Michael-F-Bryan ")], description: Some(String::from("A completely useless book")), @@ -173,10 +178,16 @@ mod tests { build_dir: PathBuf::from("outputs"), ..Default::default() }; - let html_should_be = HtmlConfig_ { + let playpen_should_be = Playpen { + editable: true, + editor: PathBuf::from("ace"), + }; + let html_should_be = HtmlConfig { curly_quotes: true, google_analytics: Some(String::from("123456")), additional_css: vec![PathBuf::from("./foo/bar/baz.css")], + theme: Some(PathBuf::from("./themedir")), + playpen: playpen_should_be, ..Default::default() }; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 55be3122..48bf07e5 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -536,7 +536,6 @@ fn fix_code_blocks(html: &str) -> String { fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String { let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap(); -<<<<<<< HEAD regex.replace_all(html, |caps: &Captures| { let text = &caps[1]; let classes = &caps[2]; diff --git a/tests/jsonconfig.rs b/tests/jsonconfig.rs deleted file mode 100644 index c43d59ce..00000000 --- a/tests/jsonconfig.rs +++ /dev/null @@ -1,87 +0,0 @@ -extern crate mdbook; -use mdbook::config::BookConfig; -use mdbook::config::jsonconfig::JsonConfig; - -use std::path::PathBuf; - -// Tests that the `src` key is correctly parsed in the JSON config -#[test] -fn from_json_source() { - let json = r#"{ - "src": "source" - }"#; - - let parsed = JsonConfig::from_json(json).expect("This should parse"); - let config = BookConfig::from_jsonconfig("root", parsed); - - assert_eq!(config.get_source(), PathBuf::from("root/source")); -} - -// Tests that the `title` key is correctly parsed in the JSON config -#[test] -fn from_json_title() { - let json = r#"{ - "title": "Some title" - }"#; - - let parsed = JsonConfig::from_json(json).expect("This should parse"); - let config = BookConfig::from_jsonconfig("root", parsed); - - assert_eq!(config.get_title(), "Some title"); -} - -// Tests that the `description` key is correctly parsed in the JSON config -#[test] -fn from_json_description() { - let json = r#"{ - "description": "This is a description" - }"#; - - let parsed = JsonConfig::from_json(json).expect("This should parse"); - let config = BookConfig::from_jsonconfig("root", parsed); - - assert_eq!(config.get_description(), "This is a description"); -} - -// Tests that the `author` key is correctly parsed in the JSON config -#[test] -fn from_json_author() { - let json = r#"{ - "author": "John Doe" - }"#; - - let parsed = JsonConfig::from_json(json).expect("This should parse"); - let config = BookConfig::from_jsonconfig("root", parsed); - - assert_eq!(config.get_authors(), &[String::from("John Doe")]); -} - -// Tests that the `dest` key is correctly parsed in the JSON config -#[test] -fn from_json_destination() { - let json = r#"{ - "dest": "htmlbook" - }"#; - - let parsed = JsonConfig::from_json(json).expect("This should parse"); - let config = BookConfig::from_jsonconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_destination(), PathBuf::from("root/htmlbook")); -} - -// Tests that the `theme_path` key is correctly parsed in the JSON config -#[test] -fn from_json_output_html_theme() { - let json = r#"{ - "theme_path": "theme" - }"#; - - let parsed = JsonConfig::from_json(json).expect("This should parse"); - let config = BookConfig::from_jsonconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_theme(), &PathBuf::from("root/theme")); -} diff --git a/tests/tomlconfig.rs b/tests/tomlconfig.rs deleted file mode 100644 index b6c02e57..00000000 --- a/tests/tomlconfig.rs +++ /dev/null @@ -1,209 +0,0 @@ -extern crate mdbook; -use mdbook::config::BookConfig; -use mdbook::config::tomlconfig::TomlConfig; - -use std::path::PathBuf; - -// Tests that the `source` key is correctly parsed in the TOML config -#[test] -fn from_toml_source() { - let toml = r#"source = "source""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - assert_eq!(config.get_source(), PathBuf::from("root/source")); -} - -// Tests that the `title` key is correctly parsed in the TOML config -#[test] -fn from_toml_title() { - let toml = r#"title = "Some title""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - assert_eq!(config.get_title(), "Some title"); -} - -// Tests that the `description` key is correctly parsed in the TOML config -#[test] -fn from_toml_description() { - let toml = r#"description = "This is a description""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - assert_eq!(config.get_description(), "This is a description"); -} - -// Tests that the `author` key is correctly parsed in the TOML config -#[test] -fn from_toml_author() { - let toml = r#"author = "John Doe""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - assert_eq!(config.get_authors(), &[String::from("John Doe")]); -} - -// Tests that the `authors` key is correctly parsed in the TOML config -#[test] -fn from_toml_authors() { - let toml = r#"authors = ["John Doe", "Jane Doe"]"#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - assert_eq!(config.get_authors(), - &[String::from("John Doe"), String::from("Jane Doe")]); -} - -// Tests that the default `playpen` config is correct in the TOML config -#[test] -fn from_toml_playpen_default() { - let toml = ""; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let playpenconfig = config.get_html_config().get_playpen_config(); - - assert_eq!(playpenconfig.get_editor(), - PathBuf::from("root/theme/editor")); - assert_eq!(playpenconfig.is_editable(), false); -} - -// Tests that the `playpen.editor` key is correctly parsed in the TOML config -#[test] -fn from_toml_playpen_editor() { - let toml = r#"[output.html.playpen] - editor = "editordir""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let playpenconfig = config.get_html_config().get_playpen_config(); - - assert_eq!(playpenconfig.get_editor(), - PathBuf::from("root/theme/editordir")); -} - -// Tests that the `playpen.editable` key is correctly parsed in the TOML config -#[test] -fn from_toml_playpen_editable() { - let toml = r#"[output.html.playpen] - editable = true"#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let playpenconfig = config.get_html_config().get_playpen_config(); - - assert_eq!(playpenconfig.is_editable(), true); -} - -// Tests that the `output.html.destination` key is correcly parsed in the TOML config -#[test] -fn from_toml_output_html_destination() { - let toml = r#"[output.html] - destination = "htmlbook""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_destination(), PathBuf::from("root/htmlbook")); -} - -// Tests that the `output.html.theme` key is correctly parsed in the TOML config -#[test] -fn from_toml_output_html_theme() { - let toml = r#"[output.html] - theme = "theme""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_theme(), &PathBuf::from("root/theme")); -} - -// Tests that the `output.html.curly-quotes` key is correctly parsed in the TOML config -#[test] -fn from_toml_output_html_curly_quotes() { - let toml = r#"[output.html] - curly-quotes = true"#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_curly_quotes(), true); -} - -// Tests that the `output.html.mathjax-support` key is correctly parsed in the TOML config -#[test] -fn from_toml_output_html_mathjax_support() { - let toml = r#"[output.html] - mathjax-support = true"#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_mathjax_support(), true); -} - -// Tests that the `output.html.google-analytics` key is correctly parsed in the TOML config -#[test] -fn from_toml_output_html_google_analytics() { - let toml = r#"[output.html] - google-analytics = "123456""#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_google_analytics_id() - .expect("the google-analytics key was provided"), - String::from("123456")); -} - -// Tests that the `output.html.additional-css` key is correctly parsed in the TOML config -#[test] -fn from_toml_output_html_additional_stylesheet() { - let toml = r#"[output.html] - additional-css = ["custom.css", "two/custom.css"]"#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_additional_css(), - &[PathBuf::from("root/custom.css"), - PathBuf::from("root/two/custom.css")]); -} - -// Tests that the `output.html.additional-js` key is correctly parsed in the TOML config -#[test] -fn from_toml_output_html_additional_scripts() { - let toml = r#"[output.html] - additional-js = ["custom.js", "two/custom.js"]"#; - - let parsed = TomlConfig::from_toml(toml).expect("This should parse"); - let config = BookConfig::from_tomlconfig("root", parsed); - - let htmlconfig = config.get_html_config(); - - assert_eq!(htmlconfig.get_additional_js(), - &[PathBuf::from("root/custom.js"), - PathBuf::from("root/two/custom.js")]); -}