aboutsummaryrefslogtreecommitdiff
path: root/t/pedigree.t
blob: 74871cfa8f7766fc7cbe10d6b596cd31e659caf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
# -*- cperl-indent-level: 8; -*-
# Testcases for the Ikiwiki pedigree plugin.

use warnings;
use strict;
use Test::More 'no_plan';

my %expected;

BEGIN { use_ok("IkiWiki"); }

# Init
%config=IkiWiki::defaultconfig();
$config{srcdir}=$config{destdir}="/dev/null";
$config{underlaydir}="underlays/basewiki";
$config{templatedir}="t/pedigree/templates";
IkiWiki::loadplugins();
IkiWiki::checkconfig();
ok(IkiWiki::loadplugin("pedigree"), "pedigree plugin loaded");

# Test data
$expected{'pedigree'} =
  {
   "" => [],
   "ikiwiki" => [],
   "ikiwiki/pagespec" =>
     [ {depth => 0, height => 2, },
       {depth => 1, height => 1, },
     ],
   "ikiwiki/pagespec/attachment" =>
     [ {depth => 0, height => 3, depth_0 => 1, height_3 => 1},
       {depth => 1, height => 2, },
       {depth => 2, height => 1, },
     ],
  };

# Test function
sub test_loop($$) {
	my $loop=shift;
	my $expected=shift;
	my $template;
	my %params;

	ok($template=template('pedigree.tmpl'), "template created");
	ok($params{template}=$template, "params populated");

	while ((my $page, my $exp) = each %{$expected}) {
		my @path=(split("/", $page));
		my $pagedepth=@path;
		my $msgprefix="$page $loop";

		# manually run the plugin hook
		$params{page}=$page;
		$template->clear_params();
		IkiWiki::Plugin::pedigree::pagetemplate(%params);
		my $res=$template->param($loop);

		is(scalar(@$res), $pagedepth, "$msgprefix: path length");
		# logic & arithmetic validation tests
		for (my $i=0; $i<$pagedepth; $i++) {
			my $r=$res->[$i];
			is($r->{height}, $pagedepth - $r->{depth},
			   "$msgprefix\[$i\]: height = pagedepth - depth");
			ok($r->{depth} ge 0, "$msgprefix\[$i\]: depth>=0");
			ok($r->{height} ge 0, "$msgprefix\[$i\]: height>=0");
		}
		# comparison tests, iff the test-suite has been written
		if (scalar(@$exp) eq $pagedepth) {
			for (my $i=0; $i<$pagedepth; $i++) {
				my $e=$exp->[$i];
				my $r=$res->[$i];
				map { is($r->{$_}, $e->{$_}, "$msgprefix\[$i\]: $_"); } keys %$e;
			}
		}
		# else {
		# 	diag("Testsuite is incomplete for ($page,$loop); cannot run comparison tests.");
		# }
	}
}

# Main
test_loop('pedigree', $expected{'pedigree'});