2017-03-14 12:25:45 +00:00
|
|
|
extern crate gutenberg;
|
|
|
|
extern crate tera;
|
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
2017-05-01 05:55:42 +00:00
|
|
|
use gutenberg::{FrontMatter, split_content, SortBy};
|
2017-03-14 12:25:45 +00:00
|
|
|
use tera::to_value;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_parse_a_valid_front_matter() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there""#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
println!("{:?}", res);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let res = res.unwrap();
|
2017-04-28 07:31:11 +00:00
|
|
|
assert_eq!(res.title.unwrap(), "Hello".to_string());
|
|
|
|
assert_eq!(res.description.unwrap(), "hey there".to_string());
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_parse_tags() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
tags = ["rust", "html"]"#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let res = res.unwrap();
|
|
|
|
|
2017-04-28 07:31:11 +00:00
|
|
|
assert_eq!(res.title.unwrap(), "Hello".to_string());
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(res.slug.unwrap(), "hello-world".to_string());
|
|
|
|
assert_eq!(res.tags.unwrap(), ["rust".to_string(), "html".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_parse_extra_attributes_in_frontmatter() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
|
|
|
|
[extra]
|
|
|
|
language = "en"
|
|
|
|
authors = ["Bob", "Alice"]"#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let res = res.unwrap();
|
|
|
|
|
2017-04-28 07:31:11 +00:00
|
|
|
assert_eq!(res.title.unwrap(), "Hello".to_string());
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(res.slug.unwrap(), "hello-world".to_string());
|
|
|
|
let extra = res.extra.unwrap();
|
2017-04-22 03:35:11 +00:00
|
|
|
assert_eq!(extra["language"], to_value("en").unwrap());
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(
|
2017-04-22 03:35:11 +00:00
|
|
|
extra["authors"],
|
|
|
|
to_value(["Bob".to_string(), "Alice".to_string()]).unwrap()
|
2017-03-14 12:25:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_ok_with_url_instead_of_slug() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
url = "hello-world""#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let res = res.unwrap();
|
|
|
|
assert!(res.slug.is_none());
|
|
|
|
assert_eq!(res.url.unwrap(), "hello-world".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-05-08 10:29:37 +00:00
|
|
|
fn test_is_ok_with_empty_front_matter() {
|
2017-03-14 12:25:45 +00:00
|
|
|
let content = r#" "#;
|
|
|
|
let res = FrontMatter::parse(content);
|
2017-05-08 10:29:37 +00:00
|
|
|
assert!(res.is_ok());
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_errors_with_invalid_front_matter() {
|
|
|
|
let content = r#"title = 1\n"#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_errors_on_non_string_tag() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
tags = ["rust", 1]"#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_errors_on_present_but_empty_slug() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = """#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_errors_on_present_but_empty_url() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
url = """#;
|
|
|
|
let res = FrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_date_yyyy_mm_dd() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2016-10-10""#;
|
|
|
|
let res = FrontMatter::parse(content).unwrap();
|
2017-04-24 09:11:51 +00:00
|
|
|
assert!(res.date().is_some());
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_date_rfc3339() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2002-10-02T15:00:00Z""#;
|
|
|
|
let res = FrontMatter::parse(content).unwrap();
|
2017-04-24 09:11:51 +00:00
|
|
|
assert!(res.date().is_some());
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cant_parse_random_date_format() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2002/10/12""#;
|
|
|
|
let res = FrontMatter::parse(content).unwrap();
|
2017-04-24 09:11:51 +00:00
|
|
|
assert!(res.date().is_none());
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 09:11:51 +00:00
|
|
|
#[test]
|
|
|
|
fn test_cant_parse_sort_by_date() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
sort_by = "date""#;
|
|
|
|
let res = FrontMatter::parse(content).unwrap();
|
|
|
|
assert!(res.sort_by.is_some());
|
2017-05-01 05:55:42 +00:00
|
|
|
assert_eq!(res.sort_by.unwrap(), SortBy::Date);
|
2017-04-24 09:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cant_parse_sort_by_order() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
sort_by = "order""#;
|
|
|
|
let res = FrontMatter::parse(content).unwrap();
|
|
|
|
assert!(res.sort_by.is_some());
|
2017-05-01 05:55:42 +00:00
|
|
|
assert_eq!(res.sort_by.unwrap(), SortBy::Order);
|
2017-04-24 09:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cant_parse_sort_by_none() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
sort_by = "none""#;
|
|
|
|
let res = FrontMatter::parse(content).unwrap();
|
|
|
|
assert!(res.sort_by.is_some());
|
2017-05-01 05:55:42 +00:00
|
|
|
assert_eq!(res.sort_by.unwrap(), SortBy::None);
|
2017-04-24 09:11:51 +00:00
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_split_content_valid() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Title"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2002/10/12"
|
|
|
|
+++
|
|
|
|
Hello
|
|
|
|
"#;
|
|
|
|
let (front_matter, content) = split_content(Path::new(""), content).unwrap();
|
|
|
|
assert_eq!(content, "Hello\n");
|
2017-04-28 07:31:11 +00:00
|
|
|
assert_eq!(front_matter.title.unwrap(), "Title");
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_split_content_with_only_frontmatter_valid() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Title"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2002/10/12"
|
|
|
|
+++"#;
|
|
|
|
let (front_matter, content) = split_content(Path::new(""), content).unwrap();
|
|
|
|
assert_eq!(content, "");
|
2017-04-28 07:31:11 +00:00
|
|
|
assert_eq!(front_matter.title.unwrap(), "Title");
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 03:01:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_can_split_content_lazily() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Title"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2002-10-02T15:00:00Z"
|
|
|
|
+++
|
|
|
|
+++"#;
|
|
|
|
let (front_matter, content) = split_content(Path::new(""), content).unwrap();
|
|
|
|
assert_eq!(content, "+++");
|
2017-04-28 07:31:11 +00:00
|
|
|
assert_eq!(front_matter.title.unwrap(), "Title");
|
2017-03-27 03:01:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
#[test]
|
|
|
|
fn test_error_if_cannot_locate_frontmatter() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Title"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2002/10/12"
|
|
|
|
"#;
|
|
|
|
let res = split_content(Path::new(""), content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|