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.

site_i18n.rs 4.6KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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!(public, "blog/index.html", "Translated in fr: Mon blog https://example.com/fr/blog/"));
  62. assert!(file_contains!(public, "blog/index.html", "Translated in it: Il mio blog https://example.com/it/blog/"));
  63. assert!(file_exists!(public, "fr/blog/index.html"));
  64. assert!(file_contains!(public, "fr/blog/index.html", "Language: fr"));
  65. assert!(file_contains!(public, "fr/blog/index.html", "Translated in : My blog https://example.com/blog/"));
  66. assert!(file_contains!(public, "fr/blog/index.html", "Translated in it: Il mio blog https://example.com/it/blog/"));
  67. // Normal pages are there with the translations
  68. assert!(file_exists!(public, "blog/something/index.html"));
  69. assert!(file_contains!(public, "blog/something/index.html", "Translated in fr: Quelque chose https://example.com/fr/blog/something/"));
  70. assert!(file_exists!(public, "fr/blog/something/index.html"));
  71. assert!(file_contains!(public, "fr/blog/something/index.html", "Language: fr"));
  72. assert!(file_contains!(public, "fr/blog/something/index.html", "Translated in : Something https://example.com/blog/something/"));
  73. // sitemap contains all languages
  74. assert!(file_exists!(public, "sitemap.xml"));
  75. assert!(file_contains!(public, "sitemap.xml", "https://example.com/blog/something-else/"));
  76. assert!(file_contains!(public, "sitemap.xml", "https://example.com/fr/blog/something-else/"));
  77. assert!(file_contains!(public, "sitemap.xml", "https://example.com/it/blog/something-else/"));
  78. // one rss per language
  79. assert!(file_exists!(public, "rss.xml"));
  80. assert!(file_contains!(public, "rss.xml", "https://example.com/blog/something-else/"));
  81. assert!(!file_contains!(public, "rss.xml", "https://example.com/fr/blog/something-else/"));
  82. assert!(file_exists!(public, "fr/rss.xml"));
  83. assert!(!file_contains!(public, "fr/rss.xml", "https://example.com/blog/something-else/"));
  84. assert!(file_contains!(public, "fr/rss.xml", "https://example.com/fr/blog/something-else/"));
  85. // Italian doesn't have RSS enabled
  86. assert!(!file_exists!(public, "it/rss.xml"));
  87. }