Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / t / parentlinks.t
blob9b46549038c12b8bc86c316cf7cb3369ded0f77d
1 #!/usr/bin/perl
2 # -*- cperl-indent-level: 8; -*-
3 # Testcases for the Ikiwiki parentlinks plugin.
5 use warnings;
6 use strict;
7 use Test::More 'no_plan';
9 my %expected;
11 BEGIN { use_ok("IkiWiki"); }
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();
21 # Test data
22 $expected{'parentlinks'} =
24 "ikiwiki" => [],
25 "ikiwiki/pagespec" =>
26 [ {depth => 0, height => 2, },
27 {depth => 1, height => 1, },
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, },
36 # Test function
37 sub test_loop($$) {
38 my $loop=shift;
39 my $expected=shift;
40 my $template;
41 my %params;
43 ok($template=template('parentlinks.tmpl'), "template created");
44 ok($params{template}=$template, "params populated");
46 while ((my $page, my $exp) = each %{$expected}) {
47 my @path=(split("/", $page));
48 my $pagedepth=@path;
49 my $msgprefix="$page $loop";
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);
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");
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;
74 # else {
75 # diag("Testsuite is incomplete for ($page,$loop); cannot run comparison tests.");
76 # }
80 # Main
81 test_loop('parentlinks', $expected{'parentlinks'});