]> sipb.mit.edu Git - ikiwiki.git/blob - t/parentlinks.t
clarification: too many things called cgiurl
[ikiwiki.git] / t / parentlinks.t
1 #!/usr/bin/perl
2 # -*- cperl-indent-level: 8; -*-
3 # Testcases for the Ikiwiki parentlinks plugin.
4
5 use warnings;
6 use strict;
7 use Test::More 'no_plan';
8
9 my %expected;
10
11 BEGIN { use_ok("IkiWiki"); }
12
13 # Init
14 %config=IkiWiki::defaultconfig();
15 $config{srcdir}=$config{destdir}="/dev/null";
16 $config{underlaydir}="underlays/basewiki";
17 $config{templatedir}="t/parentlinks/templates";
18 IkiWiki::loadplugins();
19 IkiWiki::checkconfig();
20
21 # Test data
22 $expected{'parentlinks'} =
23   {
24    "ikiwiki" => [],
25    "ikiwiki/pagespec" =>
26      [ {depth => 0, height => 2, },
27        {depth => 1, height => 1, },
28      ],
29    "ikiwiki/pagespec/attachment" =>
30      [ {depth => 0, height => 3, depth_0 => 1, height_3 => 1},
31        {depth => 1, height => 2, },
32        {depth => 2, height => 1, },
33      ],
34   };
35
36 # Test function
37 sub test_loop($$) {
38         my $loop=shift;
39         my $expected=shift;
40         my $template;
41         my %params;
42
43         ok($template=template('parentlinks.tmpl'), "template created");
44         ok($params{template}=$template, "params populated");
45
46         while ((my $page, my $exp) = each %{$expected}) {
47                 my @path=(split("/", $page));
48                 my $pagedepth=@path;
49                 my $msgprefix="$page $loop";
50
51                 # manually run the plugin hook
52                 $params{page}=$page;
53                 $template->clear_params();
54                 IkiWiki::Plugin::parentlinks::pagetemplate(%params);
55                 my $res=$template->param($loop);
56
57                 is(scalar(@$res), $pagedepth, "$msgprefix: path length");
58                 # logic & arithmetic validation tests
59                 for (my $i=0; $i<$pagedepth; $i++) {
60                         my $r=$res->[$i];
61                         is($r->{height}, $pagedepth - $r->{depth},
62                            "$msgprefix\[$i\]: height = pagedepth - depth");
63                         ok($r->{depth} ge 0, "$msgprefix\[$i\]: depth>=0");
64                         ok($r->{height} ge 0, "$msgprefix\[$i\]: height>=0");
65                 }
66                 # comparison tests, iff the test-suite has been written
67                 if (scalar(@$exp) eq $pagedepth) {
68                         for (my $i=0; $i<$pagedepth; $i++) {
69                                 my $e=$exp->[$i];
70                                 my $r=$res->[$i];
71                                 map { is($r->{$_}, $e->{$_}, "$msgprefix\[$i\]: $_"); } keys %$e;
72                         }
73                 }
74                 # else {
75                 #       diag("Testsuite is incomplete for ($page,$loop); cannot run comparison tests.");
76                 # }
77         }
78 }
79
80 # Main
81 test_loop('parentlinks', $expected{'parentlinks'});