]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/7_template_01_includes.t
add PSGI handler
[chaz/p5-CGI-Ex] / t / 7_template_01_includes.t
1 # -*- Mode: Perl; -*-
2
3 =head1 NAME
4
5 7_template_01_includes.t - Test the file include functionality of CGI::Ex::Template - including some edge cases
6
7 =cut
8
9 use vars qw($module $is_tt);
10 BEGIN {
11 $module = 'CGI::Ex::Template';
12 # $module = 'Template';
13 $is_tt = $module eq 'Template';
14 };
15
16 use strict;
17 use Test::More tests => (! $is_tt) ? 93 : 83;
18 use Data::Dumper qw(Dumper);
19 use constant test_taint => 0 && eval { require Taint::Runtime };
20
21 use_ok($module);
22
23 Taint::Runtime::taint_start() if test_taint;
24
25 ### find a place to allow for testing
26 my $test_dir = $0 .'.test_dir';
27 END { rmdir $test_dir }
28 mkdir $test_dir, 0755;
29 ok(-d $test_dir, "Got a test dir up and running");
30
31
32 sub process_ok { # process the value and say if it was ok
33 my $str = shift;
34 my $test = shift;
35 my $vars = shift || {};
36 my $conf = local $vars->{'tt_config'} = $vars->{'tt_config'} || [];
37 my $obj = shift || $module->new(@$conf, ABSOLUTE => 1, INCLUDE_PATH => $test_dir); # new object each time
38 my $out = '';
39 my $line = (caller)[2];
40 delete $vars->{'tt_config'};
41
42 Taint::Runtime::taint(\$str) if test_taint;
43
44 $obj->process(\$str, $vars, \$out);
45 my $ok = ref($test) ? $out =~ $test : $out eq $test;
46 if ($ok) {
47 ok(1, "Line $line \"$str\" => \"$out\"");
48 return $obj;
49 } else {
50 ok(0, "Line $line \"$str\"");
51 warn "# Was:\n$out\n# Should've been:\n$test\n";
52 print $obj->error if $obj->can('error');
53 print Dumper $obj->parse_tree(\$str) if $obj->can('parse_tree');
54 exit;
55 }
56 }
57
58 ### create some files to include
59 my $foo_template = "$test_dir/foo.tt";
60 END { unlink $foo_template };
61 open(my $fh, ">$foo_template") || die "Couldn't open $foo_template: $!";
62 print $fh "([% template.foo %][% INCLUDE bar.tt %])";
63 close $fh;
64
65 ###
66 my $bar_template = "$test_dir/bar.tt";
67 END { unlink $bar_template };
68 open($fh, ">$bar_template") || die "Couldn't open $bar_template: $!";
69 print $fh "[% blue %]BAR";
70 close $fh;
71
72 my $baz_template = "$test_dir/baz.tt";
73 END { unlink $baz_template };
74 open($fh, ">$baz_template") || die "Couldn't open $baz_template: $!";
75 print $fh "[% SET baz = 42 %][% baz %][% bing %]";
76 close $fh;
77
78 ###
79 my $wrap_template = "$test_dir/wrap.tt";
80 END { unlink $wrap_template };
81 open($fh, ">$wrap_template") || die "Couldn't open $wrap_template: $!";
82 print $fh "Hi[% baz; template.foo; baz = 'wrap' %][% content %]there";
83 close $fh;
84
85 ###
86 my $meta_template = "$test_dir/meta.tt";
87 END { unlink $meta_template };
88 open($fh, ">$meta_template") || die "Couldn't open $meta_template: $!";
89 print $fh "[% META bar='meta.tt' %]Metafoo([% component.foo %]) Metabar([% component.bar %])";
90 close $fh;
91
92 ###
93 my $catch_template = "$test_dir/catch.tt";
94 END { unlink $catch_template };
95 open($fh, ">$catch_template") || die "Couldn't open $catch_template: $!";
96 print $fh "Error ([% error.type %]) - ([% error.info %])";
97 close $fh;
98
99 ###
100 my $catch2_template = "$test_dir/catch2.tt";
101 END { unlink $catch2_template };
102 open($fh, ">$catch2_template") || die "Couldn't open $catch2_template: $!";
103 print $fh "Error2 ([% error.type %]) - ([% error.info %])";
104 close $fh;
105
106 ###
107 my $die_template = "$test_dir/die.tt";
108 END { unlink $die_template };
109 open($fh, ">$die_template") || die "Couldn't open $die_template: $!";
110 print $fh "[% THROW bing 'blang' %])";
111 close $fh;
112
113 ###
114 my $config_template = "$test_dir/config.tt";
115 END { unlink $config_template };
116 open($fh, ">$config_template") || die "Couldn't open $config_template: $!";
117 print $fh "[% CONFIG DUMP => {html => 1} %][% DUMP foo %]";
118 close $fh;
119
120
121 ###----------------------------------------------------------------###
122 print "### INSERT ###########################################################\n";
123
124 process_ok("([% INSERT bar.tt %])" => '([% blue %]BAR)');
125 process_ok("([% SET file = 'bar.tt' %][% INSERT \$file %])" => '([% blue %]BAR)');
126 process_ok("([% SET file = 'bar.tt' %][% INSERT \${file} %])" => '([% blue %]BAR)') if ! $is_tt;
127 process_ok("([% SET file = 'bar.tt' %][% INSERT \"\$file\" %])" => '([% blue %]BAR)');
128 process_ok("([% SET file = 'bar' %][% INSERT \"\$file.tt\" %])" => '([% blue %]BAR)') if ! $is_tt;
129
130 ###----------------------------------------------------------------###
131 print "### INCLUDE ##########################################################\n";
132
133 process_ok("([% INCLUDE bar.tt %])" => '(BAR)');
134 process_ok("[% PROCESS foo.tt %]" => '(BAR)');
135 process_ok("[% PROCESS meta.tt %]" => 'Metafoo() Metabar(meta.tt)');
136 process_ok("[% META foo = 'string'; PROCESS meta.tt %]" => 'Metafoo() Metabar(meta.tt)');
137 process_ok("[% PROCESS meta.tt %][% template.bar %]" => 'Metafoo() Metabar(meta.tt)');
138 process_ok("[% META foo = 'meta'; PROCESS foo.tt %]" => '(metaBAR)');
139 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \$file %])" => '(BAR)');
140 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \${file} %])" => '(BAR)') if ! $is_tt;
141 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \"\$file\" %])" => '(BAR)');
142 process_ok("([% SET file = 'bar' %][% INCLUDE \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
143
144 process_ok("([% INCLUDE baz.tt %])" => '(42)');
145 process_ok("([% INCLUDE baz.tt %])[% baz %]" => '(42)');
146 process_ok("[% SET baz = 21 %]([% INCLUDE baz.tt %])[% baz %]" => '(42)21');
147
148 ###----------------------------------------------------------------###
149 print "### PROCESS ##########################################################\n";
150
151 process_ok("([% PROCESS bar.tt %])" => '(BAR)');
152 process_ok("[% PROCESS foo.tt %]" => '(BAR)');
153 process_ok("[% PROCESS meta.tt %]" => 'Metafoo() Metabar(meta.tt)');
154 process_ok("[% META foo = 'string'; PROCESS meta.tt %]" => 'Metafoo() Metabar(meta.tt)');
155 process_ok("[% PROCESS meta.tt %][% template.bar %]" => 'Metafoo() Metabar(meta.tt)');
156 process_ok("[% META foo = 'meta'; PROCESS foo.tt %]" => '(metaBAR)');
157 process_ok("([% SET file = 'bar.tt' %][% PROCESS \$file %])" => '(BAR)');
158 process_ok("([% SET file = 'bar.tt' %][% PROCESS \${file} %])" => '(BAR)') if ! $is_tt;
159 process_ok("([% SET file = 'bar.tt' %][% PROCESS \"\$file\" %])" => '(BAR)');
160 process_ok("([% SET file = 'bar' %][% PROCESS \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
161
162 process_ok("([% PROCESS baz.tt %])" => '(42)');
163 process_ok("([% PROCESS baz.tt %])[% baz %]" => '(42)42');
164 process_ok("[% SET baz = 21 %]([% PROCESS baz.tt %])[% baz %]" => '(42)42');
165
166 ###----------------------------------------------------------------###
167 print "### WRAPPER ##########################################################\n";
168
169 process_ok("([% WRAPPER wrap.tt %])" => '');
170 process_ok("([% WRAPPER wrap.tt %] one [% END %])" => '(Hi one there)');
171 process_ok("([% WRAPPER wrap.tt %] ([% baz %]) [% END %])" => '(Hi () there)');
172 process_ok("([% WRAPPER wrap.tt %] one [% END %])" => '(HiBAZ one there)', {baz => 'BAZ'});
173 process_ok("([% WRAPPER wrap.tt %] ([% baz; baz='-local' %]) [% END %][% baz %])" => '(Hi-local () there-local)');
174 process_ok("([% WRAPPER wrap.tt %][% META foo='BLAM' %] [% END %])" => '(HiBLAM there)');
175
176 ###----------------------------------------------------------------###
177 print "### CONFIG PRE_PROCESS ###############################################\n";
178
179 process_ok("Foo" => "BARFoo", {tt_config => [PRE_PROCESS => 'bar.tt']});
180 process_ok("Foo" => "BARFoo", {tt_config => [PRE_PROCESS => ['bar.tt']]});
181 process_ok("Foo" => "(BAR)BARFoo", {tt_config => [PRE_PROCESS => ['foo.tt', 'bar.tt']]});
182 process_ok("Foo" => "BlueBARFoo", {tt_config => [PRE_PROCESS => 'bar.tt'], blue => 'Blue'});
183 process_ok("Foo[% blue='Blue' %]" => "BARFoo", {tt_config => [PRE_PROCESS => 'bar.tt']});
184 process_ok("Foo[% META foo='meta' %]" => "(metaBAR)Foo", {tt_config => [PRE_PROCESS => 'foo.tt']});
185 process_ok("([% WRAPPER wrap.tt %] one [% END %])" => 'BAR(Hi one there)', {tt_config => [PRE_PROCESS => 'bar.tt']});
186
187 ###----------------------------------------------------------------###
188 print "### CONFIG POST_PROCESS ##############################################\n";
189
190 process_ok("Foo" => "FooBAR", {tt_config => [POST_PROCESS => 'bar.tt']});
191 process_ok("Foo" => "FooBAR", {tt_config => [POST_PROCESS => ['bar.tt']]});
192 process_ok("Foo" => "Foo(BAR)BAR", {tt_config => [POST_PROCESS => ['foo.tt', 'bar.tt']]});
193 process_ok("Foo" => "FooBlueBAR", {tt_config => [POST_PROCESS => 'bar.tt'], blue => 'Blue'});
194 process_ok("Foo[% blue='Blue' %]" => "FooBlueBAR", {tt_config => [POST_PROCESS => 'bar.tt']});
195 process_ok("Foo[% META foo='meta' %]" => "Foo(metaBAR)", {tt_config => [POST_PROCESS => 'foo.tt']});
196 process_ok("([% WRAPPER wrap.tt %] one [% END %])" => '(Hi one there)BAR', {tt_config => [POST_PROCESS => 'bar.tt']});
197
198 ###----------------------------------------------------------------###
199 print "### CONFIG PROCESS ###################################################\n";
200
201 process_ok("Foo" => "BAR", {tt_config => [PROCESS => 'bar.tt']});
202 process_ok("Foo" => "BAR", {tt_config => [PROCESS => ['bar.tt']]});
203 process_ok("Foo" => "(BAR)BAR", {tt_config => [PROCESS => ['foo.tt', 'bar.tt']]});
204 process_ok("Foo" => "BlueBAR", {tt_config => [PROCESS => 'bar.tt'], blue => 'Blue'});
205 process_ok("Foo[% META foo='meta' %]" => "(metaBAR)", {tt_config => [PROCESS => 'foo.tt']});
206 process_ok("Foo[% META foo='meta' %]" => "BAR(metaBAR)", {tt_config => [PRE_PROCESS => 'bar.tt', PROCESS => 'foo.tt']});
207 process_ok("Foo[% META foo='meta' %]" => "(metaBAR)BAR", {tt_config => [POST_PROCESS => 'bar.tt', PROCESS => 'foo.tt']});
208
209 ###----------------------------------------------------------------###
210 print "### CONFIG WRAPPER ###################################################\n";
211
212 process_ok(" one " => 'Hi one there', {tt_config => [WRAPPER => 'wrap.tt']});
213 process_ok(" one " => 'Hi one there', {tt_config => [WRAPPER => ['wrap.tt']]});
214 process_ok(" one " => 'HiwrapHi one therethere', {tt_config => [WRAPPER => ['wrap.tt', 'wrap.tt']]});
215 process_ok(" ([% baz %]) " => 'Hi () there', {tt_config => [WRAPPER => 'wrap.tt']});
216 process_ok(" one " => 'HiBAZ one there', {baz => 'BAZ', tt_config => [WRAPPER => 'wrap.tt']});;
217 process_ok(" ([% baz; baz='-local' %]) " => 'Hi-local () there', {tt_config => [WRAPPER => 'wrap.tt']});
218 process_ok("[% META foo='BLAM' %] " => 'HiBLAM there', {tt_config => [WRAPPER => 'wrap.tt']});
219
220 process_ok(" one " => 'BARHi one there', {tt_config => [WRAPPER => 'wrap.tt', PRE_PROCESS => 'bar.tt']});
221 process_ok(" one " => 'HiBARthere', {tt_config => [WRAPPER => 'wrap.tt', PROCESS => 'bar.tt']});
222 process_ok(" one " => 'Hi one thereBAR', {tt_config => [WRAPPER => 'wrap.tt', POST_PROCESS => 'bar.tt']});
223
224 ###----------------------------------------------------------------###
225 print "### CONFIG ERRORS ####################################################\n";
226
227 process_ok("[% THROW foo 'bar' %]" => 'Error (foo) - (bar)', {tt_config => [ERROR => 'catch.tt']});
228 process_ok("[% THROW foo 'bar' %]" => 'Error (foo) - (bar)', {tt_config => [ERRORS => 'catch.tt']});
229 process_ok("[% THROW foo 'bar' %]" => 'Error (foo) - (bar)', {tt_config => [ERROR => {default => 'catch.tt'}]});
230 process_ok("[% THROW foo 'bar' %]" => 'Error (foo) - (bar)', {tt_config => [ERRORS => {default => 'catch.tt'}]});
231 process_ok("[% THROW foo 'bar' %]" => 'Error2 (foo) - (bar)', {tt_config => [ERRORS => {foo => 'catch2.tt', default => 'catch.tt'}]});
232 process_ok("[% THROW foo.baz 'bar' %]" => 'Error2 (foo.baz) - (bar)', {tt_config => [ERRORS => {foo => 'catch2.tt', default => 'catch.tt'}]});
233 process_ok("[% THROW foo.baz 'bar' %]" => 'Error2 (foo.baz) - (bar)', {tt_config => [ERRORS => {'foo.baz' => 'catch2.tt', default => 'catch.tt'}]});
234 process_ok("[% THROW foo 'bar' %]" => 'Error (foo) - (bar)', {tt_config => [ERRORS => {'foo.baz' => 'catch2.tt', default => 'catch.tt'}]});
235 process_ok("[% THROW foo.baz 'bar' %]" => 'Error2 (foo.baz) - (bar)', {tt_config => [ERRORS => {foo => 'catch2.tt', default => 'catch.tt'}]});
236
237 process_ok("[% THROW foo 'bar' %]" => 'BARError (foo) - (bar)', {tt_config => [ERROR => 'catch.tt', PRE_PROCESS => 'bar.tt']});
238 process_ok("[% THROW foo 'bar' %]" => 'Error (bing) - (blang)', {tt_config => [ERROR => 'catch.tt', PROCESS => 'die.tt']});
239 process_ok("[% THROW foo 'bar' %]" => 'Error (bing) - (blang)', {tt_config => [ERROR => 'catch.tt', PROCESS => ['bar.tt', 'die.tt']]});
240 process_ok("[% THROW foo 'bar' %]" => 'Error (foo) - (bar)BAR', {tt_config => [ERROR => 'catch.tt', POST_PROCESS => 'bar.tt']});
241 process_ok("[% THROW foo 'bar' %]" => 'HiError (foo) - (bar)there', {tt_config => [ERROR => 'catch.tt', WRAPPER => 'wrap.tt']});
242
243 process_ok("(outer)[% PROCESS 'die.tt' %]" => 'Error (bing) - (blang)', {tt_config => [ERROR => 'catch.tt']});
244 process_ok("(outer)[% TRY %][% PROCESS 'die.tt' %][% CATCH %] [% END %]" => '(outer) ', {tt_config => [ERROR => 'catch.tt']});
245
246 process_ok(" one " => '', {tt_config => [ERROR => 'catch.tt', PRE_PROCESS => 'die.tt']});
247 process_ok(" one " => '', {tt_config => [ERROR => 'catch.tt', POST_PROCESS => 'die.tt']});
248 process_ok(" one " => '', {tt_config => [ERROR => 'catch.tt', WRAPPER => 'die.tt']});
249
250 ###----------------------------------------------------------------###
251 print "### CONFIG and DUMP ##################################################\n";
252
253 process_ok("[% CONFIG DUMP => {html => 0}; DUMP foo; PROCESS config.tt; DUMP foo %]" => qq{DUMP: File "input text" line 1
254 foo = 'FOO';
255 <b>DUMP: File "config.tt" line 1</b><pre>foo = &apos;FOO&apos;;
256 </pre>DUMP: File "input text" line 1
257 foo = 'FOO';
258 }, {foo => 'FOO'}) if ! $is_tt;
259
260 ###----------------------------------------------------------------###
261 print "### NOT FOUND CACHE ##################################################\n";
262
263 process_ok("[% BLOCK foo; TRY; PROCESS blurty.tt; CATCH %]([% error.type %])([% error.info %])\n[% END; END; PROCESS foo; PROCESS foo %]" => "(file)(blurty.tt: not found)\n(file)(blurty.tt: not found (cached))\n", {tt_config => [NEGATIVE_STAT_TTL => 2]}) if ! $is_tt;
264 process_ok("[% BLOCK foo; TRY; PROCESS blurty.tt; CATCH %]([% error.type %])([% error.info %])\n[% END; END; PROCESS foo; PROCESS foo %]" => "(file)(blurty.tt: not found)\n(file)(blurty.tt: not found)\n", {tt_config => [NEGATIVE_STAT_TTL => -1]}) if ! $is_tt;
265 process_ok("[% BLOCK foo; TRY; PROCESS blurty.tt; CATCH %]([% error.type %])([% error.info %])\n[% END; END; PROCESS foo; PROCESS foo %]" => "(file)(blurty.tt: not found)\n(file)(blurty.tt: not found)\n", {tt_config => [STAT_TTL => -1]}) if ! $is_tt;
266
267 ###----------------------------------------------------------------###
268 print "### DONE #############################################################\n";
This page took 0.040361 seconds and 4 git commands to generate.