Merge remote-tracking branch 'smcv/ready/no-trails-if-dynamic'
[ikiwiki.git] / t / map.t
1 #!/usr/bin/perl
2 package IkiWiki;
3
4 use warnings;
5 use strict;
6 use Test::More;
7
8 BEGIN {
9         unless (eval { require XML::Twig }) {
10                 eval q{
11                         use Test::More skip_all => "XML::Twig is not available"
12                 }
13         }
14 }
15
16 BEGIN { use_ok("IkiWiki"); }
17 BEGIN { use_ok("IkiWiki::Render"); }
18 BEGIN { use_ok("IkiWiki::Plugin::map"); }
19 BEGIN { use_ok("IkiWiki::Plugin::mdwn"); }
20
21 ok(! system("rm -rf t/tmp; mkdir t/tmp"));
22
23 $config{verbose} = 1;
24 $config{srcdir} = 't/tmp';
25 $config{underlaydir} = 't/tmp';
26 $config{underlaydirbase} = '.';
27 $config{templatedir} = 'templates';
28 $config{usedirs} = 1;
29 $config{htmlext} = 'html';
30 $config{wiki_file_chars} = "-[:alnum:]+/.:_";
31 $config{userdir} = "users";
32 $config{tagbase} = "tags";
33 $config{default_pageext} = "mdwn";
34 $config{wiki_file_prune_regexps} = [qr/^\./];
35 $config{autoindex_commit} = 0;
36
37 is(checkconfig(), 1);
38
39 %oldrenderedfiles=%pagectime=();
40 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
41 %destsources=%renderedfiles=%pagecase=%pagestate=();
42
43 my @pages = qw(
44 alpha
45 alpha/1
46 alpha/1/i
47 alpha/1/ii
48 alpha/1/iii
49 alpha/1/iv
50 alpha/2
51 alpha/2/a
52 alpha/2/b
53 alpha/3
54 beta
55 );
56
57 foreach my $page (@pages) {
58         # we use a non-default extension for these, so they're distinguishable
59         # from programmatically-created pages
60         $pagesources{$page} = "$page.mdwn";
61         $destsources{$page} = "$page.mdwn";
62         $pagemtime{$page} = $pagectime{$page} = 1000000;
63         writefile("$page.mdwn", "t/tmp", "your ad here");
64 }
65
66 sub comment {
67         my $str = shift;
68         $str =~ s/^/# /gm;
69         print $str;
70 }
71
72 sub node {
73         my $name = shift;
74         my $kids = shift;
75         my %stuff = @_;
76
77         return { %stuff, name => $name, kids => $kids };
78 }
79
80 sub check_nodes {
81         my $ul = shift;
82         my $expected = shift;
83
84         is($ul->tag, 'ul');
85
86         # expected is a list of hashes
87         # ul is a list of li
88         foreach my $li ($ul->children) {
89                 my @kids = $li->children;
90
91                 is($li->tag, 'li');
92
93                 my $expectation = shift @$expected;
94
95                 is($kids[0]->tag, 'a');
96                 my $a = $kids[0];
97
98                 if ($expectation->{parent}) {
99                         is($a->att('class'), 'mapparent');
100                 }
101                 else {
102                         is($a->att('class'), 'mapitem');
103                 }
104
105                 is_deeply([$a->text], [$expectation->{name}]);
106
107                 if (@{$expectation->{kids}}) {
108                         is(scalar @kids, 2);
109
110                         check_nodes($kids[1], $expectation->{kids});
111                 }
112                 else {
113                         is_deeply([@kids], [$a]);
114                 }
115         }
116 }
117
118 sub check {
119         my $pagespec = shift;
120         my $expected = shift;
121         comment("*** $pagespec ***\n");
122
123         my $html = IkiWiki::Plugin::map::preprocess(pages => $pagespec,
124                 page => 'map',
125                 destpage => 'map');
126         comment($html);
127         my $tree = XML::Twig->new(pretty_print => 'indented');
128         eval {
129                 $tree->parse($html);
130         };
131         if ($@) {
132                 print "malformed XML: $@\n$html\n";
133                 ok(0);
134         }
135         my $fragment = $tree->root;
136
137         is($fragment->tag, 'div');
138         is($fragment->att('class'), 'map');
139
140         if (@$expected) {
141                 check_nodes(($fragment->children)[0], $expected);
142         }
143         else {
144                 ok(! $fragment->children);
145         }
146
147         $tree->dispose;
148 }
149
150 check('doesnotexist', []);
151
152 check('alpha', [node('alpha', [])]);
153
154 check('alpha/*',
155         [
156                 node('1', [
157                         node('i', []),
158                         node('ii', []),
159                         node('iii', []),
160                         node('iv', []),
161                 ]),
162                 node('2', [
163                         node('a', []),
164                         node('b', []),
165                 ]),
166                 node('3', []),
167         ]);
168
169 check('alpha or alpha/*',
170         [
171                 node('alpha', [
172                         node('1', [
173                                 node('i', []),
174                                 node('ii', []),
175                                 node('iii', []),
176                                 node('iv', []),
177                         ]),
178                         node('2', [
179                                 node('a', []),
180                                 node('b', []),
181                         ]),
182                         node('3', []),
183                 ]),
184         ]);
185
186 check('alpha or alpha/1 or beta',
187         [
188                 node('alpha', [
189                         node('1', []),
190                 ]),
191                 node('beta', []),
192         ]);
193
194 check('alpha/1 or beta',
195         [
196                 node('alpha', [
197                         node('1', []),
198                 ], parent => 1),
199                 node('beta', []),
200         ]);
201
202 check('alpha/1/i* or alpha/2/a or beta',
203         [
204                 node('alpha', [
205                         node('1', [
206                                 node('i', []),
207                                 node('ii', []),
208                                 node('iii', []),
209                                 node('iv', []),
210                         ], parent => 1),
211                         node('2', [
212                                 node('a', []),
213                         ], parent => 1),
214                 ], parent => 1),
215                 node('beta', []),
216         ]);
217
218 check('alpha/1/i* or alpha/2/a',
219         [
220                 node('1', [
221                         node('i', []),
222                         node('ii', []),
223                         node('iii', []),
224                         node('iv', []),
225                 ], parent => 1),
226                 node('2', [
227                         node('a', []),
228                 ], parent => 1),
229         ]);
230
231 check('alpha/1/i*',
232         [
233                 node('i', []),
234                 node('ii', []),
235                 node('iii', []),
236                 node('iv', []),
237         ]);
238
239 ok(! system("rm -rf t/tmp"));
240 done_testing;
241
242 1;