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.

250 lines
6.4KB

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