]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/map.t
(no commit message)
[git.ikiwiki.info.git] / t / map.t
1 #!/usr/bin/perl
2 package IkiWiki;
4 use warnings;
5 use strict;
6 use Test::More;
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 }
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"); }
21 ok(! system("rm -rf t/tmp; mkdir t/tmp"));
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;
37 is(checkconfig(), 1);
39 %oldrenderedfiles=%pagectime=();
40 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
41 %destsources=%renderedfiles=%pagecase=%pagestate=();
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 );
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 }
66 sub comment {
67         my $str = shift;
68         $str =~ s/^/# /gm;
69         print $str;
70 }
72 sub node {
73         my $name = shift;
74         my $kids = shift;
75         my %stuff = @_;
77         return { %stuff, name => $name, kids => $kids };
78 }
80 sub check_nodes {
81         my $ul = shift;
82         my $expected = shift;
84         is($ul->tag, 'ul');
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;
91                 is($li->tag, 'li');
93                 my $expectation = shift @$expected;
95                 is($kids[0]->tag, 'a');
96                 my $a = $kids[0];
98                 if ($expectation->{parent}) {
99                         is($a->att('class'), 'mapparent');
100                 }
101                 else {
102                         is($a->att('class'), 'mapitem');
103                 }
105                 is_deeply([$a->text], [$expectation->{name}]);
107                 if (@{$expectation->{kids}}) {
108                         is(scalar @kids, 2);
110                         check_nodes($kids[1], $expectation->{kids});
111                 }
112                 else {
113                         is_deeply([@kids], [$a]);
114                 }
115         }
118 sub check {
119         my $pagespec = shift;
120         my $expected = shift;
121         comment("*** $pagespec ***\n");
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;
137         is($fragment->tag, 'div');
138         is($fragment->att('class'), 'map');
140         if (@$expected) {
141                 check_nodes(($fragment->children)[0], $expected);
142         }
143         else {
144                 ok(! $fragment->children);
145         }
147         $tree->dispose;
150 check('doesnotexist', []);
152 check('alpha', [node('alpha', [])]);
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         ]);
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         ]);
186 check('alpha or alpha/1 or beta',
187         [
188                 node('alpha', [
189                         node('1', []),
190                 ]),
191                 node('beta', []),
192         ]);
194 check('alpha/1 or beta',
195         [
196                 node('alpha', [
197                         node('1', []),
198                 ], parent => 1),
199                 node('beta', []),
200         ]);
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         ]);
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         ]);
231 check('alpha/1/i*',
232         [
233                 node('i', []),
234                 node('ii', []),
235                 node('iii', []),
236                 node('iv', []),
237         ]);
239 ok(! system("rm -rf t/tmp"));
240 done_testing;
242 1;