Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 247 lines (217 sloc) 5.1 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 #
7 # Written by Andy Wardley <abw@cre.canon.co.uk>
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
62 my $tt_reset = Template->new({
63 INTERPOLATE => 1,
64 INCLUDE_PATH => "$dir/src:$dir/lib",
65 TRIM => 1,
3e45060 @abw *** empty log message ***
authored
66 RECURSION => 1,
67 DEFAULT => 'bad_default',
b4f721c @abw *** empty log message ***
authored
68 });
69
3e45060 @abw *** empty log message ***
authored
70 # we want to process 'metadata' directly so that the correct top-level
71 # 'template' reference is set instead of 'input text'
72 my $output;
73 $tproc->process('metadata', $replace, \$output);
74 $replace->{ metaout } = $output;
75 $replace->{ metamod } = (stat("$dir/src/metadata"))[9];
76
b4f721c @abw *** empty log message ***
authored
77 test_expect(\*DATA, [ default => $tproc, reset => $tt_reset ], $replace);
3ebfa07 @abw Initial revision
authored
78
79 __DATA__
b4f721c @abw *** empty log message ***
authored
80 -- test --
3ebfa07 @abw Initial revision
authored
81 [% a %]
b4f721c @abw *** empty log message ***
authored
82 [% PROCESS incblock -%]
3ebfa07 @abw Initial revision
authored
83 [% b %]
c1d9c89 @abw *** empty log message ***
authored
84 [% INCLUDE first_block %]
3ebfa07 @abw Initial revision
authored
85 -- expect --
86 alpha
87 bravo
c1d9c89 @abw *** empty log message ***
authored
88 this is my first block, a is set to 'alpha'
3ebfa07 @abw Initial revision
authored
89
90 -- test --
91 [% INCLUDE first_block %]
92 -- expect --
93 this is my first block, a is set to 'alpha'
94
95 -- test --
96 [% INCLUDE first_block a = 'abstract' %]
97 [% a %]
98 -- expect --
99 this is my first block, a is set to 'abstract'
100 alpha
101
102 -- test --
103 [% INCLUDE 'first_block' a = t %]
104 [% a %]
105 -- expect --
106 this is my first block, a is set to 'tango'
107 alpha
108
109 -- test --
110 [% INCLUDE 'second_block' %]
111 -- expect --
112 this is my second block, a is initially set to 'alpha' and
113 then set to 'sierra' b is bravo m is 98
114
115 -- test --
116 [% INCLUDE second_block a = r, b = c.f.g, m = 97 %]
117 [% a %]
118 -- expect --
119 this is my second block, a is initially set to 'romeo' and
120 then set to 'sierra' b is golf m is 97
121 alpha
122
123 -- test --
b4f721c @abw *** empty log message ***
authored
124 FOO: [% INCLUDE foo +%]
3ebfa07 @abw Initial revision
authored
125 FOO: [% INCLUDE foo a = b -%]
126 -- expect --
b4f721c @abw *** empty log message ***
authored
127 FOO: This is the foo file, a is alpha
128 FOO: This is the foo file, a is bravo
3ebfa07 @abw Initial revision
authored
129
130 -- test --
131 GOLF: [% INCLUDE $c.f.g %]
132 GOLF: [% INCLUDE $c.f.g g = c.f.h %]
133 [% DEFAULT g = "a new $c.f.g" -%]
134 [% g %]
135 -- expect --
136 GOLF: This is the golf file, g is golf
137 GOLF: This is the golf file, g is hotel
138 a new golf
139
140 -- test --
141 BAZ: [% INCLUDE bar/baz %]
142 BAZ: [% INCLUDE bar/baz word='wizzle' %]
143 BAZ: [% INCLUDE "bar/baz" %]
144 -- expect --
145 BAZ: This is file baz
146 The word is 'qux'
147 BAZ: This is file baz
148 The word is 'wizzle'
149 BAZ: This is file baz
150 The word is 'qux'
151
152 -- test --
153 BAZ: [% INCLUDE bar/baz.txt %]
154 BAZ: [% INCLUDE bar/baz.txt time = 'nigh' %]
155 -- expect --
156 BAZ: This is file baz
157 The word is 'qux'
158 The time is now
159 BAZ: This is file baz
160 The word is 'qux'
161 The time is nigh
162
163 -- test --
164 [% BLOCK bamboozle -%]
165 This is bamboozle
166 [%- END -%]
167 Block defined...
168 [% blockname = 'bamboozle' -%]
169 [% INCLUDE $blockname %]
170 End
171 -- expect --
172 Block defined...
173 This is bamboozle
174 End
b4f721c @abw *** empty log message ***
authored
175
176
177 # test that BLOCK definitions get AUTO_RESET (i.e. cleared) by default
178 -- test --
179 -- use reset --
180 [% a %]
181 [% PROCESS incblock -%]
182 [% INCLUDE first_block %]
183 [% INCLUDE second_block %]
184 [% b %]
185 -- expect --
186 alpha
187 this is my first block, a is set to 'alpha'
188 this is my second block, a is initially set to 'alpha' and
189 then set to 'sierra' b is bravo m is 98
190 bravo
191
192 -- test --
193 [% TRY %]
194 [% INCLUDE first_block %]
195 [% CATCH file %]
196 ERROR: [% error.info %]
197 [% END %]
198 -- expect --
199 ERROR: first_block: not found
3e45060 @abw *** empty log message ***
authored
200
201 -- test --
202 -- use default --
203 [% metaout %]
204 -- expect --
205 -- process --
206 TITLE: The cat sat on the mat
207 metadata last modified [% metamod %]
208
209 -- test --
210 [% TRY %]
211 [% PROCESS recurse counter = 1 %]
212 [% CATCH file -%]
213 [% error.info %]
214 [% END %]
215 -- expect --
216 recursion count: 1
217 recursion into 'my file'
218
219 -- test --
220 [% INCLUDE nosuchfile %]
221 -- expect --
222 This is the default file
223
224 -- test --
225 -- use reset --
226 [% TRY %]
227 [% PROCESS recurse counter = 1 %]
228 [% CATCH file %]
229 [% error.info %]
230 [% END %]
231 -- expect --
232 recursion count: 1
233 recursion count: 2
234 recursion count: 3
235
236 -- test --
237 [% TRY;
238 INCLUDE nosuchfile;
239 CATCH;
240 "ERROR: $error";
241 END
242 %]
243 -- expect --
244 ERROR: file error - nosuchfile: not found
245
246
Something went wrong with that request. Please try again.