summaryrefslogtreecommitdiff
path: root/t/po.t
blob: 444743cac5dff6451402132436f794e0fc19bd93 (plain)
  1. #!/usr/bin/perl
  2. # -*- cperl-indent-level: 8; -*-
  3. use warnings;
  4. use strict;
  5. use File::Temp qw{tempdir};
  6. BEGIN {
  7. unless (eval { require Locale::Po4a::Chooser }) {
  8. eval q{
  9. use Test::More skip_all => "Locale::Po4a::Chooser::new is not available"
  10. }
  11. }
  12. unless (eval { require Locale::Po4a::Po }) {
  13. eval q{
  14. use Test::More skip_all => "Locale::Po4a::Po::new is not available"
  15. }
  16. }
  17. }
  18. use Test::More tests => 68;
  19. BEGIN { use_ok("IkiWiki"); }
  20. my $msgprefix;
  21. my $dir = tempdir("ikiwiki-test-po.XXXXXXXXXX",
  22. DIR => File::Spec->tmpdir,
  23. CLEANUP => 1);
  24. ### Init
  25. %config=IkiWiki::defaultconfig();
  26. $config{srcdir} = "$dir/src";
  27. $config{destdir} = "$dir/dst";
  28. $config{discussion} = 0;
  29. $config{po_master_language} = { code => 'en',
  30. name => 'English'
  31. };
  32. $config{po_slave_languages} = {
  33. es => 'Castellano',
  34. fr => "Français"
  35. };
  36. $config{po_translatable_pages}='index or test1 or test2 or translatable';
  37. $config{po_link_to}='negotiated';
  38. IkiWiki::loadplugins();
  39. ok(IkiWiki::loadplugin('po'), "po plugin loaded");
  40. IkiWiki::checkconfig();
  41. ### seed %pagesources and %pagecase
  42. $pagesources{'index'}='index.mdwn';
  43. $pagesources{'index.fr'}='index.fr.po';
  44. $pagesources{'index.es'}='index.es.po';
  45. $pagesources{'test1'}='test1.mdwn';
  46. $pagesources{'test1.fr'}='test1.fr.po';
  47. $pagesources{'test2'}='test2.mdwn';
  48. $pagesources{'test2.es'}='test2.es.po';
  49. $pagesources{'test2.fr'}='test2.fr.po';
  50. $pagesources{'test3'}='test3.mdwn';
  51. $pagesources{'test3.es'}='test3.es.mdwn';
  52. $pagesources{'translatable'}='translatable.mdwn';
  53. $pagesources{'translatable.fr'}='translatable.fr.po';
  54. $pagesources{'translatable.es'}='translatable.es.po';
  55. $pagesources{'nontranslatable'}='nontranslatable.mdwn';
  56. foreach my $page (keys %pagesources) {
  57. $IkiWiki::pagecase{lc $page}=$page;
  58. }
  59. ### populate srcdir
  60. writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]');
  61. writefile('test1.mdwn', $config{srcdir}, 'test1 content');
  62. writefile('test2.mdwn', $config{srcdir}, 'test2 content');
  63. writefile('test3.mdwn', $config{srcdir}, 'test3 content');
  64. writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
  65. writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
  66. ### istranslatable/istranslation
  67. # we run these tests twice because memoization attempts made them
  68. # succeed once every two tries...
  69. foreach (1, 2) {
  70. ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
  71. ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
  72. ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
  73. ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
  74. ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
  75. ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
  76. ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
  77. ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
  78. ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
  79. ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
  80. ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
  81. ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
  82. ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
  83. }
  84. ### pofiles
  85. my @pofiles = IkiWiki::Plugin::po::pofiles(srcfile("index.mdwn"));
  86. ok( @pofiles, "pofiles is defined");
  87. ok( @pofiles == 2, "pofiles has correct size");
  88. is_deeply(\@pofiles, ["$config{srcdir}/index.es.po", "$config{srcdir}/index.fr.po"], "pofiles content is correct");
  89. ### links
  90. require IkiWiki::Render;
  91. sub refresh_n_scan(@) {
  92. my @masterfiles_rel=@_;
  93. foreach my $masterfile_rel (@masterfiles_rel) {
  94. my $masterfile=srcfile($masterfile_rel);
  95. IkiWiki::scan($masterfile_rel);
  96. next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
  97. my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
  98. IkiWiki::Plugin::po::refreshpot($masterfile);
  99. IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
  100. map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
  101. }
  102. }
  103. $config{po_link_to}='negotiated';
  104. $msgprefix="links (po_link_to=negotiated)";
  105. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  106. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  107. is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable', 'SandBox', 'ikiwiki'], "$msgprefix index.es");
  108. is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable', 'SandBox', 'ikiwiki'], "$msgprefix index.fr");
  109. is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
  110. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  111. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  112. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  113. $config{po_link_to}='current';
  114. $msgprefix="links (po_link_to=current)";
  115. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  116. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  117. is_deeply(\@{$links{'index.es'}}, [ (map bestlink('index.es', $_), ('translatable.es', 'nontranslatable')), 'SandBox', 'ikiwiki'], "$msgprefix index.es");
  118. is_deeply(\@{$links{'index.fr'}}, [ (map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable')), 'SandBox', 'ikiwiki'], "$msgprefix index.fr");
  119. is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
  120. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  121. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  122. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  123. ### targetpage
  124. $config{usedirs}=0;
  125. $msgprefix="targetpage (usedirs=0)";
  126. is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
  127. is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
  128. $config{usedirs}=1;
  129. $msgprefix="targetpage (usedirs=1)";
  130. is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
  131. is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
  132. is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
  133. is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
  134. is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
  135. is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
  136. ### urlto -> index
  137. $config{po_link_to}='current';
  138. $msgprefix="urlto (po_link_to=current)";
  139. is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
  140. is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
  141. is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
  142. $config{po_link_to}='negotiated';
  143. $msgprefix="urlto (po_link_to=negotiated)";
  144. is(urlto('', 'index'), './', "$msgprefix index -> ''");
  145. is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
  146. is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
  147. ### bestlink
  148. $config{po_link_to}='current';
  149. $msgprefix="bestlink (po_link_to=current)";
  150. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  151. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  152. $config{po_link_to}='negotiated';
  153. $msgprefix="bestlink (po_link_to=negotiated)";
  154. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  155. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  156. ### beautify_urlpath
  157. $config{po_link_to}='default';
  158. $msgprefix="beautify_urlpath (po_link_to=default)";
  159. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
  160. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
  161. $config{po_link_to}='negotiated';
  162. $msgprefix="beautify_urlpath (po_link_to=negotiated)";
  163. is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
  164. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
  165. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");