summaryrefslogtreecommitdiff
path: root/t/pedigree.t
blob: aa78cbe677df59bcba7d59addba5d38c6b98622e (plain)
  1. #!/usr/bin/perl
  2. # -*- cperl-indent-level: 8; -*-
  3. # Testcases for the Ikiwiki pedigree plugin.
  4. use warnings;
  5. use strict;
  6. use Test::More 'no_plan';
  7. my %expected;
  8. BEGIN { use_ok("IkiWiki"); }
  9. # Init
  10. %config=IkiWiki::defaultconfig();
  11. $config{srcdir}=$config{destdir}="/dev/null";
  12. $config{underlaydir}="underlays/basewiki";
  13. $config{templatedir}="t/pedigree/templates";
  14. IkiWiki::loadplugins();
  15. IkiWiki::checkconfig();
  16. ok(IkiWiki::loadplugin("pedigree"), "pedigree plugin loaded");
  17. # Test data
  18. $expected{'pedigree'} =
  19. {
  20. "" => [],
  21. "ikiwiki" => [],
  22. "ikiwiki/pagespec" => [
  23. {absdepth => 0,
  24. distance => 2,
  25. is_root => 1,
  26. is_second_ancestor => '',
  27. is_grand_mother => 1,
  28. is_mother => '',
  29. },
  30. {absdepth => 1,
  31. distance => 1,
  32. is_root => '',
  33. is_second_ancestor => 1,
  34. is_grand_mother => '',
  35. is_mother => 1,
  36. },
  37. ],
  38. "ikiwiki/pagespec/attachment" => [
  39. {absdepth => 0,
  40. distance => 3,
  41. is_root => 1,
  42. is_second_ancestor => '',
  43. is_grand_mother => '',
  44. is_mother => '',
  45. },
  46. {absdepth => 1,
  47. distance => 2,
  48. is_root => '',
  49. is_second_ancestor => 1,
  50. is_grand_mother => 1,
  51. is_mother => '',
  52. },
  53. {absdepth => 2,
  54. distance => 1,
  55. is_root => '',
  56. is_second_ancestor => '',
  57. is_grand_mother => '',
  58. is_mother => 1,
  59. },
  60. ],
  61. };
  62. $expected{'pedigree_but_root'} =
  63. {
  64. "" => [],
  65. "ikiwiki" => [],
  66. "ikiwiki/pagespec" => [],
  67. "ikiwiki/pagespec/attachment" => [],
  68. };
  69. $expected{'pedigree_but_two_oldest'} =
  70. {
  71. "" => [],
  72. "ikiwiki" => [],
  73. "ikiwiki/pagespec" => [],
  74. "ikiwiki/pagespec/attachment" => [],
  75. };
  76. # Test function
  77. sub test_loop($$) {
  78. my $loop=shift;
  79. my $expected=shift;
  80. my $template;
  81. my %params;
  82. my $offset;
  83. if ($loop eq 'pedigree') {
  84. $offset=0;
  85. } elsif ($loop eq 'pedigree_but_root') {
  86. $offset=1;
  87. } elsif ($loop eq 'pedigree_but_two_oldest') {
  88. $offset=2;
  89. }
  90. ok($template=template('pedigree.tmpl'), "template created");
  91. ok($params{template}=$template, "params populated");
  92. while ((my $page, my $exp) = each %{$expected}) {
  93. my @path=(split("/", $page));
  94. my $pagedepth=@path;
  95. my $expdepth;
  96. if (($pagedepth - $offset) >= 0) {
  97. $expdepth=$pagedepth - $offset;
  98. } else {
  99. $expdepth=0;
  100. }
  101. my $msgprefix="$page $loop";
  102. # manually run the plugin hook
  103. $params{page}=$page;
  104. $template->clear_params();
  105. IkiWiki::Plugin::pedigree::pagetemplate(%params);
  106. my $res=$template->param($loop);
  107. is(scalar(@$res), $expdepth, "$msgprefix: path length");
  108. # logic & arithmetic validation tests
  109. for (my $i=0; $i<$expdepth; $i++) {
  110. my $r=$res->[$i];
  111. is($r->{distance}, $pagedepth - $r->{absdepth},
  112. "$msgprefix\[$i\]: distance = pagedepth - absdepth");
  113. ok($r->{absdepth} ge 0, "$msgprefix\[$i\]: absdepth>=0");
  114. ok($r->{distance} ge 0, "$msgprefix\[$i\]: distance>=0");
  115. unless ($loop eq 'pedigree') {
  116. ok($r->{reldepth} ge 0, "$msgprefix\[$i\]: reldepth>=0");
  117. TODO: {
  118. local $TODO = "Known bug" if
  119. (($loop eq 'pedigree_but_root')
  120. && ($i >= $offset));
  121. is($r->{reldepth} + $offset, $r->{absdepth},
  122. "$msgprefix\[$i\]: reldepth+offset=absdepth");
  123. }
  124. }
  125. }
  126. # comparison tests, iff the test-suite has been written
  127. if (scalar(@$exp) eq $expdepth) {
  128. for (my $i=0; $i<$expdepth; $i++) {
  129. my $e=$exp->[$i];
  130. my $r=$res->[$i];
  131. map { is($r->{$_}, $e->{$_}, "$msgprefix\[$i\]: $_"); } keys %$e;
  132. }
  133. }
  134. # else {
  135. # diag("Testsuite is incomplete for ($page,$loop); cannot run comparison tests.");
  136. # }
  137. }
  138. }
  139. # Main
  140. map {
  141. test_loop($_, $expected{$_});
  142. } ('pedigree', 'pedigree_but_root', 'pedigree_but_two_oldest');