Skip to content
Newer
Older
100644 299 lines (266 sloc) 6.04 KB
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/include.t
4 #
1303d12 @abw *** empty log message ***
authored
5 # Template script testing the INCLUDE and PROCESS directives.
3ebfa07 @abw Initial revision
authored
6 #
82cbea8 @abw version 2.00
authored
7 # Written by Andy Wardley <abw@kfs.org>
3ebfa07 @abw Initial revision
authored
8 #
3e45060 @abw *** empty log message ***
authored
9 # Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
10 # Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
3ebfa07 @abw Initial revision
authored
11 #
12 # This is free software; you can redistribute it and/or modify it
13 # under the same terms as Perl itself.
14 #
15 # $Id$
16 #
17 #========================================================================
18
19 use strict;
20 use lib qw( ../lib );
21 use Template::Constants qw( :status );
22 use Template;
23 use Template::Test;
24 $^W = 1;
25
b4f721c @abw *** empty log message ***
authored
26 #$Template::Test::DEBUG = 0;
27 #$Template::Context::DEBUG = 0;
3ebfa07 @abw Initial revision
authored
28
29 # sample data
30 my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m,
31 $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z) =
32 qw( alpha bravo charlie delta echo foxtrot golf hotel india
33 juliet kilo lima mike november oscar papa quebec romeo
34 sierra tango umbrella victor whisky x-ray yankee zulu );
35
b4f721c @abw *** empty log message ***
authored
36 my $replace = {
3ebfa07 @abw Initial revision
authored
37 'a' => $a,
38 'b' => $b,
39 'c' => {
40 'd' => $d,
41 'e' => $e,
42 'f' => {
43 'g' => $g,
44 'h' => $h,
45 },
46 },
47 'r' => $r,
48 's' => $s,
49 't' => $t,
50 };
51
b4f721c @abw *** empty log message ***
authored
52 # script may be being run in distribution root or 't' directory
53 my $dir = -d 't' ? 't/test' : 'test';
3ebfa07 @abw Initial revision
authored
54 my $tproc = Template->new({
b4f721c @abw *** empty log message ***
authored
55 INTERPOLATE => 1,
56 INCLUDE_PATH => "$dir/src:$dir/lib",
57 TRIM => 1,
c1d9c89 @abw *** empty log message ***
authored
58 AUTO_RESET => 0,
3e45060 @abw *** empty log message ***
authored
59 DEFAULT => 'default',
3ebfa07 @abw Initial revision
authored
60 });
b4f721c @abw *** empty log message ***
authored
61
8a49dc5 @abw *** empty log message ***
authored
62 my $incpath = [ "$dir/src", '/nowhere' ];
b4f721c @abw *** empty log message ***
authored
63 my $tt_reset = Template->new({
64 INTERPOLATE => 1,
8a49dc5 @abw *** empty log message ***
authored
65 INCLUDE_PATH => $incpath,
b4f721c @abw *** empty log message ***
authored
66 TRIM => 1,
3e45060 @abw *** empty log message ***
authored
67 RECURSION => 1,
68 DEFAULT => 'bad_default',
b4f721c @abw *** empty log message ***
authored
69 });
8a49dc5 @abw *** empty log message ***
authored
70 $incpath->[1] = "$dir/lib";
b4f721c @abw *** empty log message ***
authored
71
3e45060 @abw *** empty log message ***
authored
72 # we want to process 'metadata' directly so that the correct top-level
73 # 'template' reference is set instead of 'input text'
74 my $output;
75 $tproc->process('metadata', $replace, \$output);
76 $replace->{ metaout } = $output;
77 $replace->{ metamod } = (stat("$dir/src/metadata"))[9];
78
b4f721c @abw *** empty log message ***
authored
79 test_expect(\*DATA, [ default => $tproc, reset => $tt_reset ], $replace);
3ebfa07 @abw Initial revision
authored
80
81 __DATA__
b4f721c @abw *** empty log message ***
authored
82 -- test --
3ebfa07 @abw Initial revision
authored
83 [% a %]
b4f721c @abw *** empty log message ***
authored
84 [% PROCESS incblock -%]
3ebfa07 @abw Initial revision
authored
85 [% b %]
c1d9c89 @abw *** empty log message ***
authored
86 [% INCLUDE first_block %]
3ebfa07 @abw Initial revision
authored
87 -- expect --
88 alpha
89 bravo
c1d9c89 @abw *** empty log message ***
authored
90 this is my first block, a is set to 'alpha'
3ebfa07 @abw Initial revision
authored
91
92 -- test --
93 [% INCLUDE first_block %]
94 -- expect --
95 this is my first block, a is set to 'alpha'
96
97 -- test --
98 [% INCLUDE first_block a = 'abstract' %]
99 [% a %]
100 -- expect --
101 this is my first block, a is set to 'abstract'
102 alpha
103
104 -- test --
105 [% INCLUDE 'first_block' a = t %]
106 [% a %]
107 -- expect --
108 this is my first block, a is set to 'tango'
109 alpha
110
111 -- test --
112 [% INCLUDE 'second_block' %]
113 -- expect --
114 this is my second block, a is initially set to 'alpha' and
115 then set to 'sierra' b is bravo m is 98
116
117 -- test --
118 [% INCLUDE second_block a = r, b = c.f.g, m = 97 %]
119 [% a %]
120 -- expect --
121 this is my second block, a is initially set to 'romeo' and
122 then set to 'sierra' b is golf m is 97
123 alpha
124
125 -- test --
b4f721c @abw *** empty log message ***
authored
126 FOO: [% INCLUDE foo +%]
3ebfa07 @abw Initial revision
authored
127 FOO: [% INCLUDE foo a = b -%]
128 -- expect --
b4f721c @abw *** empty log message ***
authored
129 FOO: This is the foo file, a is alpha
130 FOO: This is the foo file, a is bravo
3ebfa07 @abw Initial revision
authored
131
132 -- test --
133 GOLF: [% INCLUDE $c.f.g %]
134 GOLF: [% INCLUDE $c.f.g g = c.f.h %]
135 [% DEFAULT g = "a new $c.f.g" -%]
136 [% g %]
137 -- expect --
138 GOLF: This is the golf file, g is golf
139 GOLF: This is the golf file, g is hotel
140 a new golf
141
142 -- test --
143 BAZ: [% INCLUDE bar/baz %]
144 BAZ: [% INCLUDE bar/baz word='wizzle' %]
145 BAZ: [% INCLUDE "bar/baz" %]
146 -- expect --
147 BAZ: This is file baz
148 The word is 'qux'
149 BAZ: This is file baz
150 The word is 'wizzle'
151 BAZ: This is file baz
152 The word is 'qux'
153
154 -- test --
155 BAZ: [% INCLUDE bar/baz.txt %]
156 BAZ: [% INCLUDE bar/baz.txt time = 'nigh' %]
157 -- expect --
158 BAZ: This is file baz
159 The word is 'qux'
160 The time is now
161 BAZ: This is file baz
162 The word is 'qux'
163 The time is nigh
164
165 -- test --
166 [% BLOCK bamboozle -%]
167 This is bamboozle
168 [%- END -%]
169 Block defined...
170 [% blockname = 'bamboozle' -%]
171 [% INCLUDE $blockname %]
172 End
173 -- expect --
174 Block defined...
175 This is bamboozle
176 End
b4f721c @abw *** empty log message ***
authored
177
178
179 # test that BLOCK definitions get AUTO_RESET (i.e. cleared) by default
180 -- test --
181 -- use reset --
182 [% a %]
183 [% PROCESS incblock -%]
184 [% INCLUDE first_block %]
185 [% INCLUDE second_block %]
186 [% b %]
187 -- expect --
188 alpha
189 this is my first block, a is set to 'alpha'
190 this is my second block, a is initially set to 'alpha' and
191 then set to 'sierra' b is bravo m is 98
192 bravo
193
194 -- test --
195 [% TRY %]
196 [% INCLUDE first_block %]
197 [% CATCH file %]
198 ERROR: [% error.info %]
199 [% END %]
200 -- expect --
201 ERROR: first_block: not found
3e45060 @abw *** empty log message ***
authored
202
203 -- test --
204 -- use default --
205 [% metaout %]
206 -- expect --
207 -- process --
208 TITLE: The cat sat on the mat
209 metadata last modified [% metamod %]
210
211 -- test --
212 [% TRY %]
213 [% PROCESS recurse counter = 1 %]
214 [% CATCH file -%]
215 [% error.info %]
216 [% END %]
217 -- expect --
218 recursion count: 1
219 recursion into 'my file'
220
221 -- test --
222 [% INCLUDE nosuchfile %]
223 -- expect --
224 This is the default file
225
226 -- test --
227 -- use reset --
228 [% TRY %]
229 [% PROCESS recurse counter = 1 %]
230 [% CATCH file %]
231 [% error.info %]
232 [% END %]
233 -- expect --
234 recursion count: 1
235 recursion count: 2
236 recursion count: 3
237
238 -- test --
239 [% TRY;
240 INCLUDE nosuchfile;
241 CATCH;
242 "ERROR: $error";
243 END
244 %]
245 -- expect --
246 ERROR: file error - nosuchfile: not found
247
36ff03e @abw *** empty log message ***
authored
248 -- test --
249 [% INCLUDE src:foo %]
250 [% BLOCK src:foo; "This is foo!"; END %]
251 -- expect --
252 This is foo!
3e45060 @abw *** empty log message ***
authored
253
36ff03e @abw *** empty log message ***
authored
254 -- test --
255 [% a = ''; b = ''; d = ''; e = 0 %]
256 [% INCLUDE foo name = a or b or 'c'
257 item = d or e or 'f' -%]
258 [% BLOCK foo; "name: $name item: $item\n"; END %]
259 -- expect --
260 name: c item: f
261
262 -- test --
263 [% style = 'light'; your_title="Hello World" -%]
264 [% INCLUDE foo
265 title = my_title or your_title or default_title
266 bgcol = (style == 'dark' ? '#000000' : '#ffffff') %]
267 [% BLOCK foo; "title: $title\nbgcol: $bgcol\n"; END %]
268 -- expect --
269 title: Hello World
270 bgcol: #ffffff
6ddeb78 @abw added leak.t
authored
271
272 -- test --
273 [% myhash = {
274 name = 'Tom'
275 item = 'teacup'
276 }
277 -%]
278 [% INCLUDE myblock
279 name = 'Fred'
280 item = 'fish'
281 %]
282 [% INCLUDE myblock
283 import=myhash
284 %]
285 import([% import %])
286 [% PROCESS myblock
287 import={ name = 'Tim', item = 'teapot' }
288 %]
289 import([% import %])
290 [% BLOCK myblock %][% name %] has a [% item %][% END %]
291 -- expect --
292 Fred has a fish
293 Tom has a teacup
294 import()
295 Tim has a teapot
296 import()
297
298 -- test --
Something went wrong with that request. Please try again.