You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.5KB

  1. extern crate gutenberg;
  2. extern crate tera;
  3. use std::path::Path;
  4. use gutenberg::{FrontMatter, split_content};
  5. use tera::to_value;
  6. #[test]
  7. fn test_can_parse_a_valid_front_matter() {
  8. let content = r#"
  9. title = "Hello"
  10. description = "hey there""#;
  11. let res = FrontMatter::parse(content);
  12. println!("{:?}", res);
  13. assert!(res.is_ok());
  14. let res = res.unwrap();
  15. assert_eq!(res.title, "Hello".to_string());
  16. assert_eq!(res.description, "hey there".to_string());
  17. }
  18. #[test]
  19. fn test_can_parse_tags() {
  20. let content = r#"
  21. title = "Hello"
  22. description = "hey there"
  23. slug = "hello-world"
  24. tags = ["rust", "html"]"#;
  25. let res = FrontMatter::parse(content);
  26. assert!(res.is_ok());
  27. let res = res.unwrap();
  28. assert_eq!(res.title, "Hello".to_string());
  29. assert_eq!(res.slug.unwrap(), "hello-world".to_string());
  30. assert_eq!(res.tags.unwrap(), ["rust".to_string(), "html".to_string()]);
  31. }
  32. #[test]
  33. fn test_can_parse_extra_attributes_in_frontmatter() {
  34. let content = r#"
  35. title = "Hello"
  36. description = "hey there"
  37. slug = "hello-world"
  38. [extra]
  39. language = "en"
  40. authors = ["Bob", "Alice"]"#;
  41. let res = FrontMatter::parse(content);
  42. assert!(res.is_ok());
  43. let res = res.unwrap();
  44. assert_eq!(res.title, "Hello".to_string());
  45. assert_eq!(res.slug.unwrap(), "hello-world".to_string());
  46. let extra = res.extra.unwrap();
  47. assert_eq!(extra.get("language").unwrap(), &to_value("en").unwrap());
  48. assert_eq!(
  49. extra.get("authors").unwrap(),
  50. &to_value(["Bob".to_string(), "Alice".to_string()]).unwrap()
  51. );
  52. }
  53. #[test]
  54. fn test_is_ok_with_url_instead_of_slug() {
  55. let content = r#"
  56. title = "Hello"
  57. description = "hey there"
  58. url = "hello-world""#;
  59. let res = FrontMatter::parse(content);
  60. assert!(res.is_ok());
  61. let res = res.unwrap();
  62. assert!(res.slug.is_none());
  63. assert_eq!(res.url.unwrap(), "hello-world".to_string());
  64. }
  65. #[test]
  66. fn test_errors_with_empty_front_matter() {
  67. let content = r#" "#;
  68. let res = FrontMatter::parse(content);
  69. assert!(res.is_err());
  70. }
  71. #[test]
  72. fn test_errors_with_invalid_front_matter() {
  73. let content = r#"title = 1\n"#;
  74. let res = FrontMatter::parse(content);
  75. assert!(res.is_err());
  76. }
  77. #[test]
  78. fn test_errors_with_missing_required_value_front_matter() {
  79. let content = r#"title = """#;
  80. let res = FrontMatter::parse(content);
  81. assert!(res.is_err());
  82. }
  83. #[test]
  84. fn test_errors_on_non_string_tag() {
  85. let content = r#"
  86. title = "Hello"
  87. description = "hey there"
  88. slug = "hello-world"
  89. tags = ["rust", 1]"#;
  90. let res = FrontMatter::parse(content);
  91. assert!(res.is_err());
  92. }
  93. #[test]
  94. fn test_errors_on_present_but_empty_slug() {
  95. let content = r#"
  96. title = "Hello"
  97. description = "hey there"
  98. slug = """#;
  99. let res = FrontMatter::parse(content);
  100. assert!(res.is_err());
  101. }
  102. #[test]
  103. fn test_errors_on_present_but_empty_url() {
  104. let content = r#"
  105. title = "Hello"
  106. description = "hey there"
  107. url = """#;
  108. let res = FrontMatter::parse(content);
  109. assert!(res.is_err());
  110. }
  111. #[test]
  112. fn test_parse_date_yyyy_mm_dd() {
  113. let content = r#"
  114. title = "Hello"
  115. description = "hey there"
  116. date = "2016-10-10""#;
  117. let res = FrontMatter::parse(content).unwrap();
  118. assert!(res.parse_date().is_some());
  119. }
  120. #[test]
  121. fn test_parse_date_rfc3339() {
  122. let content = r#"
  123. title = "Hello"
  124. description = "hey there"
  125. date = "2002-10-02T15:00:00Z""#;
  126. let res = FrontMatter::parse(content).unwrap();
  127. assert!(res.parse_date().is_some());
  128. }
  129. #[test]
  130. fn test_cant_parse_random_date_format() {
  131. let content = r#"
  132. title = "Hello"
  133. description = "hey there"
  134. date = "2002/10/12""#;
  135. let res = FrontMatter::parse(content).unwrap();
  136. assert!(res.parse_date().is_none());
  137. }
  138. #[test]
  139. fn test_can_split_content_valid() {
  140. let content = r#"
  141. +++
  142. title = "Title"
  143. description = "hey there"
  144. date = "2002/10/12"
  145. +++
  146. Hello
  147. "#;
  148. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  149. assert_eq!(content, "Hello\n");
  150. assert_eq!(front_matter.title, "Title");
  151. }
  152. #[test]
  153. fn test_can_split_content_with_only_frontmatter_valid() {
  154. let content = r#"
  155. +++
  156. title = "Title"
  157. description = "hey there"
  158. date = "2002/10/12"
  159. +++"#;
  160. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  161. assert_eq!(content, "");
  162. assert_eq!(front_matter.title, "Title");
  163. }
  164. #[test]
  165. fn test_error_if_cannot_locate_frontmatter() {
  166. let content = r#"
  167. +++
  168. title = "Title"
  169. description = "hey there"
  170. date = "2002/10/12"
  171. "#;
  172. let res = split_content(Path::new(""), content);
  173. assert!(res.is_err());
  174. }