extend test, some cases now fail
[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 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->children) {
76                 my @kids = $li->children;
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->att('class'), 'mapparent');
87                 }
88                 else {
89                         is($a->att('class'), 'mapitem');
90                 }
91
92                 is_deeply([$a->text], [$expectation->{name}]);
93
94                 if (@{$expectation->{kids}}) {
95                         is(scalar @kids, 2);
96
97                         check_nodes($kids[1], $expectation->{kids});
98                 }
99                 else {
100                         is_deeply([@kids], [$a]);
101                 }
102         }
103 }
104
105 sub check {
106         my $pagespec = shift;
107         my $expected = shift;
108         print "*** $pagespec ***\n";
109
110         my $html = IkiWiki::Plugin::map::preprocess(pages => $pagespec,
111                 page => 'map',
112                 destpage => 'map');
113         my $tree = XML::Twig->new(pretty_print => 'indented');
114         eval { 
115                 $tree->parse($html);
116         };
117         if ($@) {
118                 print "malformed XML: $@\n$html\n";
119                 ok(0);
120         }
121         my $fragment = $tree->root;
122
123         is($fragment->tag, 'div');
124         is($fragment->att('class'), 'map');
125
126         if (@$expected) {
127                 check_nodes(($fragment->children)[0], $expected);
128         }
129         else {
130                 ok(! $fragment->children);
131         }
132
133         $tree->dispose;
134         print "<!-- -->\n";
135 }
136
137 check('doesnotexist', []);
138
139 check('alpha', [node('alpha', [])]);
140
141 check('alpha/*',
142         [
143                 node('1', [
144                         node('i', []),
145                         node('ii', []),
146                         node('iii', []),
147                         node('iv', []),
148                 ]),
149                 node('2', [
150                         node('a', []),
151                         node('b', []),
152                 ]),
153                 node('3', []),
154         ]);
155
156 check('alpha or alpha/*',
157         [
158                 node('alpha', [
159                         node('1', [
160                                 node('i', []),
161                                 node('ii', []),
162                                 node('iii', []),
163                                 node('iv', []),
164                         ]),
165                         node('2', [
166                                 node('a', []),
167                                 node('b', []),
168                         ]),
169                         node('3', []),
170                 ]),
171         ]);
172
173 check('alpha or alpha/1 or beta',
174         [
175                 node('alpha', [
176                         node('1', []),
177                 ]),
178                 node('beta', []),
179         ]);
180
181 check('alpha/1 or beta',
182         [
183                 node('alpha', [
184                         node('1', []),
185                 ], parent => 1),
186                 node('beta', []),
187         ]);
188
189 check('alpha/1/i* or alpha/2/a or beta',
190         [
191                 node('alpha', [
192                         node('1', [
193                                 node('i', []),
194                                 node('ii', []),
195                                 node('iii', []),
196                                 node('iv', []),
197                         ], parent => 1),
198                         node('2', [
199                                 node('a', []),
200                         ], parent => 1),
201                 ], parent => 1),
202                 node('beta', []),
203         ]);
204
205 check('alpha/1/i* or alpha/2/a',
206         [
207                 node('1', [
208                         node('i', []),
209                         node('ii', []),
210                         node('iii', []),
211                         node('iv', []),
212                 ], parent => 1),
213                 node('2', [
214                         node('a', []),
215                 ], parent => 1),
216         ]);
217
218 check('alpha/1/i*',
219         [
220                 node('i', []),
221                 node('ii', []),
222                 node('iii', []),
223                 node('iv', []),
224         ]);
225
226 done_testing;
227
228 1;