summaryrefslogtreecommitdiff
path: root/t/po.t
blob: 51321fa96c73d3a325064fb59bda6aa6b10fbdea (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 => 91;
  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{destdir} = "$dir/dst";
  29. $config{underlaydirbase} = "/dev/null";
  30. $config{underlaydir} = "/dev/null";
  31. $config{discussion} = 0;
  32. $config{po_master_language} = { code => 'en',
  33. name => 'English'
  34. };
  35. $config{po_slave_languages} = {
  36. es => 'Castellano',
  37. fr => "Français"
  38. };
  39. $config{po_translatable_pages}='index or test1 or test2 or translatable';
  40. $config{po_link_to}='negotiated';
  41. IkiWiki::loadplugins();
  42. ok(IkiWiki::loadplugin('meta'), "meta plugin loaded");
  43. ok(IkiWiki::loadplugin('po'), "po plugin loaded");
  44. IkiWiki::checkconfig();
  45. ### seed %pagesources and %pagecase
  46. $pagesources{'index'}='index.mdwn';
  47. $pagesources{'index.fr'}='index.fr.po';
  48. $pagesources{'index.es'}='index.es.po';
  49. $pagesources{'test1'}='test1.mdwn';
  50. $pagesources{'test1.es'}='test1.es.po';
  51. $pagesources{'test1.fr'}='test1.fr.po';
  52. $pagesources{'test2'}='test2.mdwn';
  53. $pagesources{'test2.es'}='test2.es.po';
  54. $pagesources{'test2.fr'}='test2.fr.po';
  55. $pagesources{'test3'}='test3.mdwn';
  56. $pagesources{'test3.es'}='test3.es.mdwn';
  57. $pagesources{'translatable'}='translatable.mdwn';
  58. $pagesources{'translatable.fr'}='translatable.fr.po';
  59. $pagesources{'translatable.es'}='translatable.es.po';
  60. $pagesources{'nontranslatable'}='nontranslatable.mdwn';
  61. foreach my $page (keys %pagesources) {
  62. $IkiWiki::pagecase{lc $page}=$page;
  63. }
  64. ### populate srcdir
  65. writefile('index.mdwn', $config{srcdir},
  66. "[[!meta title=\"index title\"]]\n[[translatable]] [[nontranslatable]]");
  67. writefile('test1.mdwn', $config{srcdir},
  68. "[[!meta title=\"test1 title\"]]\ntest1 content");
  69. writefile('test2.mdwn', $config{srcdir}, 'test2 content');
  70. writefile('test3.mdwn', $config{srcdir}, 'test3 content');
  71. writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
  72. writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
  73. ### istranslatable/istranslation
  74. # we run these tests twice because memoization attempts made them
  75. # succeed once every two tries...
  76. foreach (1, 2) {
  77. ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
  78. ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
  79. ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
  80. ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
  81. ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
  82. ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
  83. ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
  84. ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
  85. ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
  86. ok(IkiWiki::Plugin::po::istranslatable('test1'), "test1 is translatable");
  87. ok(IkiWiki::Plugin::po::istranslation('test1.es'), "test1.es is a translation");
  88. ok(IkiWiki::Plugin::po::istranslation('test1.fr'), "test1.fr is a translation");
  89. ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
  90. ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
  91. ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
  92. ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
  93. }
  94. ### pofiles
  95. my @pofiles = IkiWiki::Plugin::po::pofiles(srcfile("index.mdwn"));
  96. ok( @pofiles, "pofiles is defined");
  97. ok( @pofiles == 2, "pofiles has correct size");
  98. is_deeply(\@pofiles, ["$config{srcdir}/index.es.po", "$config{srcdir}/index.fr.po"], "pofiles content is correct");
  99. ### links
  100. require IkiWiki::Render;
  101. sub refresh_n_scan(@) {
  102. my @masterfiles_rel=@_;
  103. foreach my $masterfile_rel (@masterfiles_rel) {
  104. my $masterfile=srcfile($masterfile_rel);
  105. IkiWiki::scan($masterfile_rel);
  106. next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
  107. my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
  108. IkiWiki::Plugin::po::refreshpot($masterfile);
  109. IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
  110. map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
  111. }
  112. }
  113. $config{po_link_to}='negotiated';
  114. $msgprefix="links (po_link_to=negotiated)";
  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'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
  118. is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
  119. is_deeply(\@{$links{'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. $config{po_link_to}='current';
  124. $msgprefix="links (po_link_to=current)";
  125. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  126. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  127. is_deeply(\@{$links{'index.es'}}, [ (map bestlink('index.es', $_), ('translatable.es', 'nontranslatable'))], "$msgprefix index.es");
  128. is_deeply(\@{$links{'index.fr'}}, [ (map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable'))], "$msgprefix index.fr");
  129. is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
  130. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  131. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  132. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  133. ### targetpage
  134. $config{usedirs}=0;
  135. $msgprefix="targetpage (usedirs=0)";
  136. is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
  137. is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
  138. $config{usedirs}=1;
  139. $msgprefix="targetpage (usedirs=1)";
  140. is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
  141. is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
  142. is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
  143. is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
  144. is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
  145. is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
  146. ### urlto -> index
  147. $config{po_link_to}='current';
  148. $msgprefix="urlto (po_link_to=current)";
  149. is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
  150. is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
  151. is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
  152. $config{po_link_to}='negotiated';
  153. $msgprefix="urlto (po_link_to=negotiated)";
  154. is(urlto('', 'index'), './', "$msgprefix index -> ''");
  155. is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
  156. is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
  157. ### bestlink
  158. $config{po_link_to}='current';
  159. $msgprefix="bestlink (po_link_to=current)";
  160. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  161. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  162. $config{po_link_to}='negotiated';
  163. $msgprefix="bestlink (po_link_to=negotiated)";
  164. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  165. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  166. ### beautify_urlpath
  167. $config{po_link_to}='default';
  168. $msgprefix="beautify_urlpath (po_link_to=default)";
  169. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
  170. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
  171. $config{po_link_to}='negotiated';
  172. $msgprefix="beautify_urlpath (po_link_to=negotiated)";
  173. is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
  174. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
  175. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");
  176. ### re-scan
  177. refresh_n_scan('index.mdwn');
  178. is($pagestate{'index'}{meta}{title}, 'index title');
  179. is($pagestate{'index.es'}{meta}{title}, 'index title');
  180. is($pagestate{'index.fr'}{meta}{title}, 'index title');
  181. refresh_n_scan('test1.mdwn');
  182. is($pagestate{'test1'}{meta}{title}, 'test1 title');
  183. is($pagestate{'test1.es'}{meta}{title}, 'test1 title');
  184. is($pagestate{'test1.fr'}{meta}{title}, 'test1 title');
  185. ### istranslatedto
  186. ok(IkiWiki::Plugin::po::istranslatedto('index', 'es'));
  187. ok(IkiWiki::Plugin::po::istranslatedto('index', 'fr'));
  188. ok(! IkiWiki::Plugin::po::istranslatedto('index', 'cz'));
  189. ok(IkiWiki::Plugin::po::istranslatedto('test1', 'es'));
  190. ok(IkiWiki::Plugin::po::istranslatedto('test1', 'fr'));
  191. ok(! IkiWiki::Plugin::po::istranslatedto('test1', 'cz'));
  192. ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'es'));
  193. ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'cz'));
  194. ok(! IkiWiki::Plugin::po::istranslatedto('test1.es', 'fr'));
  195. ok(! IkiWiki::Plugin::po::istranslatedto('test1.fr', 'es'));