Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 614 lines (556 sloc) 10.07 kb
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/foreach.t
4 #
5 # Template script testing the FOREACH directive.
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;
2ca01ce @abw added INC path
authored
20 use lib qw( ./lib ../lib );
3ebfa07 @abw Initial revision
authored
21 use Template qw( :status );
22 use Template::Test;
23 $^W = 1;
24
b4f721c @abw *** empty log message ***
authored
25 #$Template::Test::DEBUG = 0;
3e45060 @abw *** empty log message ***
authored
26 #$Template::Parser::DEBUG = 1;
31cb481 @abw 2.03
authored
27 #$Template::Directive::PRETTY = 1;
3ebfa07 @abw Initial revision
authored
28
29 my ($a, $b, $c, $d, $l, $o, $r, $u, $w ) =
30 qw( alpha bravo charlie delta lima oscar romeo uncle whisky );
31
32 my $day = -1;
33 my @days = qw( Monday Tuesday Wednesday Thursday Friday Saturday Sunday );
34 my @months = qw( jan feb mar apr may jun jul aug sep oct nov dec );
35 my @people = ( { 'id' => 'abw', 'name' => 'Andy Wardley' },
36 { 'id' => 'sam', 'name' => 'Simon Matthews' } );
37 my @seta = ( $a, $b, $w );
38 my @setb = ( $c, $l, $o, $u, $d );
39
40
41 my $params = {
42 'a' => $a,
43 'b' => $b,
44 'c' => $c,
45 'C' => uc $c,
46 'd' => $d,
47 'l' => $l,
48 'o' => $o,
49 'r' => $r,
50 'u' => $u,
51 'w' => $w,
52 'seta' => \@seta,
53 'setb' => \@setb,
54 'users' => \@people,
55 'item' => 'foo',
56 'items' => [ 'foo', 'bar' ],
57 'days' => \@days,
58 'months' => sub { return \@months },
59 'format' => \&format,
60 'people' => [
61 { id => 'abw', code => 'abw', name => 'Andy Wardley' },
62 { id => 'aaz', code => 'zaz', name => 'Azbaz Azbaz Zazbazzer' },
63 { id => 'bcd', code => 'dec', name => 'Binary Coded Decimal' },
64 { id => 'efg', code => 'zzz', name => 'Extra Fine Grass' },
65 ],
66 'sections' => {
67 one => 'Section One',
68 two => 'Section Two',
69 three => 'Section Three',
7a87144 @abw added Dumper plugin, fixed DOM, misc other fixes
authored
70 four => 'Section Four',
3ebfa07 @abw Initial revision
authored
71 },
7a87144 @abw added Dumper plugin, fixed DOM, misc other fixes
authored
72 nested => [
73 [ qw( a b c ) ],
74 [ qw( x y z ) ],
75 ],
3ebfa07 @abw Initial revision
authored
76 };
77
78 sub format {
79 my $format = shift;
80 $format = '%s' unless defined $format;
81 return sub {
82 sprintf($format, shift);
83 }
84 }
85
82cbea8 @abw version 2.00
authored
86 my $template = Template->new({
87 INTERPOLATE => 1,
88 POST_CHOMP => 1,
89 ANYCASE => 0
90 });
3ebfa07 @abw Initial revision
authored
91
fa9c1c8 @abw patch from Eric, FOREACH/DEBUG loop fix
authored
92 my $ttdebug = Template->new({
93 DEBUG => 1,
6173dda @abw Added t/debug.t and fixes some DEBUG issues in stash.t and foreach.t
authored
94 DEBUG_FORMAT => '',
fa9c1c8 @abw patch from Eric, FOREACH/DEBUG loop fix
authored
95 });
96
97 test_expect(\*DATA, [ default => $template, debug => $ttdebug ], $params);
3ebfa07 @abw Initial revision
authored
98
99 __DATA__
100 -- test --
101 [% FOREACH a = [ 1, 2, 3 ] %]
102 [% a +%]
103 [% END %]
104
105 [% FOREACH foo.bar %]
106 [% a %]
107 [% END %]
108 -- expect --
109 1
110 2
111 3
112
113 -- test --
114 Commence countdown...
115 [% FOREACH count = [ 'five' 'four' 'three' 'two' 'one' ] %]
116 [% count +%]
117 [% END %]
118 Fire!
119 -- expect --
120 Commence countdown...
121 five
122 four
123 three
124 two
125 one
126 Fire!
127
128 -- test --
129 [% FOR count = [ 1 2 3 ] %]${count}..[% END %]
130 -- expect --
131 1..2..3..
132
133 -- test --
134 people:
135 [% bloke = r %]
136 [% people = [ c, bloke, o, 'frank' ] %]
137 [% FOREACH person = people %]
138 [ [% person %] ]
139 [% END %]
140 -- expect --
141 people:
142 [ charlie ]
143 [ romeo ]
144 [ oscar ]
145 [ frank ]
146
147 -- test --
148 [% FOREACH name = setb %]
149 [% name %],
150 [% END %]
151 -- expect --
152 charlie,
153 lima,
154 oscar,
155 uncle,
156 delta,
157
158 -- test --
159 [% FOREACH name = r %]
160 [% name %], $name, wherefore art thou, $name?
161 [% END %]
162 -- expect --
163 romeo, romeo, wherefore art thou, romeo?
164
165 -- test --
166 [% user = 'fred' %]
167 [% FOREACH user = users %]
168 $user.name ([% user.id %])
169 [% END %]
170 [% user.name %]
171 -- expect --
172 Andy Wardley (abw)
173 Simon Matthews (sam)
174 Simon Matthews
175
176 -- test --
177 [% name = 'Joe Random Hacker' id = 'jrh' %]
178 [% FOREACH users %]
179 $name ([% id %])
180 [% END %]
181 $name ($id)
182 -- expect --
183 Andy Wardley (abw)
184 Simon Matthews (sam)
185 Joe Random Hacker (jrh)
186
187 -- test --
188 [% FOREACH i = [1..4] %]
189 [% i +%]
190 [% END %]
191 -- expect --
192 1
193 2
194 3
195 4
196
197 -- test --
198 [% first = 4
199 last = 8
200 %]
201 [% FOREACH i = [first..last] %]
202 [% i +%]
203 [% END %]
204 -- expect --
205 4
206 5
207 6
208 7
209 8
210
211 -- test --
212 [% list = [ 'one' 'two' 'three' 'four' ] %]
213 [% list.0 %] [% list.3 %]
214
215 [% FOREACH n = [0..3] %]
216 [% list.${n} %],
217 [%- END %]
218
219 -- expect --
220 one four
221 one, two, three, four,
222
223 -- test --
224 [% "$i, " FOREACH i = [-2..2] %]
225
226 -- expect --
227 -2, -1, 0, 1, 2,
228
229 -- test --
230 [% FOREACH i = item -%]
231 - [% i %]
232 [% END %]
233 -- expect --
234 - foo
235
236 -- test --
237 [% FOREACH i = items -%]
238 - [% i +%]
239 [% END %]
240 -- expect --
241 - foo
242 - bar
243
244 -- test --
245 [% FOREACH item = [ a b c d ] %]
246 $item
247 [% END %]
248 -- expect --
249 alpha
250 bravo
251 charlie
252 delta
253
254 -- test --
255 [% items = [ d C a c b ] %]
256 [% FOREACH item = items.sort %]
257 $item
258 [% END %]
259 -- expect --
260 alpha
261 bravo
262 CHARLIE
263 charlie
264 delta
265
266 -- test --
267 [% items = [ d a c b ] %]
268 [% FOREACH item = items.sort.reverse %]
269 $item
270 [% END %]
271 -- expect --
272 delta
273 charlie
274 bravo
275 alpha
276
277 -- test --
278 [% userlist = [ b c d a C 'Andy' 'tom' 'dick' 'harry' ] %]
279 [% FOREACH u = userlist.sort %]
280 $u
281 [% END %]
282 -- expect --
283 alpha
284 Andy
285 bravo
286 charlie
287 CHARLIE
288 delta
289 dick
290 harry
291 tom
292
293 -- test --
294 [% ulist = [ b c d a 'Andy' ] %]
295 [% USE f = format("[- %-7s -]\n") %]
296 [% f(item) FOREACH item = ulist.sort %]
297 -- expect --
298 [- alpha -]
299 [- Andy -]
300 [- bravo -]
301 [- charlie -]
302 [- delta -]
303
304 -- test --
305 [% FOREACH item = [ a b c d ] %]
306 [% "List of $loop.size items:\n" IF loop.first %]
307 #[% loop.number %]/[% loop.size %]: [% item +%]
308 [% "That's all folks\n" IF loop.last %]
309 [% END %]
310 -- expect --
311 List of 4 items:
312 #1/4: alpha
313 #2/4: bravo
314 #3/4: charlie
315 #4/4: delta
316 That's all folks
317
318 -- test --
319 [% items = [ d b c a ] %]
320 [% FOREACH item = items.sort %]
321 [% "List of $loop.size items:\n----------------\n" IF loop.first %]
322 * [% item +%]
323 [% "----------------\n" IF loop.last %]
324 [% END %]
325 -- expect --
326 List of 4 items:
327 ----------------
328 * alpha
329 * bravo
330 * charlie
331 * delta
332 ----------------
333
334 -- test --
335 [% list = [ a b c d ] %]
336 [% i = 1 %]
337 [% FOREACH item = list %]
338 #[% i %]/[% list.size %]: [% item +%]
339 [% i = inc(i) %]
340 [% END %]
341 -- expect --
342 #1/4: alpha
343 #2/4: bravo
344 #3/4: charlie
345 #4/4: delta
346
347 -- test --
348 [% FOREACH a = ['foo', 'bar', 'baz'] %]
349 * [% loop.index %] [% a +%]
350 [% FOREACH b = ['wiz', 'woz', 'waz'] %]
351 - [% loop.index %] [% b +%]
352 [% END %]
353 [% END %]
354
355 -- expect --
356 * 0 foo
357 - 0 wiz
358 - 1 woz
359 - 2 waz
360 * 1 bar
361 - 0 wiz
362 - 1 woz
363 - 2 waz
364 * 2 baz
365 - 0 wiz
366 - 1 woz
367 - 2 waz
368
369 -- test --
370 [% id = 12345
371 name = 'Original'
372 user1 = { id => 'tom', name => 'Thomas' }
373 user2 = { id => 'reg', name => 'Reginald' }
374 %]
375 [% FOREACH [ user1 ] %]
376 id: [% id +%]
377 name: [% name +%]
378 [% FOREACH [ user2 ] %]
379 - id: [% id +%]
380 - name: [% name +%]
381 [% END %]
382 [% END %]
383 id: [% id +%]
384 name: [% name +%]
385 -- expect --
386 id: tom
387 name: Thomas
388 - id: reg
389 - name: Reginald
390 id: 12345
391 name: Original
392
393 -- test --
394 [% them = [ people.1 people.2 ] %]
395 [% "$p.id($p.code): $p.name\n"
396 FOREACH p = them.sort('id') %]
397 -- expect --
398 aaz(zaz): Azbaz Azbaz Zazbazzer
399 bcd(dec): Binary Coded Decimal
400
401 -- test --
402 [% "$p.id($p.code): $p.name\n"
403 FOREACH p = people.sort('code') %]
404 -- expect --
405 abw(abw): Andy Wardley
406 bcd(dec): Binary Coded Decimal
407 aaz(zaz): Azbaz Azbaz Zazbazzer
408 efg(zzz): Extra Fine Grass
409
410 -- test --
411 [% "$p.id($p.code): $p.name\n"
412 FOREACH p = people.sort('code').reverse %]
413 -- expect --
414 efg(zzz): Extra Fine Grass
415 aaz(zaz): Azbaz Azbaz Zazbazzer
416 bcd(dec): Binary Coded Decimal
417 abw(abw): Andy Wardley
418
419 -- test --
420 [% "$p.id($p.code): $p.name\n"
421 FOREACH p = people.sort('code') %]
422 -- expect --
423 abw(abw): Andy Wardley
424 bcd(dec): Binary Coded Decimal
425 aaz(zaz): Azbaz Azbaz Zazbazzer
426 efg(zzz): Extra Fine Grass
427
428
429 -- test --
430 Section List:
431 [% FOREACH item = sections %]
432 [% item.key %] - [% item.value +%]
433 [% END %]
434 -- expect --
435 Section List:
436 four - Section Four
437 one - Section One
438 three - Section Three
439 two - Section Two
440
3e45060 @abw *** empty log message ***
authored
441 -- test --
442 [% FOREACH a = [ 2..6 ] %]
443 before [% a %]
d52480d @abw *** empty log message ***
authored
444 [% NEXT IF a == 5 +%]
3e45060 @abw *** empty log message ***
authored
445 after [% a +%]
446 [% END %]
447 -- expect --
448 before 2
449 after 2
450 before 3
451 after 3
452 before 4
453 after 4
454 before 5before 6
455 after 6
456
457 -- test --
458 [% count = 1; WHILE (count < 10) %]
459 [% count = count + 1 %]
82cbea8 @abw version 2.00
authored
460 [% NEXT IF count < 5 %]
3e45060 @abw *** empty log message ***
authored
461 count: [% count +%]
462 [% END %]
463 -- expect --
464 count: 5
465 count: 6
466 count: 7
467 count: 8
468 count: 9
469 count: 10
d52480d @abw *** empty log message ***
authored
470
471 -- test --
82cbea8 @abw version 2.00
authored
472 [% FOR count = [ 1 2 3 ] %]${count}..[% END %]
d52480d @abw *** empty log message ***
authored
473 -- expect --
474 1..2..3..
475
476 -- test --
82cbea8 @abw version 2.00
authored
477 [% FOREACH count = [ 1 2 3 ] %]${count}..[% END %]
d52480d @abw *** empty log message ***
authored
478 -- expect --
479 1..2..3..
480
481 -- test --
82cbea8 @abw version 2.00
authored
482 [% FOR [ 1 2 3 ] %]<blip>..[% END %]
d52480d @abw *** empty log message ***
authored
483 -- expect --
484 <blip>..<blip>..<blip>..
485
486 -- test --
82cbea8 @abw version 2.00
authored
487 [% FOREACH [ 1 2 3 ] %]<blip>..[% END %]
d52480d @abw *** empty log message ***
authored
488 -- expect --
489 <blip>..<blip>..<blip>..
490
7a87144 @abw added Dumper plugin, fixed DOM, misc other fixes
authored
491 -- test --
492 [% FOREACH outer = nested -%]
493 outer start
494 [% FOREACH inner = outer -%]
495 inner [% inner +%]
496 [% "last inner\n" IF loop.last -%]
497 [% END %]
498 [% "last outer\n" IF loop.last -%]
499 [% END %]
500 -- expect --
501 outer start
502 inner a
503 inner b
504 inner c
505 last inner
506 outer start
507 inner x
508 inner y
509 inner z
510 last inner
511 last outer
512
513
82cbea8 @abw version 2.00
authored
514 -- test --
515 [% FOREACH n = [ 1 2 3 4 5 ] -%]
516 [% LAST IF loop.last -%]
517 [% n %],
518 [%- END %]
519 -- expect --
520 1, 2, 3, 4,
521
522 -- test --
523 [% FOREACH n = [ 1 2 3 4 5 ] -%]
524 [% BREAK IF loop.last -%]
525 [% n %],
526 [%- END %]
527 -- expect --
528 1, 2, 3, 4,
529
fa9c1c8 @abw patch from Eric, FOREACH/DEBUG loop fix
authored
530 -- test --
531 -- use debug --
532 [% FOREACH a = [ 1, 2, 3 ] -%]
533 * [% a %]
534 [% END -%]
535 -- expect --
536 * 1
537 * 2
538 * 3
6c54b0a @abw 2.03b
authored
539 -- test --
540 [%
541 FOREACH i = [1 .. 10];
542 SWITCH i;
543 CASE 5;
544 NEXT;
545 CASE 8;
546 LAST;
547 END;
548 "$i\n";
549 END;
550 -%]
551 -- expect --
552 1
553 2
554 3
555 4
556 6
557 7
558 -- test --
559 [%
560 FOREACH i = [1 .. 10];
561 IF 1;
562 IF i == 5; NEXT; END;
563 IF i == 8; LAST; END;
564 END;
565 "$i\n";
566 END;
567 -%]
568 -- expect --
569 1
570 2
571 3
572 4
573 6
574 7
575 -- test --
576 [%
577 FOREACH i = [1 .. 4];
578 FOREACH j = [1 .. 4];
579 k = 1;
580 SWITCH j;
581 CASE 2;
582 LAST FOREACH k = [ 1 .. 2 ];
583 CASE 3;
584 NEXT IF j == 3;
585 END;
586 "$i,$j,$k\n";
587 END;
588 END;
589 -%]
590 -- expect --
591 1,1,1
592 1,2,1
593 1,4,1
594 2,1,1
595 2,2,1
596 2,4,1
597 3,1,1
598 3,2,1
599 3,4,1
600 4,1,1
601 4,2,1
602 4,4,1
6173dda @abw Added t/debug.t and fixes some DEBUG issues in stash.t and foreach.t
authored
603
6c54b0a @abw 2.03b
authored
604 -- test --
605 [%
606 LAST FOREACH k = [ 1 .. 4];
607 "$k\n";
608 # Should finish loop with k = 4. Instead this is an infinite loop!!
609 #NEXT FOREACH k = [ 1 .. 4];
610 #"$k\n";
611 -%]
612 -- expect --
613 1
Something went wrong with that request. Please try again.