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