Merge branch 'master' into fancypodcast
[ikiwiki.git] / t / map.t
1 #!/usr/bin/perl
2 package IkiWiki;
3
4 use warnings;
5 use strict;
6 use XML::Twig;
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 comment {
60         my $str = shift;
61         $str =~ s/^/# /gm;
62         print $str;
63 }
64
65 sub node {
66         my $name = shift;
67         my $kids = shift;
68         my %stuff = @_;
69
70         return { %stuff, name => $name, kids => $kids };
71 }
72
73 sub check_nodes {
74         my $ul = shift;
75         my $expected = shift;
76
77         is($ul->tag, 'ul');
78
79         # expected is a list of hashes
80         # ul is a list of li
81         foreach my $li ($ul->children) {
82                 my @kids = $li->children;
83
84                 is($li->tag, 'li');
85
86                 my $expectation = shift @$expected;
87
88                 is($kids[0]->tag, 'a');
89                 my $a = $kids[0];
90
91                 if ($expectation->{parent}) {
92                         is($a->att('class'), 'mapparent');
93                 }
94                 else {
95                         is($a->att('class'), 'mapitem');
96                 }
97
98                 is_deeply([$a->text], [$expectation->{name}]);
99
100                 if (@{$expectation->{kids}}) {
101                         is(scalar @kids, 2);
102
103                         check_nodes($kids[1], $expectation->{kids});
104                 }
105                 else {
106                         is_deeply([@kids], [$a]);
107                 }
108         }
109 }
110
111 sub check {
112         my $pagespec = shift;
113         my $expected = shift;
114         comment("*** $pagespec ***\n");
115
116         my $html = IkiWiki::Plugin::map::preprocess(pages => $pagespec,
117                 page => 'map',
118                 destpage => 'map');
119         comment($html);
120         my $tree = XML::Twig->new(pretty_print => 'indented');
121         eval {
122                 $tree->parse($html);
123         };
124         if ($@) {
125                 print "malformed XML: $@\n$html\n";
126                 ok(0);
127         }
128         my $fragment = $tree->root;
129
130         is($fragment->tag, 'div');
131         is($fragment->att('class'), 'map');
132
133         if (@$expected) {
134                 check_nodes(($fragment->children)[0], $expected);
135         }
136         else {
137                 ok(! $fragment->children);
138         }
139
140         $tree->dispose;
141 }
142
143 check('doesnotexist', []);
144
145 check('alpha', [node('alpha', [])]);
146
147 check('alpha/*',
148         [
149                 node('1', [
150                         node('i', []),
151                         node('ii', []),
152                         node('iii', []),
153                         node('iv', []),
154                 ]),
155                 node('2', [
156                         node('a', []),
157                         node('b', []),
158                 ]),
159                 node('3', []),
160         ]);
161
162 check('alpha or alpha/*',
163         [
164                 node('alpha', [
165                         node('1', [
166                                 node('i', []),
167                                 node('ii', []),
168                                 node('iii', []),
169                                 node('iv', []),
170                         ]),
171                         node('2', [
172                                 node('a', []),
173                                 node('b', []),
174                         ]),
175                         node('3', []),
176                 ]),
177         ]);
178
179 check('alpha or alpha/1 or beta',
180         [
181                 node('alpha', [
182                         node('1', []),
183                 ]),
184                 node('beta', []),
185         ]);
186
187 check('alpha/1 or beta',
188         [
189                 node('alpha', [
190                         node('1', []),
191                 ], parent => 1),
192                 node('beta', []),
193         ]);
194
195 check('alpha/1/i* or alpha/2/a or beta',
196         [
197                 node('alpha', [
198                         node('1', [
199                                 node('i', []),
200                                 node('ii', []),
201                                 node('iii', []),
202                                 node('iv', []),
203                         ], parent => 1),
204                         node('2', [
205                                 node('a', []),
206                         ], parent => 1),
207                 ], parent => 1),
208                 node('beta', []),
209         ]);
210
211 check('alpha/1/i* or alpha/2/a',
212         [
213                 node('1', [
214                         node('i', []),
215                         node('ii', []),
216                         node('iii', []),
217                         node('iv', []),
218                 ], parent => 1),
219                 node('2', [
220                         node('a', []),
221                 ], parent => 1),
222         ]);
223
224 check('alpha/1/i*',
225         [
226                 node('i', []),
227                 node('ii', []),
228                 node('iii', []),
229                 node('iv', []),
230         ]);
231
232 ok(! system("rm -rf t/tmp"));
233 done_testing;
234
235 1;