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