summaryrefslogtreecommitdiff
path: root/t/parentlinks.t
blob: 74871cfa8f7766fc7cbe10d6b596cd31e659caf0 (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. [ {depth => 0, height => 2, },
  24. {depth => 1, height => 1, },
  25. ],
  26. "ikiwiki/pagespec/attachment" =>
  27. [ {depth => 0, height => 3, depth_0 => 1, height_3 => 1},
  28. {depth => 1, height => 2, },
  29. {depth => 2, height => 1, },
  30. ],
  31. };
  32. # Test function
  33. sub test_loop($$) {
  34. my $loop=shift;
  35. my $expected=shift;
  36. my $template;
  37. my %params;
  38. ok($template=template('pedigree.tmpl'), "template created");
  39. ok($params{template}=$template, "params populated");
  40. while ((my $page, my $exp) = each %{$expected}) {
  41. my @path=(split("/", $page));
  42. my $pagedepth=@path;
  43. my $msgprefix="$page $loop";
  44. # manually run the plugin hook
  45. $params{page}=$page;
  46. $template->clear_params();
  47. IkiWiki::Plugin::pedigree::pagetemplate(%params);
  48. my $res=$template->param($loop);
  49. is(scalar(@$res), $pagedepth, "$msgprefix: path length");
  50. # logic & arithmetic validation tests
  51. for (my $i=0; $i<$pagedepth; $i++) {
  52. my $r=$res->[$i];
  53. is($r->{height}, $pagedepth - $r->{depth},
  54. "$msgprefix\[$i\]: height = pagedepth - depth");
  55. ok($r->{depth} ge 0, "$msgprefix\[$i\]: depth>=0");
  56. ok($r->{height} ge 0, "$msgprefix\[$i\]: height>=0");
  57. }
  58. # comparison tests, iff the test-suite has been written
  59. if (scalar(@$exp) eq $pagedepth) {
  60. for (my $i=0; $i<$pagedepth; $i++) {
  61. my $e=$exp->[$i];
  62. my $r=$res->[$i];
  63. map { is($r->{$_}, $e->{$_}, "$msgprefix\[$i\]: $_"); } keys %$e;
  64. }
  65. }
  66. # else {
  67. # diag("Testsuite is incomplete for ($page,$loop); cannot run comparison tests.");
  68. # }
  69. }
  70. }
  71. # Main
  72. test_loop('pedigree', $expected{'pedigree'});