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.

237 lines
5.4KB

  1. extern crate gutenberg;
  2. extern crate tera;
  3. use std::path::Path;
  4. use gutenberg::{FrontMatter, split_content, SortBy};
  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_is_ok_with_empty_front_matter() {
  67. let content = r#" "#;
  68. let res = FrontMatter::parse(content);
  69. assert!(res.is_ok());
  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.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.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.date().is_none());
  131. }
  132. #[test]
  133. fn test_cant_parse_sort_by_date() {
  134. let content = r#"
  135. title = "Hello"
  136. description = "hey there"
  137. sort_by = "date""#;
  138. let res = FrontMatter::parse(content).unwrap();
  139. assert!(res.sort_by.is_some());
  140. assert_eq!(res.sort_by.unwrap(), SortBy::Date);
  141. }
  142. #[test]
  143. fn test_cant_parse_sort_by_order() {
  144. let content = r#"
  145. title = "Hello"
  146. description = "hey there"
  147. sort_by = "order""#;
  148. let res = FrontMatter::parse(content).unwrap();
  149. assert!(res.sort_by.is_some());
  150. assert_eq!(res.sort_by.unwrap(), SortBy::Order);
  151. }
  152. #[test]
  153. fn test_cant_parse_sort_by_none() {
  154. let content = r#"
  155. title = "Hello"
  156. description = "hey there"
  157. sort_by = "none""#;
  158. let res = FrontMatter::parse(content).unwrap();
  159. assert!(res.sort_by.is_some());
  160. assert_eq!(res.sort_by.unwrap(), SortBy::None);
  161. }
  162. #[test]
  163. fn test_can_split_content_valid() {
  164. let content = r#"
  165. +++
  166. title = "Title"
  167. description = "hey there"
  168. date = "2002/10/12"
  169. +++
  170. Hello
  171. "#;
  172. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  173. assert_eq!(content, "Hello\n");
  174. assert_eq!(front_matter.title.unwrap(), "Title");
  175. }
  176. #[test]
  177. fn test_can_split_content_with_only_frontmatter_valid() {
  178. let content = r#"
  179. +++
  180. title = "Title"
  181. description = "hey there"
  182. date = "2002/10/12"
  183. +++"#;
  184. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  185. assert_eq!(content, "");
  186. assert_eq!(front_matter.title.unwrap(), "Title");
  187. }
  188. #[test]
  189. fn test_can_split_content_lazily() {
  190. let content = r#"
  191. +++
  192. title = "Title"
  193. description = "hey there"
  194. date = "2002-10-02T15:00:00Z"
  195. +++
  196. +++"#;
  197. let (front_matter, content) = split_content(Path::new(""), content).unwrap();
  198. assert_eq!(content, "+++");
  199. assert_eq!(front_matter.title.unwrap(), "Title");
  200. }
  201. #[test]
  202. fn test_error_if_cannot_locate_frontmatter() {
  203. let content = r#"
  204. +++
  205. title = "Title"
  206. description = "hey there"
  207. date = "2002/10/12"
  208. "#;
  209. let res = split_content(Path::new(""), content);
  210. assert!(res.is_err());
  211. }