Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 208 lines (193 sloc) 3.256 kb
e1ad160 @abw *** empty log message ***
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/while.t
4 #
5 # Test the WHILE directive
6 #
7 # Written by Andy Wardley <abw@kfs.org>
8 #
5a89101 @abw Fixed a bug in the parser/grammar to make NEXT/LAST work correctly in…
authored
9 # Copyright (C) 1996-2009 Andy Wardley. All Rights Reserved.
e1ad160 @abw *** empty log message ***
authored
10 #
11 # This is free software; you can redistribute it and/or modify it
12 # under the same terms as Perl itself.
13 #
14 #========================================================================
15
16 use strict;
5a89101 @abw Fixed a bug in the parser/grammar to make NEXT/LAST work correctly in…
authored
17 use warnings;
e1ad160 @abw *** empty log message ***
authored
18 use lib qw( ./lib ../lib );
19 use Template::Test;
20 use Template::Parser;
21 use Template::Directive;
22
23 $Template::Test::DEBUG = 0;
24 #$Template::Parser::DEBUG = 1;
d52480d @abw *** empty log message ***
authored
25 #$Template::Directive::PRETTY = 1;
e1ad160 @abw *** empty log message ***
authored
26
27 # set low limit on WHILE's maximum iteration count
28 $Template::Directive::WHILE_MAX = 100;
29
30 my $config = {
31 INTERPOLATE => 1,
32 POST_CHOMP => 1,
33 };
34
35 my @list = ( 'x-ray', 'yankee', 'zulu', );
36 my @pending;
37 my $replace = {
38 'a' => 'alpha',
39 'b' => 'bravo',
40 'c' => 'charlie',
41 'd' => 'delta',
42 'dec' => sub { --$_[0] },
43 'inc' => sub { ++$_[0] },
44 'reset' => sub { @pending = @list; "Reset list\n" },
45 'next' => sub { shift @pending },
46 'true' => 1,
47 };
48
49 test_expect(\*DATA, $config, $replace);
50
51 __DATA__
52 before
53 [% WHILE bollocks %]
54 do nothing
55 [% END %]
56 after
57 -- expect --
58 before
59 after
60
61 -- test --
62 Commence countdown...
63 [% a = 10 %]
64 [% WHILE a %]
65 [% a %]..[% a = dec(a) %]
66 [% END +%]
67 The end
68 -- expect --
69 Commence countdown...
70 10..9..8..7..6..5..4..3..2..1..
71 The end
72
73 -- test --
74 [% reset %]
75 [% WHILE (item = next) %]
76 item: [% item +%]
77 [% END %]
78 -- expect --
79 Reset list
80 item: x-ray
81 item: yankee
82 item: zulu
83
84 -- test --
85 [% reset %]
86 [% WHILE (item = next) %]
87 item: [% item +%]
88 [% BREAK IF item == 'yankee' %]
89 [% END %]
90 Finis
91 -- expect --
92 Reset list
93 item: x-ray
94 item: yankee
95 Finis
96
97 -- test --
98 [% reset %]
99 [% "* $item\n" WHILE (item = next) %]
100 -- expect --
101 Reset list
102 * x-ray
103 * yankee
104 * zulu
105
106 -- test --
107 [% TRY %]
108 [% WHILE true %].[% END %]
109 [% CATCH +%]
110 error: [% error.info %]
111 [% END %]
112 -- expect --
113 ...................................................................................................
114 error: WHILE loop terminated (> 100 iterations)
115
116
d52480d @abw *** empty log message ***
authored
117 -- test --
118 [% reset %]
119 [% WHILE (item = next) %]
120 [% NEXT IF item == 'yankee' -%]
121 * [% item +%]
122 [% END %]
123 -- expect --
124 Reset list
125 * x-ray
126 * zulu
6c54b0a @abw 2.03b
authored
127 -- test --
128 [%
129 i = 1;
130 WHILE i <= 10;
131 SWITCH i;
132 CASE 5;
133 i = i + 1;
134 NEXT;
135 CASE 8;
136 LAST;
137 END;
138 "$i\n";
139 i = i + 1;
140 END;
141 -%]
142 -- expect --
143 1
144 2
145 3
146 4
147 6
148 7
149 -- test --
150 [%
151 i = 1;
152 WHILE i <= 10;
153 IF 1;
154 IF i == 5; i = i + 1; NEXT; END;
155 IF i == 8; LAST; END;
156 END;
157 "$i\n";
158 i = i + 1;
159 END;
160 -%]
161 -- expect --
162 1
163 2
164 3
165 4
166 6
167 7
168 -- test --
169 [%
170 i = 1;
171 WHILE i <= 4;
172 j = 1;
173 WHILE j <= 4;
174 k = 1;
175 SWITCH j;
176 CASE 2;
5a89101 @abw Fixed a bug in the parser/grammar to make NEXT/LAST work correctly in…
authored
177 WHILE k == 1; LAST; END;
6c54b0a @abw 2.03b
authored
178 CASE 3;
179 IF j == 3; j = j + 1; NEXT; END;
180 END;
181 "$i,$j,$k\n";
182 j = j + 1;
183 END;
184 i = i + 1;
185 END;
186 -%]
187 -- expect --
188 1,1,1
189 1,2,1
190 1,4,1
191 2,1,1
192 2,2,1
193 2,4,1
194 3,1,1
195 3,2,1
196 3,4,1
197 4,1,1
198 4,2,1
199 4,4,1
200 -- test --
201 [%
202 k = 1;
203 LAST WHILE k == 1;
204 "$k\n";
205 -%]
206 -- expect --
207 1
Something went wrong with that request. Please try again.