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.

205 lines
4.6KB

  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.unwrap(), "Hello".to_string());
  16. assert_eq!(res.description.unwrap(), "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.unwrap(), "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.unwrap(), "Hello".to_string());
  45. assert_eq!(res.slug.unwrap(), "hello-world".to_string());
  46. let extra = res.extra.unwrap();
  47. assert_eq!(extra["language"], to_value("en").unwrap());
  48. assert_eq!(
  49. extra["authors"],
  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_on_non_string_tag() {
  79. let content = r#"
  80. title = "Hello"
  81. description = "hey there"
  82. slug = "hello-world"
  83. tags = ["rust", 1]"#;
  84. let res = FrontMatter::parse(content);
  85. assert!(res.is_err());
  86. }
  87. #[test]
  88. fn test_errors_on_present_but_empty_slug() {
  89. let content = r#"
  90. title = "Hello"
  91. description = "hey there"
  92. slug = """#;
  93. let res = FrontMatter::parse(content);
  94. assert!(res.is_err());
  95. }
  96. #[test]
  97. fn test_errors_on_present_but_empty_url() {
  98. let content = r#"
  99. title = "Hello"
  100. description = "hey there"
  101. url = """#;
  102. let res = FrontMatter::parse(content);
  103. assert!(res.is_err());
  104. }
  105. #[test]
  106. fn test_parse_date_yyyy_mm_dd() {
  107. let content = r#"
  108. title = "Hello"
  109. description = "hey there"
  110. date = "2016-10-10""#;
  111. let res = FrontMatter::parse(content).unwrap();
  112. assert!(res.parse_date().is_some());
  113. }
  114. #[test]
  115. fn test_parse_date_rfc3339() {
  116. let content = r#"
  117. title = "Hello"
  118. description = "hey there"
  119. date = "2002-10-02T15:00:00Z""#;
  120. let res = FrontMatter::parse(content).unwrap();
  121. assert!(res.parse_date().is_some());
  122. }
  123. #[test]
  124. fn test_cant_parse_random_date_format() {
  125. let content = r#"
  126. title = "Hello"
  127. description = "hey there"
  128. date = "2002/10/12""#;
  129. let res = FrontMatter::parse(content).unwrap();
  130. assert!(res.parse_date().is_none());
  131. }
  132. #[test]
  133. fn test_can_split_content_valid() {
  134. let content = r#"
  135. +++
  136. title = "Title"
  137. description = "hey there"
  138. date = "2002/10/12"
  139. +++
  140. Hello
  141. "#;
  142. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  143. assert_eq!(content, "Hello\n");
  144. assert_eq!(front_matter.title.unwrap(), "Title");
  145. }
  146. #[test]
  147. fn test_can_split_content_with_only_frontmatter_valid() {
  148. let content = r#"
  149. +++
  150. title = "Title"
  151. description = "hey there"
  152. date = "2002/10/12"
  153. +++"#;
  154. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  155. assert_eq!(content, "");
  156. assert_eq!(front_matter.title.unwrap(), "Title");
  157. }
  158. #[test]
  159. fn test_can_split_content_lazily() {
  160. let content = r#"
  161. +++
  162. title = "Title"
  163. description = "hey there"
  164. date = "2002-10-02T15:00:00Z"
  165. +++
  166. +++"#;
  167. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  168. assert_eq!(content, "+++");
  169. assert_eq!(front_matter.title.unwrap(), "Title");
  170. }
  171. #[test]
  172. fn test_error_if_cannot_locate_frontmatter() {
  173. let content = r#"
  174. +++
  175. title = "Title"
  176. description = "hey there"
  177. date = "2002/10/12"
  178. "#;
  179. let res = split_content(Path::new(""), content);
  180. assert!(res.is_err());
  181. }