]> sipb.mit.edu Git - ikiwiki.git/blob - t/templatebody.t
Add regression test for libdir/libdirs
[ikiwiki.git] / t / templatebody.t
1 #!/usr/bin/perl
2 package IkiWiki;
3
4 use warnings;
5 use strict;
6 use Test::More tests => 18;
7
8 BEGIN { use_ok("IkiWiki"); }
9 BEGIN { use_ok("IkiWiki::Render"); }
10 BEGIN { use_ok("IkiWiki::Plugin::templatebody"); }
11 BEGIN { use_ok("IkiWiki::Plugin::mdwn"); }
12 BEGIN { use_ok("IkiWiki::Plugin::tag"); }
13 BEGIN { use_ok("IkiWiki::Plugin::template"); }
14
15 sub assert_pagespec_matches {
16         my $page = shift;
17         my $spec = shift;
18         my @params = @_;
19         @params = (location => 'index') unless @params;
20
21         my $res = pagespec_match($page, $spec, @params);
22
23         if ($res) {
24                 pass($res);
25         }
26         else {
27                 fail($res);
28         }
29 }
30
31 sub assert_pagespec_doesnt_match {
32         my $page = shift;
33         my $spec = shift;
34         my @params = @_;
35         @params = (location => 'index') unless @params;
36
37         my $res = pagespec_match($page, $spec, @params);
38
39         if (ref $res && $res->isa("IkiWiki::ErrorReason")) {
40                 fail($res);
41         }
42         elsif ($res) {
43                 fail($res);
44         }
45         else {
46                 pass($res);
47         }
48 }
49
50 ok(! system("rm -rf t/tmp; mkdir t/tmp t/tmp/src t/tmp/dst"));
51
52 $config{verbose} = 1;
53 $config{srcdir} = 't/tmp/src';
54 $config{underlaydir} = 't/tmp/src';
55 $config{destdir} = 't/tmp/dst';
56 $config{underlaydirbase} = '.';
57 $config{templatedir} = 'templates';
58 $config{usedirs} = 1;
59 $config{htmlext} = 'html';
60 $config{wiki_file_chars} = "-[:alnum:]+/.:_";
61 $config{default_pageext} = "mdwn";
62 $config{wiki_file_prune_regexps} = [qr/^\./];
63
64 is(checkconfig(), 1);
65
66 %oldrenderedfiles=%pagectime=();
67 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
68 %destsources=%renderedfiles=%pagecase=%pagestate=();
69
70 $pagesources{index} = "index.mdwn";
71 $pagemtime{index} = $pagectime{index} = 1000000;
72 writefile("index.mdwn", "t/tmp/src", <<EOF
73 [[!template id="deftmpl" greeting="hello" them="world"]]
74 [[!template id="oldtmpl" greeting="greetings" them="earthlings"]]
75 EOF
76 );
77
78 $pagesources{"templates/deftmpl"} = "templates/deftmpl.mdwn";
79 $pagemtime{index} = $pagectime{index} = 1000000;
80 writefile("templates/deftmpl.mdwn", "t/tmp/src", <<EOF
81 [[!templatebody <<ENDBODY
82 <p><b><TMPL_VAR GREETING>, <TMPL_VAR THEM></b></p>
83 [[!tag greeting]]
84 ENDBODY]]
85
86 This template says hello to someone.
87 [[!tag documentation]]
88 EOF
89 );
90
91 $pagesources{"templates/oldtmpl"} = "templates/oldtmpl.mdwn";
92 $pagemtime{index} = $pagectime{index} = 1000000;
93 writefile("templates/oldtmpl.mdwn", "t/tmp/src", <<EOF
94 <p><i><TMPL_VAR GREETING>, <TMPL_VAR THEM></i></p>
95 EOF
96 );
97
98 my %content;
99
100 foreach my $page (keys %pagesources) {
101         my $content = readfile("t/tmp/src/$pagesources{$page}");
102         $content = IkiWiki::filter($page, $page, $content);
103         $content = IkiWiki::preprocess($page, $page, $content);
104         $content{$page} = $content;
105 }
106
107 # Templates are expanded
108 like($content{index}, qr{<p><b>hello, world</b></p>});
109 like($content{index}, qr{<p><i>greetings, earthlings</i></p>});
110 assert_pagespec_matches('index', 'tagged(greeting)');
111 # The documentation from the templatebody-using page is not expanded
112 unlike($content{index}, qr{This template says hello to someone});
113 assert_pagespec_doesnt_match('index', 'tagged(documentation)');
114
115 # In the templatebody-using page, the documentation is expanded
116 like($content{'templates/deftmpl'}, qr{This template says hello to someone});
117 assert_pagespec_matches('templates/deftmpl', 'tagged(documentation)');
118 # In the templatebody-using page, the template is *not* expanded
119 unlike($content{'templates/deftmpl'}, qr{<p><b>hello, world</b></p>});
120 unlike($content{'templates/deftmpl'}, qr{<p><i>greetings, earthlings</i></p>});
121 assert_pagespec_doesnt_match('templates/deftmpl', 'tagged(greeting)');
122
123 1;