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.

252 lines
7.6KB

  1. extern crate gutenberg;
  2. extern crate tera;
  3. extern crate tempdir;
  4. use std::collections::HashMap;
  5. use std::fs::{File, create_dir};
  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_automatic_summary_is_empty_string() {
  153. let content = r#"
  154. +++
  155. title = "Hello"
  156. description = "hey there"
  157. +++
  158. Hello world"#.to_string();
  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. assert_eq!(page.summary, None);
  164. }
  165. #[test]
  166. fn test_can_specify_summary() {
  167. let content = r#"
  168. +++
  169. title = "Hello"
  170. description = "hey there"
  171. +++
  172. Hello world
  173. <!-- more -->
  174. "#.to_string();
  175. let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
  176. assert!(res.is_ok());
  177. let mut page = res.unwrap();
  178. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  179. assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
  180. }
  181. #[test]
  182. fn test_can_auto_detect_when_highlighting_needed() {
  183. let content = r#"
  184. +++
  185. title = "Hello"
  186. description = "hey there"
  187. +++
  188. ```
  189. Hey there
  190. ```
  191. "#.to_string();
  192. let mut config = Config::default();
  193. config.highlight_code = Some(true);
  194. let res = Page::parse(Path::new("hello.md"), &content, &config);
  195. assert!(res.is_ok());
  196. let mut page = res.unwrap();
  197. page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
  198. assert!(page.content.starts_with("<pre"));
  199. }
  200. #[test]
  201. fn test_page_with_assets_gets_right_parent_path() {
  202. let tmp_dir = TempDir::new("example").expect("create temp dir");
  203. let path = tmp_dir.path();
  204. create_dir(&path.join("content")).expect("create content temp dir");
  205. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  206. let nested_path = path.join("content").join("posts").join("assets");
  207. create_dir(&nested_path).expect("create nested temp dir");
  208. File::create(nested_path.join("index.md")).unwrap();
  209. File::create(nested_path.join("example.js")).unwrap();
  210. File::create(nested_path.join("graph.jpg")).unwrap();
  211. File::create(nested_path.join("fail.png")).unwrap();
  212. let res = Page::parse(
  213. nested_path.join("index.md").as_path(),
  214. "+++\nurl=\"hey\"+++\n",
  215. &Config::default()
  216. );
  217. assert!(res.is_ok());
  218. let page = res.unwrap();
  219. assert_eq!(page.parent_path, path.join("content").join("posts"));
  220. }
  221. #[test]
  222. fn test_file_not_named_index_with_assets() {
  223. let tmp_dir = TempDir::new("example").expect("create temp dir");
  224. File::create(tmp_dir.path().join("something.md")).unwrap();
  225. File::create(tmp_dir.path().join("example.js")).unwrap();
  226. File::create(tmp_dir.path().join("graph.jpg")).unwrap();
  227. File::create(tmp_dir.path().join("fail.png")).unwrap();
  228. let page = Page::from_file(tmp_dir.path().join("something.md"), &Config::default());
  229. assert!(page.is_err());
  230. }