From 7672014582a994624503399a1f50e855c4fc9ca7 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 21 Feb 2014 22:45:29 +0000 Subject: Add templatebody plugin and directive, and enable it by default Also add a regression test for templatebody. --- t/templatebody.t | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100755 t/templatebody.t (limited to 't') diff --git a/t/templatebody.t b/t/templatebody.t new file mode 100755 index 000000000..b0bc82ce5 --- /dev/null +++ b/t/templatebody.t @@ -0,0 +1,123 @@ +#!/usr/bin/perl +package IkiWiki; + +use warnings; +use strict; +use Test::More tests => 18; + +BEGIN { use_ok("IkiWiki"); } +BEGIN { use_ok("IkiWiki::Render"); } +BEGIN { use_ok("IkiWiki::Plugin::templatebody"); } +BEGIN { use_ok("IkiWiki::Plugin::mdwn"); } +BEGIN { use_ok("IkiWiki::Plugin::tag"); } +BEGIN { use_ok("IkiWiki::Plugin::template"); } + +sub assert_pagespec_matches { + my $page = shift; + my $spec = shift; + my @params = @_; + @params = (location => 'index') unless @params; + + my $res = pagespec_match($page, $spec, @params); + + if ($res) { + pass($res); + } + else { + fail($res); + } +} + +sub assert_pagespec_doesnt_match { + my $page = shift; + my $spec = shift; + my @params = @_; + @params = (location => 'index') unless @params; + + my $res = pagespec_match($page, $spec, @params); + + if (ref $res && $res->isa("IkiWiki::ErrorReason")) { + fail($res); + } + elsif ($res) { + fail($res); + } + else { + pass($res); + } +} + +ok(! system("rm -rf t/tmp; mkdir t/tmp t/tmp/src t/tmp/dst")); + +$config{verbose} = 1; +$config{srcdir} = 't/tmp/src'; +$config{underlaydir} = 't/tmp/src'; +$config{destdir} = 't/tmp/dst'; +$config{underlaydirbase} = '.'; +$config{templatedir} = 'templates'; +$config{usedirs} = 1; +$config{htmlext} = 'html'; +$config{wiki_file_chars} = "-[:alnum:]+/.:_"; +$config{default_pageext} = "mdwn"; +$config{wiki_file_prune_regexps} = [qr/^\./]; + +is(checkconfig(), 1); + +%oldrenderedfiles=%pagectime=(); +%pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks= +%destsources=%renderedfiles=%pagecase=%pagestate=(); + +$pagesources{index} = "index.mdwn"; +$pagemtime{index} = $pagectime{index} = 1000000; +writefile("index.mdwn", "t/tmp/src", <,

+[[!tag greeting]] +ENDBODY]] + +This template says hello to someone. +[[!tag documentation]] +EOF +); + +$pagesources{"templates/oldtmpl"} = "templates/oldtmpl.mdwn"; +$pagemtime{index} = $pagectime{index} = 1000000; +writefile("templates/oldtmpl.mdwn", "t/tmp/src", <,

+EOF +); + +my %content; + +foreach my $page (keys %pagesources) { + my $content = readfile("t/tmp/src/$pagesources{$page}"); + $content = IkiWiki::filter($page, $page, $content); + $content = IkiWiki::preprocess($page, $page, $content); + $content{$page} = $content; +} + +# Templates are expanded +like($content{index}, qr{

hello, world

}); +like($content{index}, qr{

greetings, earthlings

}); +assert_pagespec_matches('index', 'tagged(greeting)'); +# The documentation from the templatebody-using page is not expanded +unlike($content{index}, qr{This template says hello to someone}); +assert_pagespec_doesnt_match('index', 'tagged(documentation)'); + +# In the templatebody-using page, the documentation is expanded +like($content{'templates/deftmpl'}, qr{This template says hello to someone}); +assert_pagespec_matches('templates/deftmpl', 'tagged(documentation)'); +# In the templatebody-using page, the template is *not* expanded +unlike($content{'templates/deftmpl'}, qr{

hello, world

}); +unlike($content{'templates/deftmpl'}, qr{

greetings, earthlings

}); +assert_pagespec_doesnt_match('templates/deftmpl', 'tagged(greeting)'); + +1; -- cgit v1.2.3