1#============================================================= -*-perl-*-
2#
3# t/binop.t
4#
5# Template script testing the conditional binary operators: and/&&, or/||,
6# not/!, <, >, <=, >= , == and !=.
7#
8# Written by Andy Wardley <abw@kfs.org>
9#
10# Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
11# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
12#
13# This is free software; you can redistribute it and/or modify it
14# under the same terms as Perl itself.
15#
16# $Id$
17#
18#========================================================================
19
20use strict;
21use lib qw( ../lib );
22use Template::Test;
23use Template::Parser;
24$^W = 1;
25
26$Template::Test::DEBUG = 0;
27$Template::Parser::DEBUG = 0;
28
29
30my $counter  = 0;
31my $params   = {
32    'yes'    => 1,
33    'no'     => 0,
34    'true'   => 'this is true',
35    'false'  => '0',
36    'happy'  => 'yes',
37    'sad'    => '',
38    'ten'    => 10,
39    'twenty' => 20,
40    'alpha'  => sub { return ++$counter },
41    'omega'  => sub { $counter += 10; return 0 },
42    'count'  => sub { return $counter },
43    'reset'  => sub { return $counter == 0 },
44};
45
46my $template = Template->new({ INTERPOLATE => 1, POST_CHOMP => 1 });
47
48test_expect(\*DATA, $template, $params);
49
50
51
52__DATA__
53maybe
54[% IF yes %]
55yes
56[% END %]
57-- expect --
58maybe
59yes
60
61-- test --
62[% IF yes %]
63yes
64[% ELSE %]
65no 
66[% END %]
67-- expect --
68yes
69
70-- test --
71[% IF yes %]
72yes
73[% ELSE %]
74no 
75[% END %]
76-- expect --
77yes
78
79-- test --
80[% IF yes and true %]
81yes
82[% ELSE %]
83no 
84[% END %]
85-- expect --
86yes
87
88
89-- test --
90[% IF yes && true %]
91yes
92[% ELSE %]
93no 
94[% END %]
95-- expect --
96yes
97
98-- test --
99[% IF yes && sad || happy %]
100yes
101[% ELSE %]
102no 
103[% END %]
104-- expect --
105yes
106
107-- test --
108[% IF yes AND ten && true and twenty && 30 %]
109yes
110[% ELSE %]
111no
112[% END %]
113-- expect --
114yes
115
116-- test --
117[% IF ! yes %]
118no
119[% ELSE %]
120yes
121[% END %]
122-- expect --
123yes
124
125-- test --
126[% UNLESS yes %]
127no
128[% ELSE %]
129yes
130[% END %]
131-- expect --
132yes
133
134-- test --
135[% "yes" UNLESS no %]
136-- expect --
137yes
138
139
140-- test --
141[% IF ! yes %]
142no
143[% ELSE %]
144yes
145[% END %]
146-- expect --
147yes
148
149-- test --
150[% IF yes || no %]
151yes
152[% ELSE %]
153no
154[% END %]
155-- expect --
156yes
157
158-- test --
159[% IF yes || no || true || false %]
160yes
161[% ELSE %]
162no
163[% END %]
164-- expect --
165yes
166
167-- test --
168[% IF yes or no %]
169yes
170[% ELSE %]
171no
172[% END %]
173-- expect --
174yes
175
176-- test --
177[% IF not false and not sad %]
178yes
179[% ELSE %]
180no
181[% END %]
182-- expect --
183yes
184
185-- test --
186[% IF ten == 10 %]
187yes
188[% ELSE %]
189no
190[% END %]
191-- expect --
192yes
193
194-- test --
195[% IF ten == twenty %]
196I canna break the laws of mathematics, Captain.
197[% ELSIF ten > twenty %]
198Your numerical system is inverted.  Please reboot your Universe.
199[% ELSIF twenty < ten %]
200Your inverted system is numerical.  Please universe your reboot.
201[% ELSE %]
202Normality is restored.  Anything you can't cope with is your own problem.
203[% END %]
204-- expect --
205Normality is restored.  Anything you can't cope with is your own problem.
206
207-- test --
208[% IF ten >= twenty or false %]
209no
210[% ELSIF twenty <= ten  %]
211nope
212[% END %]
213nothing
214-- expect --
215nothing
216
217-- test --
218[% IF ten >= twenty or false %]
219no
220[% ELSIF twenty <= ten  %]
221nope
222[% END %]
223nothing
224-- expect --
225nothing
226
227-- test --
228[% IF ten > twenty %]
229no
230[% ELSIF ten < twenty  %]
231yep
232[% END %]
233-- expect --
234yep
235
236-- test --
237[% IF ten != 10 %]
238no
239[% ELSIF ten == 10  %]
240yep
241[% END %]
242-- expect --
243yep
244
245
246
247#------------------------------------------------------------------------
248# test short-circuit operations
249#------------------------------------------------------------------------
250
251-- test --
252[% IF alpha AND omega %]
253alpha and omega are true
254[% ELSE %]
255alpha and/or omega are not true
256[% END %]
257count: [% count %]
258-- expect --
259alpha and/or omega are not true
260count: 11
261
262-- test --
263[% IF omega AND alpha %]
264omega and alpha are true
265[% ELSE %]
266omega and/or alpha are not true
267[% END %]
268count: [% count %]
269-- expect --
270omega and/or alpha are not true
271count: 21
272
273-- test --
274[% IF alpha OR omega %]
275alpha and/or omega are true
276[% ELSE %]
277neither alpha nor omega are true
278[% END %]
279count: [% count %]
280-- expect --
281alpha and/or omega are true
282count: 22
283
284-- test --
285[% IF omega OR alpha %]
286alpha and/or omega are true
287[% ELSE %]
288neither alpha nor omega are true
289[% END %]
290count: [% count %]
291-- expect --
292alpha and/or omega are true
293count: 33
294
295-- test --
296[% small = 5
297   mid   = 7
298   big   = 10
299   both  = small + big
300   less  = big - mid
301   half  = big / small
302   left  = big % mid
303   mult  = big * small
304%]
305both: [% both +%]
306less: [% less +%]
307half: [% half +%]
308left: [% left +%]
309mult: [% mult +%]
310maxi: [% mult + 2 * 2 +%]
311mega: [% mult * 2 + 2 * 3 %]
312
313-- expect --
314both: 15
315less: 3
316half: 2
317left: 3
318mult: 50
319maxi: 54
320mega: 106
321
322-- test --
323[% 10 mod 4 +%] [% 10 MOD 4 +%]
324[% 10 div 3 %] [% 10 DIV 3 %]
325-- expect --
3262 2
3273 3
328
329
330-- stop -- 
331# this is for testing the lt operator which isn't enabled by default.
332-- test --
333[% IF 'one' lt 'two' -%]
334one is less than two
335[% ELSE -%]
336ERROR!
337[% END -%]
338-- expect --
339one is less than two
340