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.

266 lines
7.7KB

  1. extern crate gutenberg;
  2. extern crate tera;
  3. extern crate tempdir;
  4. use std::collections::HashMap;
  5. use std::fs::File;
  6. use std::path::Path;
  7. use tempdir::TempDir;
  8. use tera::Tera;
  9. use gutenberg::{Page, Config};
  10. #[test]
  11. fn test_can_parse_a_valid_page() {
  12. let content = r#"
  13. +++
  14. title = "Hello"
  15. description = "hey there"
  16. slug = "hello-world"
  17. +++
  18. Hello world"#;
  19. let res = Page::parse(Path::new("post.md"), content, &Config::default());
  20. assert!(res.is_ok());
  21. let mut page = res.unwrap();
  22. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  23. assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
  24. assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
  25. assert_eq!(page.raw_content, "Hello world".to_string());
  26. assert_eq!(page.content, "<p>Hello world</p>\n".to_string());
  27. }
  28. #[test]
  29. fn test_can_find_one_parent_directory() {
  30. let content = r#"
  31. +++
  32. title = "Hello"
  33. description = "hey there"
  34. slug = "hello-world"
  35. +++
  36. Hello world"#;
  37. let res = Page::parse(Path::new("content/posts/intro.md"), content, &Config::default());
  38. assert!(res.is_ok());
  39. let mut page = res.unwrap();
  40. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  41. assert_eq!(page.components, vec!["posts".to_string()]);
  42. }
  43. #[test]
  44. fn test_can_find_multiple_parent_directories() {
  45. let content = r#"
  46. +++
  47. title = "Hello"
  48. description = "hey there"
  49. slug = "hello-world"
  50. +++
  51. Hello world"#;
  52. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &Config::default());
  53. assert!(res.is_ok());
  54. let mut page = res.unwrap();
  55. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  56. assert_eq!(page.components, vec!["posts".to_string(), "intro".to_string()]);
  57. }
  58. #[test]
  59. fn test_can_make_url_from_sections_and_slug() {
  60. let content = r#"
  61. +++
  62. title = "Hello"
  63. description = "hey there"
  64. slug = "hello-world"
  65. +++
  66. Hello world"#;
  67. let mut conf = Config::default();
  68. conf.base_url = "http://hello.com/".to_string();
  69. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &conf);
  70. assert!(res.is_ok());
  71. let mut page = res.unwrap();
  72. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  73. assert_eq!(page.path, "posts/intro/hello-world");
  74. assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world");
  75. }
  76. #[test]
  77. fn test_can_make_permalink_with_non_trailing_slash_base_url() {
  78. let content = r#"
  79. +++
  80. title = "Hello"
  81. description = "hey there"
  82. slug = "hello-world"
  83. +++
  84. Hello world"#;
  85. let mut conf = Config::default();
  86. conf.base_url = "http://hello.com".to_string();
  87. let res = Page::parse(Path::new("content/posts/intro/hello-world.md"), content, &conf);
  88. assert!(res.is_ok());
  89. let mut page = res.unwrap();
  90. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  91. assert_eq!(page.path, "posts/intro/hello-world");
  92. assert_eq!(page.permalink, format!("{}{}", conf.base_url, "/posts/intro/hello-world"));
  93. }
  94. #[test]
  95. fn test_can_make_url_from_slug_only() {
  96. let content = r#"
  97. +++
  98. title = "Hello"
  99. description = "hey there"
  100. slug = "hello-world"
  101. +++
  102. Hello world"#;
  103. let res = Page::parse(Path::new("start.md"), content, &Config::default());
  104. assert!(res.is_ok());
  105. let mut page = res.unwrap();
  106. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  107. assert_eq!(page.path, "hello-world");
  108. assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "hello-world"));
  109. }
  110. #[test]
  111. fn test_errors_on_invalid_front_matter_format() {
  112. let content = r#"
  113. title = "Hello"
  114. description = "hey there"
  115. slug = "hello-world"
  116. +++
  117. Hello world"#;
  118. let res = Page::parse(Path::new("start.md"), content, &Config::default());
  119. assert!(res.is_err());
  120. }
  121. #[test]
  122. fn test_can_make_slug_from_non_slug_filename() {
  123. let content = r#"
  124. +++
  125. title = "Hello"
  126. description = "hey there"
  127. +++
  128. Hello world"#;
  129. let res = Page::parse(Path::new("file with space.md"), content, &Config::default());
  130. assert!(res.is_ok());
  131. let mut page = res.unwrap();
  132. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  133. assert_eq!(page.slug, "file-with-space");
  134. assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
  135. }
  136. #[test]
  137. fn test_trim_slug_if_needed() {
  138. let content = r#"
  139. +++
  140. title = "Hello"
  141. description = "hey there"
  142. +++
  143. Hello world"#;
  144. let res = Page::parse(Path::new(" file with space.md"), content, &Config::default());
  145. assert!(res.is_ok());
  146. let mut page = res.unwrap();
  147. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  148. assert_eq!(page.slug, "file-with-space");
  149. assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
  150. }
  151. #[test]
  152. fn test_reading_analytics_short() {
  153. let content = r#"
  154. +++
  155. title = "Hello"
  156. description = "hey there"
  157. +++
  158. Hello world"#;
  159. let res = Page::parse(Path::new("hello.md"), content, &Config::default());
  160. assert!(res.is_ok());
  161. let mut page = res.unwrap();
  162. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  163. let (word_count, reading_time) = page.get_reading_analytics();
  164. assert_eq!(word_count, 2);
  165. assert_eq!(reading_time, 0);
  166. }
  167. #[test]
  168. fn test_reading_analytics_long() {
  169. let mut content = r#"
  170. +++
  171. title = "Hello"
  172. description = "hey there"
  173. +++
  174. Hello world"#.to_string();
  175. for _ in 0..1000 {
  176. content.push_str(" Hello world");
  177. }
  178. let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
  179. assert!(res.is_ok());
  180. let mut page = res.unwrap();
  181. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  182. let (word_count, reading_time) = page.get_reading_analytics();
  183. assert_eq!(word_count, 2002);
  184. assert_eq!(reading_time, 10);
  185. }
  186. #[test]
  187. fn test_automatic_summary_is_empty_string() {
  188. let content = r#"
  189. +++
  190. title = "Hello"
  191. description = "hey there"
  192. +++
  193. Hello world"#.to_string();
  194. let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
  195. assert!(res.is_ok());
  196. let mut page = res.unwrap();
  197. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  198. assert_eq!(page.summary, None);
  199. }
  200. #[test]
  201. fn test_can_specify_summary() {
  202. let content = r#"
  203. +++
  204. title = "Hello"
  205. description = "hey there"
  206. +++
  207. Hello world
  208. <!-- more -->
  209. "#.to_string();
  210. let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
  211. assert!(res.is_ok());
  212. let mut page = res.unwrap();
  213. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  214. assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
  215. }
  216. #[test]
  217. fn test_can_auto_detect_when_highlighting_needed() {
  218. let content = r#"
  219. +++
  220. title = "Hello"
  221. description = "hey there"
  222. +++
  223. ```
  224. Hey there
  225. ```
  226. "#.to_string();
  227. let mut config = Config::default();
  228. config.highlight_code = Some(true);
  229. let res = Page::parse(Path::new("hello.md"), &content, &config);
  230. assert!(res.is_ok());
  231. let mut page = res.unwrap();
  232. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  233. assert!(page.content.starts_with("<pre"));
  234. }
  235. #[test]
  236. fn test_file_not_named_index_with_assets() {
  237. let tmp_dir = TempDir::new("example").expect("create temp dir");
  238. File::create(tmp_dir.path().join("something.md")).unwrap();
  239. File::create(tmp_dir.path().join("example.js")).unwrap();
  240. File::create(tmp_dir.path().join("graph.jpg")).unwrap();
  241. File::create(tmp_dir.path().join("fail.png")).unwrap();
  242. let page = Page::from_file(tmp_dir.path().join("something.md"), &Config::default());
  243. assert!(page.is_err());
  244. }