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