Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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