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