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