Add a test for the map directive
[ikiwiki.git] / t / map.t
1 #!/usr/bin/perl
2 package IkiWiki;
3
4 use warnings;
5 use strict;
6 use HTML::TreeBuilder;
7 use Test::More;
8
9 BEGIN { use_ok("IkiWiki"); }
10 BEGIN { use_ok("IkiWiki::Render"); }
11 BEGIN { use_ok("IkiWiki::Plugin::map"); }
12 BEGIN { use_ok("IkiWiki::Plugin::mdwn"); }
13
14 ok(! system("rm -rf t/tmp; mkdir t/tmp"));
15
16 $config{verbose} = 1;
17 $config{srcdir} = 't/tmp';
18 $config{underlaydir} = 't/tmp';
19 $config{underlaydirbase} = '.';
20 $config{templatedir} = 'templates';
21 $config{usedirs} = 1;
22 $config{htmlext} = 'html';
23 $config{wiki_file_chars} = "-[:alnum:]+/.:_";
24 $config{userdir} = "users";
25 $config{tagbase} = "tags";
26 $config{default_pageext} = "mdwn";
27 $config{wiki_file_prune_regexps} = [qr/^\./];
28 $config{autoindex_commit} = 0;
29
30 is(checkconfig(), 1);
31
32 %oldrenderedfiles=%pagectime=();
33 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
34 %destsources=%renderedfiles=%pagecase=%pagestate=();
35
36 my @pages = qw(
37 alpha
38 alpha/1
39 alpha/1/i
40 alpha/1/ii
41 alpha/1/iii
42 alpha/1/iv
43 alpha/2
44 alpha/2/a
45 alpha/2/b
46 alpha/3
47 beta
48 );
49
50 foreach my $page (@pages) {
51         # we use a non-default extension for these, so they're distinguishable
52         # from programmatically-created pages
53         $pagesources{$page} = "$page.mdwn";
54         $destsources{$page} = "$page.mdwn";
55         $pagemtime{$page} = $pagectime{$page} = 1000000;
56         writefile("$page.mdwn", "t/tmp", "your ad here");
57 }
58
59 sub node {
60         my $name = shift;
61         my $kids = shift;
62         my %stuff = @_;
63
64         return { %stuff, name => $name, kids => $kids };
65 }
66
67 sub check_nodes {
68         my $ul = shift;
69         my $expected = shift;
70
71         is($ul->tag, 'ul');
72
73         # expected is a list of hashes
74         # ul is a list of li
75         foreach my $li ($ul->content_list) {
76                 my @kids = $li->content_list;
77
78                 is($li->tag, 'li');
79
80                 my $expectation = shift @$expected;
81
82                 is($kids[0]->tag, 'a');
83                 my $a = $kids[0];
84
85                 if ($expectation->{parent}) {
86                         is($a->attr('class'), 'mapparent');
87                 }
88                 else {
89                         is($a->attr('class'), 'mapitem');
90                 }
91
92                 is_deeply([$a->content_list], [$expectation->{name}]);
93
94                 if (@{$expectation->{kids}}) {
95                         is($kids[1]->tag, 'ul');
96                         is(scalar @kids, 2);
97
98                         check_nodes($kids[1], $expectation->{kids});
99                 }
100                 else {
101                         is_deeply([@kids], [$a]);
102                 }
103         }
104 }
105
106 sub check {
107         my $pagespec = shift;
108         my $expected = shift;
109
110         my $html = IkiWiki::Plugin::map::preprocess(pages => $pagespec,
111                 page => 'map',
112                 destpage => 'map');
113         my $tree = HTML::TreeBuilder->new;
114         $tree->implicit_tags(0);
115         $tree->unbroken_text(1);
116         $tree->strict_end(1);
117         $tree->strict_names(1);
118         $tree->strict_comment(1);
119         $tree->empty_element_tags(1);
120         $tree->parse_content($html);
121         my $fragment = $tree->disembowel;
122         print $fragment->dump;
123
124         is($fragment->tag, 'div');
125         is($fragment->attr('class'), 'map');
126
127         check_nodes(($fragment->content_list)[0], $expected);
128
129         $fragment->delete;
130         print "<!-- -->\n";
131 }
132
133 check('alpha', [node('alpha', [])]);
134
135 check('alpha/*',
136         [
137                 node('1', [
138                         node('i', []),
139                         node('ii', []),
140                         node('iii', []),
141                         node('iv', []),
142                 ]),
143                 node('2', [
144                         node('a', []),
145                         node('b', []),
146                 ]),
147                 node('3', []),
148         ]);
149
150 check('alpha or alpha/*',
151         [
152                 node('alpha', [
153                         node('1', [
154                                 node('i', []),
155                                 node('ii', []),
156                                 node('iii', []),
157                                 node('iv', []),
158                         ]),
159                         node('2', [
160                                 node('a', []),
161                                 node('b', []),
162                         ]),
163                         node('3', []),
164                 ]),
165         ]);
166
167 check('alpha or alpha/1 or beta',
168         [
169                 node('alpha', [
170                         node('1', []),
171                 ]),
172                 node('beta', []),
173         ]);
174
175 done_testing;
176
177 1;