f4aac765f2971ff85583b6dccc6843ba7681a340
[ikiwiki.git] / t / tag.t
1 #!/usr/bin/perl
2 package IkiWiki;
3
4 use warnings;
5 use strict;
6 use Test::More tests => 22;
7
8 BEGIN { use_ok("IkiWiki"); }
9 BEGIN { use_ok("IkiWiki::Render"); }
10 BEGIN { use_ok("IkiWiki::Plugin::mdwn"); }
11 BEGIN { use_ok("IkiWiki::Plugin::tag"); }
12
13 ok(! system("rm -rf t/tmp; mkdir t/tmp"));
14
15 $config{verbose} = 1;
16 $config{srcdir} = 't/tmp';
17 $config{underlaydir} = 't/tmp';
18 $config{templatedir} = 'templates';
19 $config{usedirs} = 1;
20 $config{htmlext} = 'html';
21 $config{wiki_file_chars} = "-[:alnum:]+/.:_";
22 $config{userdir} = "users";
23 $config{tagbase} = "tags";
24 $config{tag_autocreate} = 1;
25 $config{tag_autocreate_commit} = 1;
26 $config{default_pageext} = "mdwn";
27 $config{wiki_file_prune_regexps} = [qr/^\./];
28 $config{underlaydirbase} = '.';
29
30 is(checkconfig(), 1);
31
32 %oldrenderedfiles=%pagectime=();
33 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
34 %destsources=%renderedfiles=%pagecase=%pagestate=();
35
36 foreach my $page (qw(tags/numbers tags/letters one two alpha beta)) {
37         $pagesources{$page} = "$page.mdwn";
38         $pagemtime{$page} = $pagectime{$page} = 1000000;
39         writefile("$page.mdwn", "t/tmp", "your ad here");
40 }
41
42 $links{one}=[qw(tags/numbers alpha tags/letters)];
43 $links{two}=[qw(tags/numbers)];
44 $links{alpha}=[qw(tags/letters one)];
45 $links{beta}=[qw(tags/letters)];
46 $typedlinks{one}={tag => {"tags/numbers" => 1 }};
47 $typedlinks{two}={tag => {"tags/numbers" => 1 }};
48 $typedlinks{alpha}={tag => {"tags/letters" => 1 }};
49 $typedlinks{beta}={tag => {"tags/letters" => 1 }};
50
51 ok(pagespec_match("one", "tagged(numbers)"));
52 ok(!pagespec_match("two", "tagged(alpha)"));
53 ok(pagespec_match("one", "link(tags/numbers)"));
54 ok(pagespec_match("one", "link(alpha)"));
55
56 # emulate preprocessing [[!tag numbers primes lucky]] on page "seven", causing
57 # the "numbers" and "primes" tag pages to be auto-created
58 IkiWiki::Plugin::tag::preprocess_tag(page => "seven", numbers => undef, primes => undef, lucky => undef);
59 is($autofiles{"tags/lucky.mdwn"}{plugin}, "tag");
60 is($autofiles{"tags/numbers.mdwn"}{plugin}, "tag");
61 is($autofiles{"tags/primes.mdwn"}{plugin}, "tag");
62 is_deeply([sort keys %autofiles], [qw(tags/lucky.mdwn tags/numbers.mdwn tags/primes.mdwn)]);
63
64 ok(!-e "t/tmp/tags/lucky.mdwn");
65 my (%pages, @del);
66 IkiWiki::gen_autofile("tags/lucky.mdwn", \%pages, \@del);
67 is_deeply(\%pages, {"t/tmp/tags/lucky" => 1}) || diag explain \%pages;
68 is_deeply(\@del, []) || diag explain \@del;
69 ok(-s "t/tmp/tags/lucky.mdwn");
70
71 # generating an autofile that already exists does nothing
72 %pages = @del = ();
73 IkiWiki::gen_autofile("tags/numbers.mdwn", \%pages, \@del);
74 is_deeply(\%pages, {}) || diag explain \%pages;
75 is_deeply(\@del, []) || diag explain \@del;
76
77 # generating an autofile that we just deleted does nothing
78 %pages = ();
79 @del = ('tags/primes.mdwn');
80 IkiWiki::gen_autofile("tags/primes.mdwn", \%pages, \@del);
81 is_deeply(\%pages, {}) || diag explain \%pages;
82 is_deeply(\@del, ['tags/primes.mdwn']) || diag explain \@del;
83
84 1;