extern crate gutenberg; extern crate tera; extern crate tempdir; use std::collections::HashMap; use std::fs::{File, create_dir}; use std::path::Path; use tempdir::TempDir; use tera::Tera; use gutenberg::{Page, Config}; #[test] fn test_can_parse_a_valid_page() { let content = r#" +++ title = "Hello" description = "hey there" slug = "hello-world" +++ Hello world"#; let res = Page::parse(Path::new("post.md"), content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.meta.title.unwrap(), "Hello".to_string()); assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string()); assert_eq!(page.raw_content, "Hello world".to_string()); assert_eq!(page.content, "
Hello world
\n".to_string()); } #[test] fn test_can_find_one_parent_directory() { let content = r#" +++ title = "Hello" description = "hey there" slug = "hello-world" +++ Hello world"#; let res = Page::parse(Path::new("content/posts/intro.md"), content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.components, vec!["posts".to_string()]); } #[test] fn test_can_find_multiple_parent_directories() { let content = r#" +++ title = "Hello" description = "hey there" slug = "hello-world" +++ Hello world"#; let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.components, vec!["posts".to_string(), "intro".to_string()]); } #[test] fn test_can_make_url_from_sections_and_slug() { let content = r#" +++ title = "Hello" description = "hey there" slug = "hello-world" +++ Hello world"#; let mut conf = Config::default(); conf.base_url = "http://hello.com/".to_string(); let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &conf); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.path, "posts/intro/hello-world"); assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world"); } #[test] fn test_can_make_permalink_with_non_trailing_slash_base_url() { let content = r#" +++ title = "Hello" description = "hey there" slug = "hello-world" +++ Hello world"#; let mut conf = Config::default(); conf.base_url = "http://hello.com".to_string(); let res = Page::parse(Path::new("content/posts/intro/hello-world.md"), content, &conf); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.path, "posts/intro/hello-world"); assert_eq!(page.permalink, format!("{}{}", conf.base_url, "/posts/intro/hello-world")); } #[test] fn test_can_make_url_from_slug_only() { let content = r#" +++ title = "Hello" description = "hey there" slug = "hello-world" +++ Hello world"#; let res = Page::parse(Path::new("start.md"), content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.path, "hello-world"); assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "hello-world")); } #[test] fn test_errors_on_invalid_front_matter_format() { let content = r#" title = "Hello" description = "hey there" slug = "hello-world" +++ Hello world"#; let res = Page::parse(Path::new("start.md"), content, &Config::default()); assert!(res.is_err()); } #[test] fn test_can_make_slug_from_non_slug_filename() { let content = r#" +++ title = "Hello" description = "hey there" +++ Hello world"#; let res = Page::parse(Path::new("file with space.md"), content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.slug, "file-with-space"); assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space")); } #[test] fn test_trim_slug_if_needed() { let content = r#" +++ title = "Hello" description = "hey there" +++ Hello world"#; let res = Page::parse(Path::new(" file with space.md"), content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.slug, "file-with-space"); assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space")); } #[test] fn test_automatic_summary_is_empty_string() { let content = r#" +++ title = "Hello" description = "hey there" +++ Hello world"#.to_string(); let res = Page::parse(Path::new("hello.md"), &content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.summary, None); } #[test] fn test_can_specify_summary() { let content = r#" +++ title = "Hello" description = "hey there" +++ Hello world "#.to_string(); let res = Page::parse(Path::new("hello.md"), &content, &Config::default()); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert_eq!(page.summary, Some("Hello world
\n".to_string())); } #[test] fn test_can_auto_detect_when_highlighting_needed() { let content = r#" +++ title = "Hello" description = "hey there" +++ ``` Hey there ``` "#.to_string(); let mut config = Config::default(); config.highlight_code = Some(true); let res = Page::parse(Path::new("hello.md"), &content, &config); assert!(res.is_ok()); let mut page = res.unwrap(); page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert!(page.content.starts_with("