Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 448 lines (397 sloc) 7.798 kB
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/foreach.t
4 #
5 # Template script testing the FOREACH directive.
6 #
7 # Written by Andy Wardley <abw@cre.canon.co.uk>
8 #
9 # Copyright (C) 1998-1999 Canon Research Centre Europe Ltd.
10 # All Rights Reserved.
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 qw( :status );
22 use Template::Test;
23 $^W = 1;
24
b4f721c @abw *** empty log message ***
authored
25 #$Template::Test::DEBUG = 0;
26 #$Template::Parser::DEBUG = 0;
3ebfa07 @abw Initial revision
authored
27
28 my ($a, $b, $c, $d, $l, $o, $r, $u, $w ) =
29 qw( alpha bravo charlie delta lima oscar romeo uncle whisky );
30
31 my $day = -1;
32 my @days = qw( Monday Tuesday Wednesday Thursday Friday Saturday Sunday );
33 my @months = qw( jan feb mar apr may jun jul aug sep oct nov dec );
34 my @people = ( { 'id' => 'abw', 'name' => 'Andy Wardley' },
35 { 'id' => 'sam', 'name' => 'Simon Matthews' } );
36 my @seta = ( $a, $b, $w );
37 my @setb = ( $c, $l, $o, $u, $d );
38
39
40 my $params = {
41 'a' => $a,
42 'b' => $b,
43 'c' => $c,
44 'C' => uc $c,
45 'd' => $d,
46 'l' => $l,
47 'o' => $o,
48 'r' => $r,
49 'u' => $u,
50 'w' => $w,
51 'seta' => \@seta,
52 'setb' => \@setb,
53 'users' => \@people,
54 'item' => 'foo',
55 'items' => [ 'foo', 'bar' ],
56 'days' => \@days,
57 'months' => sub { return \@months },
58 'format' => \&format,
59 'people' => [
60 { id => 'abw', code => 'abw', name => 'Andy Wardley' },
61 { id => 'aaz', code => 'zaz', name => 'Azbaz Azbaz Zazbazzer' },
62 { id => 'bcd', code => 'dec', name => 'Binary Coded Decimal' },
63 { id => 'efg', code => 'zzz', name => 'Extra Fine Grass' },
64 ],
65 'sections' => {
66 one => 'Section One',
67 two => 'Section Two',
68 three => 'Section Three',
69 four => 'Section Four'
70 },
71
72 };
73
74 sub format {
75 my $format = shift;
76 $format = '%s' unless defined $format;
77 return sub {
78 sprintf($format, shift);
79 }
80 }
81
82 my $template = Template->new({ INTERPOLATE => 1, POST_CHOMP => 1 });
83
84 test_expect(\*DATA, $template, $params);
85
86 __DATA__
87 -- test --
88 [% FOREACH a = [ 1, 2, 3 ] %]
89 [% a +%]
90 [% END %]
91
92 [% FOREACH foo.bar %]
93 [% a %]
94 [% END %]
95 -- expect --
96 1
97 2
98 3
99
100 -- test --
101 Commence countdown...
102 [% FOREACH count = [ 'five' 'four' 'three' 'two' 'one' ] %]
103 [% count +%]
104 [% END %]
105 Fire!
106 -- expect --
107 Commence countdown...
108 five
109 four
110 three
111 two
112 one
113 Fire!
114
115 -- test --
116 [% FOR count = [ 1 2 3 ] %]${count}..[% END %]
117 -- expect --
118 1..2..3..
119
120 -- test --
121 [% for count = [ 1 2 3 ] %]${count}..[% END %]
122 -- expect --
123 1..2..3..
124
125 -- test --
126 [% foreach count = [ 1 2 3 ] %]${count}..[% END %]
127 -- expect --
128 1..2..3..
129
130 -- test --
131 [% for [ 1 2 3 ] %]<blip>..[% END %]
132 -- expect --
133 <blip>..<blip>..<blip>..
134
135 -- test --
136 [% foreach [ 1 2 3 ] %]<blip>..[% END %]
137 -- expect --
138 <blip>..<blip>..<blip>..
139
140 -- test --
141 people:
142 [% bloke = r %]
143 [% people = [ c, bloke, o, 'frank' ] %]
144 [% FOREACH person = people %]
145 [ [% person %] ]
146 [% END %]
147 -- expect --
148 people:
149 [ charlie ]
150 [ romeo ]
151 [ oscar ]
152 [ frank ]
153
154 -- test --
155 [% FOREACH name = setb %]
156 [% name %],
157 [% END %]
158 -- expect --
159 charlie,
160 lima,
161 oscar,
162 uncle,
163 delta,
164
165 -- test --
166 [% FOREACH name = r %]
167 [% name %], $name, wherefore art thou, $name?
168 [% END %]
169 -- expect --
170 romeo, romeo, wherefore art thou, romeo?
171
172 -- test --
173 [% user = 'fred' %]
174 [% FOREACH user = users %]
175 $user.name ([% user.id %])
176 [% END %]
177 [% user.name %]
178 -- expect --
179 Andy Wardley (abw)
180 Simon Matthews (sam)
181 Simon Matthews
182
183 -- test --
184 [% name = 'Joe Random Hacker' id = 'jrh' %]
185 [% FOREACH users %]
186 $name ([% id %])
187 [% END %]
188 $name ($id)
189 -- expect --
190 Andy Wardley (abw)
191 Simon Matthews (sam)
192 Joe Random Hacker (jrh)
193
194 -- test --
195 [% FOREACH i = [1..4] %]
196 [% i +%]
197 [% END %]
198 -- expect --
199 1
200 2
201 3
202 4
203
204 -- test --
205 [% first = 4
206 last = 8
207 %]
208 [% FOREACH i = [first..last] %]
209 [% i +%]
210 [% END %]
211 -- expect --
212 4
213 5
214 6
215 7
216 8
217
218 -- test --
219 [% list = [ 'one' 'two' 'three' 'four' ] %]
220 [% list.0 %] [% list.3 %]
221
222 [% FOREACH n = [0..3] %]
223 [% list.${n} %],
224 [%- END %]
225
226 -- expect --
227 one four
228 one, two, three, four,
229
230 -- test --
231 [% "$i, " FOREACH i = [-2..2] %]
232
233 -- expect --
234 -2, -1, 0, 1, 2,
235
236 -- test --
237 [% FOREACH i = item -%]
238 - [% i %]
239 [% END %]
240 -- expect --
241 - foo
242
243 -- test --
244 [% FOREACH i = items -%]
245 - [% i +%]
246 [% END %]
247 -- expect --
248 - foo
249 - bar
250
251 -- test --
252 [% FOREACH item = [ a b c d ] %]
253 $item
254 [% END %]
255 -- expect --
256 alpha
257 bravo
258 charlie
259 delta
260
261 -- test --
262 [% items = [ d C a c b ] %]
263 [% FOREACH item = items.sort %]
264 $item
265 [% END %]
266 -- expect --
267 alpha
268 bravo
269 CHARLIE
270 charlie
271 delta
272
273 -- test --
274 [% items = [ d a c b ] %]
275 [% FOREACH item = items.sort.reverse %]
276 $item
277 [% END %]
278 -- expect --
279 delta
280 charlie
281 bravo
282 alpha
283
284 -- test --
285 [% userlist = [ b c d a C 'Andy' 'tom' 'dick' 'harry' ] %]
286 [% FOREACH u = userlist.sort %]
287 $u
288 [% END %]
289 -- expect --
290 alpha
291 Andy
292 bravo
293 charlie
294 CHARLIE
295 delta
296 dick
297 harry
298 tom
299
300 -- test --
301 [% ulist = [ b c d a 'Andy' ] %]
302 [% USE f = format("[- %-7s -]\n") %]
303 [% f(item) FOREACH item = ulist.sort %]
304 -- expect --
305 [- alpha -]
306 [- Andy -]
307 [- bravo -]
308 [- charlie -]
309 [- delta -]
310
311 -- test --
312 [% FOREACH item = [ a b c d ] %]
313 [% "List of $loop.size items:\n" IF loop.first %]
314 #[% loop.number %]/[% loop.size %]: [% item +%]
315 [% "That's all folks\n" IF loop.last %]
316 [% END %]
317 -- expect --
318 List of 4 items:
319 #1/4: alpha
320 #2/4: bravo
321 #3/4: charlie
322 #4/4: delta
323 That's all folks
324
325 -- test --
326 [% items = [ d b c a ] %]
327 [% FOREACH item = items.sort %]
328 [% "List of $loop.size items:\n----------------\n" IF loop.first %]
329 * [% item +%]
330 [% "----------------\n" IF loop.last %]
331 [% END %]
332 -- expect --
333 List of 4 items:
334 ----------------
335 * alpha
336 * bravo
337 * charlie
338 * delta
339 ----------------
340
341 -- test --
342 [% list = [ a b c d ] %]
343 [% i = 1 %]
344 [% FOREACH item = list %]
345 #[% i %]/[% list.size %]: [% item +%]
346 [% i = inc(i) %]
347 [% END %]
348 -- expect --
349 #1/4: alpha
350 #2/4: bravo
351 #3/4: charlie
352 #4/4: delta
353
354 -- test --
355 [% FOREACH a = ['foo', 'bar', 'baz'] %]
356 * [% loop.index %] [% a +%]
357 [% FOREACH b = ['wiz', 'woz', 'waz'] %]
358 - [% loop.index %] [% b +%]
359 [% END %]
360 [% END %]
361
362 -- expect --
363 * 0 foo
364 - 0 wiz
365 - 1 woz
366 - 2 waz
367 * 1 bar
368 - 0 wiz
369 - 1 woz
370 - 2 waz
371 * 2 baz
372 - 0 wiz
373 - 1 woz
374 - 2 waz
375
376 -- test --
377 [% id = 12345
378 name = 'Original'
379 user1 = { id => 'tom', name => 'Thomas' }
380 user2 = { id => 'reg', name => 'Reginald' }
381 %]
382 [% FOREACH [ user1 ] %]
383 id: [% id +%]
384 name: [% name +%]
385 [% FOREACH [ user2 ] %]
386 - id: [% id +%]
387 - name: [% name +%]
388 [% END %]
389 [% END %]
390 id: [% id +%]
391 name: [% name +%]
392 -- expect --
393 id: tom
394 name: Thomas
395 - id: reg
396 - name: Reginald
397 id: 12345
398 name: Original
399
400 -- test --
401 [% them = [ people.1 people.2 ] %]
402 [% "$p.id($p.code): $p.name\n"
403 FOREACH p = them.sort('id') %]
404 -- expect --
405 aaz(zaz): Azbaz Azbaz Zazbazzer
406 bcd(dec): Binary Coded Decimal
407
408 -- test --
409 [% "$p.id($p.code): $p.name\n"
410 FOREACH p = people.sort('code') %]
411 -- expect --
412 abw(abw): Andy Wardley
413 bcd(dec): Binary Coded Decimal
414 aaz(zaz): Azbaz Azbaz Zazbazzer
415 efg(zzz): Extra Fine Grass
416
417 -- test --
418 [% "$p.id($p.code): $p.name\n"
419 FOREACH p = people.sort('code').reverse %]
420 -- expect --
421 efg(zzz): Extra Fine Grass
422 aaz(zaz): Azbaz Azbaz Zazbazzer
423 bcd(dec): Binary Coded Decimal
424 abw(abw): Andy Wardley
425
426 -- test --
427 [% "$p.id($p.code): $p.name\n"
428 FOREACH p = people.sort('code') %]
429 -- expect --
430 abw(abw): Andy Wardley
431 bcd(dec): Binary Coded Decimal
432 aaz(zaz): Azbaz Azbaz Zazbazzer
433 efg(zzz): Extra Fine Grass
434
435
436 -- test --
437 Section List:
438 [% FOREACH item = sections %]
439 [% item.key %] - [% item.value +%]
440 [% END %]
441 -- expect --
442 Section List:
443 four - Section Four
444 one - Section One
445 three - Section Three
446 two - Section Two
447
Something went wrong with that request. Please try again.