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.

129 lines
4.8KB

  1. extern crate site;
  2. mod common;
  3. use std::env;
  4. use common::build_site;
  5. use site::Site;
  6. #[test]
  7. fn can_parse_multilingual_site() {
  8. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  9. path.push("test_site_i18n");
  10. let mut site = Site::new(&path, "config.toml").unwrap();
  11. site.load().unwrap();
  12. assert_eq!(site.library.pages().len(), 10);
  13. assert_eq!(site.library.sections().len(), 6);
  14. // default index sections
  15. let default_index_section =
  16. site.library.get_section(&path.join("content").join("_index.md")).unwrap();
  17. assert_eq!(default_index_section.pages.len(), 1);
  18. assert!(default_index_section.ancestors.is_empty());
  19. let fr_index_section =
  20. site.library.get_section(&path.join("content").join("_index.fr.md")).unwrap();
  21. assert_eq!(fr_index_section.pages.len(), 1);
  22. assert!(fr_index_section.ancestors.is_empty());
  23. // blog sections get only their own language pages
  24. let blog_path = path.join("content").join("blog");
  25. let default_blog = site.library.get_section(&blog_path.join("_index.md")).unwrap();
  26. assert_eq!(default_blog.subsections.len(), 0);
  27. assert_eq!(default_blog.pages.len(), 4);
  28. assert_eq!(
  29. default_blog.ancestors,
  30. vec![*site.library.get_section_key(&default_index_section.file.path).unwrap()]
  31. );
  32. for key in &default_blog.pages {
  33. let page = site.library.get_page_by_key(*key);
  34. assert_eq!(page.lang, None);
  35. }
  36. let fr_blog = site.library.get_section(&blog_path.join("_index.fr.md")).unwrap();
  37. assert_eq!(fr_blog.subsections.len(), 0);
  38. assert_eq!(fr_blog.pages.len(), 3);
  39. assert_eq!(
  40. fr_blog.ancestors,
  41. vec![*site.library.get_section_key(&fr_index_section.file.path).unwrap()]
  42. );
  43. for key in &fr_blog.pages {
  44. let page = site.library.get_page_by_key(*key);
  45. assert_eq!(page.lang, Some("fr".to_string()));
  46. }
  47. }
  48. #[test]
  49. fn can_build_multilingual_site() {
  50. let (_, _tmp_dir, public) = build_site("test_site_i18n");
  51. assert!(public.exists());
  52. // Index pages
  53. assert!(file_exists!(public, "index.html"));
  54. assert!(file_exists!(public, "fr/index.html"));
  55. assert!(file_contains!(public, "fr/index.html", "Une page"));
  56. assert!(file_contains!(public, "fr/index.html", "Language: fr"));
  57. assert!(file_exists!(public, "base/index.html"));
  58. assert!(file_exists!(public, "fr/base/index.html"));
  59. // Sections are there as well, with translations info
  60. assert!(file_exists!(public, "blog/index.html"));
  61. assert!(file_contains!(
  62. public,
  63. "blog/index.html",
  64. "Translated in fr: Mon blog https://example.com/fr/blog/"
  65. ));
  66. assert!(file_contains!(
  67. public,
  68. "blog/index.html",
  69. "Translated in it: Il mio blog https://example.com/it/blog/"
  70. ));
  71. assert!(file_exists!(public, "fr/blog/index.html"));
  72. assert!(file_contains!(public, "fr/blog/index.html", "Language: fr"));
  73. assert!(file_contains!(
  74. public,
  75. "fr/blog/index.html",
  76. "Translated in : My blog https://example.com/blog/"
  77. ));
  78. assert!(file_contains!(
  79. public,
  80. "fr/blog/index.html",
  81. "Translated in it: Il mio blog https://example.com/it/blog/"
  82. ));
  83. // Normal pages are there with the translations
  84. assert!(file_exists!(public, "blog/something/index.html"));
  85. assert!(file_contains!(
  86. public,
  87. "blog/something/index.html",
  88. "Translated in fr: Quelque chose https://example.com/fr/blog/something/"
  89. ));
  90. assert!(file_exists!(public, "fr/blog/something/index.html"));
  91. assert!(file_contains!(public, "fr/blog/something/index.html", "Language: fr"));
  92. assert!(file_contains!(
  93. public,
  94. "fr/blog/something/index.html",
  95. "Translated in : Something https://example.com/blog/something/"
  96. ));
  97. // sitemap contains all languages
  98. assert!(file_exists!(public, "sitemap.xml"));
  99. assert!(file_contains!(public, "sitemap.xml", "https://example.com/blog/something-else/"));
  100. assert!(file_contains!(public, "sitemap.xml", "https://example.com/fr/blog/something-else/"));
  101. assert!(file_contains!(public, "sitemap.xml", "https://example.com/it/blog/something-else/"));
  102. // one rss per language
  103. assert!(file_exists!(public, "rss.xml"));
  104. assert!(file_contains!(public, "rss.xml", "https://example.com/blog/something-else/"));
  105. assert!(!file_contains!(public, "rss.xml", "https://example.com/fr/blog/something-else/"));
  106. assert!(file_exists!(public, "fr/rss.xml"));
  107. assert!(!file_contains!(public, "fr/rss.xml", "https://example.com/blog/something-else/"));
  108. assert!(file_contains!(public, "fr/rss.xml", "https://example.com/fr/blog/something-else/"));
  109. // Italian doesn't have RSS enabled
  110. assert!(!file_exists!(public, "it/rss.xml"));
  111. }