Debian Codegraph @

tests/template_tests/syntax_tests/test_if.py

raw
1 from django.template import TemplateSyntaxError
2 from django.template.defaulttags import IfNode
3 from django.test import SimpleTestCase
4
5 from ..utils import TestObj, setup
6
7
8 class IfTagTests(SimpleTestCase):
9 @setup({"if-tag01": "{% if foo %}yes{% else %}no{% endif %}"})
10 def test_if_tag01(self):
11 output = self.engine.render_to_string("if-tag01", {"foo": True})
12 self.assertEqual(output, "yes")
13
14 @setup({"if-tag02": "{% if foo %}yes{% else %}no{% endif %}"})
15 def test_if_tag02(self):
16 output = self.engine.render_to_string("if-tag02", {"foo": False})
17 self.assertEqual(output, "no")
18
19 @setup({"if-tag03": "{% if foo %}yes{% else %}no{% endif %}"})
20 def test_if_tag03(self):
21 output = self.engine.render_to_string("if-tag03")
22 self.assertEqual(output, "no")
23
24 @setup({"if-tag04": "{% if foo %}foo{% elif bar %}bar{% endif %}"})
25 def test_if_tag04(self):
26 output = self.engine.render_to_string("if-tag04", {"foo": True})
27 self.assertEqual(output, "foo")
28
29 @setup({"if-tag05": "{% if foo %}foo{% elif bar %}bar{% endif %}"})
30 def test_if_tag05(self):
31 output = self.engine.render_to_string("if-tag05", {"bar": True})
32 self.assertEqual(output, "bar")
33
34 @setup({"if-tag06": "{% if foo %}foo{% elif bar %}bar{% endif %}"})
35 def test_if_tag06(self):
36 output = self.engine.render_to_string("if-tag06")
37 self.assertEqual(output, "")
38
39 @setup({"if-tag07": "{% if foo %}foo{% elif bar %}bar{% else %}nothing{% endif %}"})
40 def test_if_tag07(self):
41 output = self.engine.render_to_string("if-tag07", {"foo": True})
42 self.assertEqual(output, "foo")
43
44 @setup({"if-tag08": "{% if foo %}foo{% elif bar %}bar{% else %}nothing{% endif %}"})
45 def test_if_tag08(self):
46 output = self.engine.render_to_string("if-tag08", {"bar": True})
47 self.assertEqual(output, "bar")
48
49 @setup({"if-tag09": "{% if foo %}foo{% elif bar %}bar{% else %}nothing{% endif %}"})
50 def test_if_tag09(self):
51 output = self.engine.render_to_string("if-tag09")
52 self.assertEqual(output, "nothing")
53
54 @setup(
55 {
56 "if-tag10": (
57 "{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing"
58 "{% endif %}"
59 )
60 }
61 )
62 def test_if_tag10(self):
63 output = self.engine.render_to_string("if-tag10", {"foo": True})
64 self.assertEqual(output, "foo")
65
66 @setup(
67 {
68 "if-tag11": (
69 "{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing"
70 "{% endif %}"
71 )
72 }
73 )
74 def test_if_tag11(self):
75 output = self.engine.render_to_string("if-tag11", {"bar": True})
76 self.assertEqual(output, "bar")
77
78 @setup(
79 {
80 "if-tag12": (
81 "{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing"
82 "{% endif %}"
83 )
84 }
85 )
86 def test_if_tag12(self):
87 output = self.engine.render_to_string("if-tag12", {"baz": True})
88 self.assertEqual(output, "baz")
89
90 @setup(
91 {
92 "if-tag13": (
93 "{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing"
94 "{% endif %}"
95 )
96 }
97 )
98 def test_if_tag13(self):
99 output = self.engine.render_to_string("if-tag13")
100 self.assertEqual(output, "nothing")
101
102 # Filters
103 @setup({"if-tag-filter01": "{% if foo|length == 5 %}yes{% else %}no{% endif %}"})
104 def test_if_tag_filter01(self):
105 output = self.engine.render_to_string("if-tag-filter01", {"foo": "abcde"})
106 self.assertEqual(output, "yes")
107
108 @setup({"if-tag-filter02": "{% if foo|upper == 'ABC' %}yes{% else %}no{% endif %}"})
109 def test_if_tag_filter02(self):
110 output = self.engine.render_to_string("if-tag-filter02")
111 self.assertEqual(output, "no")
112
113 # Equality
114 @setup({"if-tag-eq01": "{% if foo == bar %}yes{% else %}no{% endif %}"})
115 def test_if_tag_eq01(self):
116 output = self.engine.render_to_string("if-tag-eq01")
117 self.assertEqual(output, "yes")
118
119 @setup({"if-tag-eq02": "{% if foo == bar %}yes{% else %}no{% endif %}"})
120 def test_if_tag_eq02(self):
121 output = self.engine.render_to_string("if-tag-eq02", {"foo": 1})
122 self.assertEqual(output, "no")
123
124 @setup({"if-tag-eq03": "{% if foo == bar %}yes{% else %}no{% endif %}"})
125 def test_if_tag_eq03(self):
126 output = self.engine.render_to_string("if-tag-eq03", {"foo": 1, "bar": 1})
127 self.assertEqual(output, "yes")
128
129 @setup({"if-tag-eq04": "{% if foo == bar %}yes{% else %}no{% endif %}"})
130 def test_if_tag_eq04(self):
131 output = self.engine.render_to_string("if-tag-eq04", {"foo": 1, "bar": 2})
132 self.assertEqual(output, "no")
133
134 @setup({"if-tag-eq05": "{% if foo == '' %}yes{% else %}no{% endif %}"})
135 def test_if_tag_eq05(self):
136 output = self.engine.render_to_string("if-tag-eq05")
137 self.assertEqual(output, "no")
138
139 # Inequality
140 @setup({"if-tag-noteq01": "{% if foo != bar %}yes{% else %}no{% endif %}"})
141 def test_if_tag_noteq01(self):
142 output = self.engine.render_to_string("if-tag-noteq01")
143 self.assertEqual(output, "no")
144
145 @setup({"if-tag-noteq02": "{% if foo != bar %}yes{% else %}no{% endif %}"})
146 def test_if_tag_noteq02(self):
147 output = self.engine.render_to_string("if-tag-noteq02", {"foo": 1})
148 self.assertEqual(output, "yes")
149
150 @setup({"if-tag-noteq03": "{% if foo != bar %}yes{% else %}no{% endif %}"})
151 def test_if_tag_noteq03(self):
152 output = self.engine.render_to_string("if-tag-noteq03", {"foo": 1, "bar": 1})
153 self.assertEqual(output, "no")
154
155 @setup({"if-tag-noteq04": "{% if foo != bar %}yes{% else %}no{% endif %}"})
156 def test_if_tag_noteq04(self):
157 output = self.engine.render_to_string("if-tag-noteq04", {"foo": 1, "bar": 2})
158 self.assertEqual(output, "yes")
159
160 @setup({"if-tag-noteq05": '{% if foo != "" %}yes{% else %}no{% endif %}'})
161 def test_if_tag_noteq05(self):
162 output = self.engine.render_to_string("if-tag-noteq05")
163 self.assertEqual(output, "yes")
164
165 # Comparison
166 @setup({"if-tag-gt-01": "{% if 2 > 1 %}yes{% else %}no{% endif %}"})
167 def test_if_tag_gt_01(self):
168 output = self.engine.render_to_string("if-tag-gt-01")
169 self.assertEqual(output, "yes")
170
171 @setup({"if-tag-gt-02": "{% if 1 > 1 %}yes{% else %}no{% endif %}"})
172 def test_if_tag_gt_02(self):
173 output = self.engine.render_to_string("if-tag-gt-02")
174 self.assertEqual(output, "no")
175
176 @setup({"if-tag-gte-01": "{% if 1 >= 1 %}yes{% else %}no{% endif %}"})
177 def test_if_tag_gte_01(self):
178 output = self.engine.render_to_string("if-tag-gte-01")
179 self.assertEqual(output, "yes")
180
181 @setup({"if-tag-gte-02": "{% if 1 >= 2 %}yes{% else %}no{% endif %}"})
182 def test_if_tag_gte_02(self):
183 output = self.engine.render_to_string("if-tag-gte-02")
184 self.assertEqual(output, "no")
185
186 @setup({"if-tag-lt-01": "{% if 1 < 2 %}yes{% else %}no{% endif %}"})
187 def test_if_tag_lt_01(self):
188 output = self.engine.render_to_string("if-tag-lt-01")
189 self.assertEqual(output, "yes")
190
191 @setup({"if-tag-lt-02": "{% if 1 < 1 %}yes{% else %}no{% endif %}"})
192 def test_if_tag_lt_02(self):
193 output = self.engine.render_to_string("if-tag-lt-02")
194 self.assertEqual(output, "no")
195
196 @setup({"if-tag-lte-01": "{% if 1 <= 1 %}yes{% else %}no{% endif %}"})
197 def test_if_tag_lte_01(self):
198 output = self.engine.render_to_string("if-tag-lte-01")
199 self.assertEqual(output, "yes")
200
201 @setup({"if-tag-lte-02": "{% if 2 <= 1 %}yes{% else %}no{% endif %}"})
202 def test_if_tag_lte_02(self):
203 output = self.engine.render_to_string("if-tag-lte-02")
204 self.assertEqual(output, "no")
205
206 # Contains
207 @setup({"if-tag-in-01": "{% if 1 in x %}yes{% else %}no{% endif %}"})
208 def test_if_tag_in_01(self):
209 output = self.engine.render_to_string("if-tag-in-01", {"x": [1]})
210 self.assertEqual(output, "yes")
211
212 @setup({"if-tag-in-02": "{% if 2 in x %}yes{% else %}no{% endif %}"})
213 def test_if_tag_in_02(self):
214 output = self.engine.render_to_string("if-tag-in-02", {"x": [1]})
215 self.assertEqual(output, "no")
216
217 @setup({"if-tag-not-in-01": "{% if 1 not in x %}yes{% else %}no{% endif %}"})
218 def test_if_tag_not_in_01(self):
219 output = self.engine.render_to_string("if-tag-not-in-01", {"x": [1]})
220 self.assertEqual(output, "no")
221
222 @setup({"if-tag-not-in-02": "{% if 2 not in x %}yes{% else %}no{% endif %}"})
223 def test_if_tag_not_in_02(self):
224 output = self.engine.render_to_string("if-tag-not-in-02", {"x": [1]})
225 self.assertEqual(output, "yes")
226
227 # AND
228 @setup({"if-tag-and01": "{% if foo and bar %}yes{% else %}no{% endif %}"})
229 def test_if_tag_and01(self):
230 output = self.engine.render_to_string(
231 "if-tag-and01", {"foo": True, "bar": True}
232 )
233 self.assertEqual(output, "yes")
234
235 @setup({"if-tag-and02": "{% if foo and bar %}yes{% else %}no{% endif %}"})
236 def test_if_tag_and02(self):
237 output = self.engine.render_to_string(
238 "if-tag-and02", {"foo": True, "bar": False}
239 )
240 self.assertEqual(output, "no")
241
242 @setup({"if-tag-and03": "{% if foo and bar %}yes{% else %}no{% endif %}"})
243 def test_if_tag_and03(self):
244 output = self.engine.render_to_string(
245 "if-tag-and03", {"foo": False, "bar": True}
246 )
247 self.assertEqual(output, "no")
248
249 @setup({"if-tag-and04": "{% if foo and bar %}yes{% else %}no{% endif %}"})
250 def test_if_tag_and04(self):
251 output = self.engine.render_to_string(
252 "if-tag-and04", {"foo": False, "bar": False}
253 )
254 self.assertEqual(output, "no")
255
256 @setup({"if-tag-and05": "{% if foo and bar %}yes{% else %}no{% endif %}"})
257 def test_if_tag_and05(self):
258 output = self.engine.render_to_string("if-tag-and05", {"foo": False})
259 self.assertEqual(output, "no")
260
261 @setup({"if-tag-and06": "{% if foo and bar %}yes{% else %}no{% endif %}"})
262 def test_if_tag_and06(self):
263 output = self.engine.render_to_string("if-tag-and06", {"bar": False})
264 self.assertEqual(output, "no")
265
266 @setup({"if-tag-and07": "{% if foo and bar %}yes{% else %}no{% endif %}"})
267 def test_if_tag_and07(self):
268 output = self.engine.render_to_string("if-tag-and07", {"foo": True})
269 self.assertEqual(output, "no")
270
271 @setup({"if-tag-and08": "{% if foo and bar %}yes{% else %}no{% endif %}"})
272 def test_if_tag_and08(self):
273 output = self.engine.render_to_string("if-tag-and08", {"bar": True})
274 self.assertEqual(output, "no")
275
276 # OR
277 @setup({"if-tag-or01": "{% if foo or bar %}yes{% else %}no{% endif %}"})
278 def test_if_tag_or01(self):
279 output = self.engine.render_to_string("if-tag-or01", {"foo": True, "bar": True})
280 self.assertEqual(output, "yes")
281
282 @setup({"if-tag-or02": "{% if foo or bar %}yes{% else %}no{% endif %}"})
283 def test_if_tag_or02(self):
284 output = self.engine.render_to_string(
285 "if-tag-or02", {"foo": True, "bar": False}
286 )
287 self.assertEqual(output, "yes")
288
289 @setup({"if-tag-or03": "{% if foo or bar %}yes{% else %}no{% endif %}"})
290 def test_if_tag_or03(self):
291 output = self.engine.render_to_string(
292 "if-tag-or03", {"foo": False, "bar": True}
293 )
294 self.assertEqual(output, "yes")
295
296 @setup({"if-tag-or04": "{% if foo or bar %}yes{% else %}no{% endif %}"})
297 def test_if_tag_or04(self):
298 output = self.engine.render_to_string(
299 "if-tag-or04", {"foo": False, "bar": False}
300 )
301 self.assertEqual(output, "no")
302
303 @setup({"if-tag-or05": "{% if foo or bar %}yes{% else %}no{% endif %}"})
304 def test_if_tag_or05(self):
305 output = self.engine.render_to_string("if-tag-or05", {"foo": False})
306 self.assertEqual(output, "no")
307
308 @setup({"if-tag-or06": "{% if foo or bar %}yes{% else %}no{% endif %}"})
309 def test_if_tag_or06(self):
310 output = self.engine.render_to_string("if-tag-or06", {"bar": False})
311 self.assertEqual(output, "no")
312
313 @setup({"if-tag-or07": "{% if foo or bar %}yes{% else %}no{% endif %}"})
314 def test_if_tag_or07(self):
315 output = self.engine.render_to_string("if-tag-or07", {"foo": True})
316 self.assertEqual(output, "yes")
317
318 @setup({"if-tag-or08": "{% if foo or bar %}yes{% else %}no{% endif %}"})
319 def test_if_tag_or08(self):
320 output = self.engine.render_to_string("if-tag-or08", {"bar": True})
321 self.assertEqual(output, "yes")
322
323 @setup({"if-tag-or09": "{% if foo or bar or baz %}yes{% else %}no{% endif %}"})
324 def test_if_tag_or09(self):
325 """
326 multiple ORs
327 """
328 output = self.engine.render_to_string("if-tag-or09", {"baz": True})
329 self.assertEqual(output, "yes")
330
331 # NOT
332 @setup({"if-tag-not01": "{% if not foo %}no{% else %}yes{% endif %}"})
333 def test_if_tag_not01(self):
334 output = self.engine.render_to_string("if-tag-not01", {"foo": True})
335 self.assertEqual(output, "yes")
336
337 @setup({"if-tag-not02": "{% if not not foo %}no{% else %}yes{% endif %}"})
338 def test_if_tag_not02(self):
339 output = self.engine.render_to_string("if-tag-not02", {"foo": True})
340 self.assertEqual(output, "no")
341
342 @setup({"if-tag-not06": "{% if foo and not bar %}yes{% else %}no{% endif %}"})
343 def test_if_tag_not06(self):
344 output = self.engine.render_to_string("if-tag-not06")
345 self.assertEqual(output, "no")
346
347 @setup({"if-tag-not07": "{% if foo and not bar %}yes{% else %}no{% endif %}"})
348 def test_if_tag_not07(self):
349 output = self.engine.render_to_string(
350 "if-tag-not07", {"foo": True, "bar": True}
351 )
352 self.assertEqual(output, "no")
353
354 @setup({"if-tag-not08": "{% if foo and not bar %}yes{% else %}no{% endif %}"})
355 def test_if_tag_not08(self):
356 output = self.engine.render_to_string(
357 "if-tag-not08", {"foo": True, "bar": False}
358 )
359 self.assertEqual(output, "yes")
360
361 @setup({"if-tag-not09": "{% if foo and not bar %}yes{% else %}no{% endif %}"})
362 def test_if_tag_not09(self):
363 output = self.engine.render_to_string(
364 "if-tag-not09", {"foo": False, "bar": True}
365 )
366 self.assertEqual(output, "no")
367
368 @setup({"if-tag-not10": "{% if foo and not bar %}yes{% else %}no{% endif %}"})
369 def test_if_tag_not10(self):
370 output = self.engine.render_to_string(
371 "if-tag-not10", {"foo": False, "bar": False}
372 )
373 self.assertEqual(output, "no")
374
375 @setup({"if-tag-not11": "{% if not foo and bar %}yes{% else %}no{% endif %}"})
376 def test_if_tag_not11(self):
377 output = self.engine.render_to_string("if-tag-not11")
378 self.assertEqual(output, "no")
379
380 @setup({"if-tag-not12": "{% if not foo and bar %}yes{% else %}no{% endif %}"})
381 def test_if_tag_not12(self):
382 output = self.engine.render_to_string(
383 "if-tag-not12", {"foo": True, "bar": True}
384 )
385 self.assertEqual(output, "no")
386
387 @setup({"if-tag-not13": "{% if not foo and bar %}yes{% else %}no{% endif %}"})
388 def test_if_tag_not13(self):
389 output = self.engine.render_to_string(
390 "if-tag-not13", {"foo": True, "bar": False}
391 )
392 self.assertEqual(output, "no")
393
394 @setup({"if-tag-not14": "{% if not foo and bar %}yes{% else %}no{% endif %}"})
395 def test_if_tag_not14(self):
396 output = self.engine.render_to_string(
397 "if-tag-not14", {"foo": False, "bar": True}
398 )
399 self.assertEqual(output, "yes")
400
401 @setup({"if-tag-not15": "{% if not foo and bar %}yes{% else %}no{% endif %}"})
402 def test_if_tag_not15(self):
403 output = self.engine.render_to_string(
404 "if-tag-not15", {"foo": False, "bar": False}
405 )
406 self.assertEqual(output, "no")
407
408 @setup({"if-tag-not16": "{% if foo or not bar %}yes{% else %}no{% endif %}"})
409 def test_if_tag_not16(self):
410 output = self.engine.render_to_string("if-tag-not16")
411 self.assertEqual(output, "yes")
412
413 @setup({"if-tag-not17": "{% if foo or not bar %}yes{% else %}no{% endif %}"})
414 def test_if_tag_not17(self):
415 output = self.engine.render_to_string(
416 "if-tag-not17", {"foo": True, "bar": True}
417 )
418 self.assertEqual(output, "yes")
419
420 @setup({"if-tag-not18": "{% if foo or not bar %}yes{% else %}no{% endif %}"})
421 def test_if_tag_not18(self):
422 output = self.engine.render_to_string(
423 "if-tag-not18", {"foo": True, "bar": False}
424 )
425 self.assertEqual(output, "yes")
426
427 @setup({"if-tag-not19": "{% if foo or not bar %}yes{% else %}no{% endif %}"})
428 def test_if_tag_not19(self):
429 output = self.engine.render_to_string(
430 "if-tag-not19", {"foo": False, "bar": True}
431 )
432 self.assertEqual(output, "no")
433
434 @setup({"if-tag-not20": "{% if foo or not bar %}yes{% else %}no{% endif %}"})
435 def test_if_tag_not20(self):
436 output = self.engine.render_to_string(
437 "if-tag-not20", {"foo": False, "bar": False}
438 )
439 self.assertEqual(output, "yes")
440
441 @setup({"if-tag-not21": "{% if not foo or bar %}yes{% else %}no{% endif %}"})
442 def test_if_tag_not21(self):
443 output = self.engine.render_to_string("if-tag-not21")
444 self.assertEqual(output, "yes")
445
446 @setup({"if-tag-not22": "{% if not foo or bar %}yes{% else %}no{% endif %}"})
447 def test_if_tag_not22(self):
448 output = self.engine.render_to_string(
449 "if-tag-not22", {"foo": True, "bar": True}
450 )
451 self.assertEqual(output, "yes")
452
453 @setup({"if-tag-not23": "{% if not foo or bar %}yes{% else %}no{% endif %}"})
454 def test_if_tag_not23(self):
455 output = self.engine.render_to_string(
456 "if-tag-not23", {"foo": True, "bar": False}
457 )
458 self.assertEqual(output, "no")
459
460 @setup({"if-tag-not24": "{% if not foo or bar %}yes{% else %}no{% endif %}"})
461 def test_if_tag_not24(self):
462 output = self.engine.render_to_string(
463 "if-tag-not24", {"foo": False, "bar": True}
464 )
465 self.assertEqual(output, "yes")
466
467 @setup({"if-tag-not25": "{% if not foo or bar %}yes{% else %}no{% endif %}"})
468 def test_if_tag_not25(self):
469 output = self.engine.render_to_string(
470 "if-tag-not25", {"foo": False, "bar": False}
471 )
472 self.assertEqual(output, "yes")
473
474 @setup({"if-tag-not26": "{% if not foo and not bar %}yes{% else %}no{% endif %}"})
475 def test_if_tag_not26(self):
476 output = self.engine.render_to_string("if-tag-not26")
477 self.assertEqual(output, "yes")
478
479 @setup({"if-tag-not27": "{% if not foo and not bar %}yes{% else %}no{% endif %}"})
480 def test_if_tag_not27(self):
481 output = self.engine.render_to_string(
482 "if-tag-not27", {"foo": True, "bar": True}
483 )
484 self.assertEqual(output, "no")
485
486 @setup({"if-tag-not28": "{% if not foo and not bar %}yes{% else %}no{% endif %}"})
487 def test_if_tag_not28(self):
488 output = self.engine.render_to_string(
489 "if-tag-not28", {"foo": True, "bar": False}
490 )
491 self.assertEqual(output, "no")
492
493 @setup({"if-tag-not29": "{% if not foo and not bar %}yes{% else %}no{% endif %}"})
494 def test_if_tag_not29(self):
495 output = self.engine.render_to_string(
496 "if-tag-not29", {"foo": False, "bar": True}
497 )
498 self.assertEqual(output, "no")
499
500 @setup({"if-tag-not30": "{% if not foo and not bar %}yes{% else %}no{% endif %}"})
501 def test_if_tag_not30(self):
502 output = self.engine.render_to_string(
503 "if-tag-not30", {"foo": False, "bar": False}
504 )
505 self.assertEqual(output, "yes")
506
507 @setup({"if-tag-not31": "{% if not foo or not bar %}yes{% else %}no{% endif %}"})
508 def test_if_tag_not31(self):
509 output = self.engine.render_to_string("if-tag-not31")
510 self.assertEqual(output, "yes")
511
512 @setup({"if-tag-not32": "{% if not foo or not bar %}yes{% else %}no{% endif %}"})
513 def test_if_tag_not32(self):
514 output = self.engine.render_to_string(
515 "if-tag-not32", {"foo": True, "bar": True}
516 )
517 self.assertEqual(output, "no")
518
519 @setup({"if-tag-not33": "{% if not foo or not bar %}yes{% else %}no{% endif %}"})
520 def test_if_tag_not33(self):
521 output = self.engine.render_to_string(
522 "if-tag-not33", {"foo": True, "bar": False}
523 )
524 self.assertEqual(output, "yes")
525
526 @setup({"if-tag-not34": "{% if not foo or not bar %}yes{% else %}no{% endif %}"})
527 def test_if_tag_not34(self):
528 output = self.engine.render_to_string(
529 "if-tag-not34", {"foo": False, "bar": True}
530 )
531 self.assertEqual(output, "yes")
532
533 @setup({"if-tag-not35": "{% if not foo or not bar %}yes{% else %}no{% endif %}"})
534 def test_if_tag_not35(self):
535 output = self.engine.render_to_string(
536 "if-tag-not35", {"foo": False, "bar": False}
537 )
538 self.assertEqual(output, "yes")
539
540 # Various syntax errors
541 @setup({"if-tag-error01": "{% if %}yes{% endif %}"})
542 def test_if_tag_error01(self):
543 with self.assertRaises(TemplateSyntaxError):
544 self.engine.get_template("if-tag-error01")
545
546 @setup({"if-tag-error02": "{% if foo and %}yes{% else %}no{% endif %}"})
547 def test_if_tag_error02(self):
548 with self.assertRaises(TemplateSyntaxError):
549 self.engine.render_to_string("if-tag-error02", {"foo": True})
550
551 @setup({"if-tag-error03": "{% if foo or %}yes{% else %}no{% endif %}"})
552 def test_if_tag_error03(self):
553 with self.assertRaises(TemplateSyntaxError):
554 self.engine.render_to_string("if-tag-error03", {"foo": True})
555
556 @setup({"if-tag-error04": "{% if not foo and %}yes{% else %}no{% endif %}"})
557 def test_if_tag_error04(self):
558 with self.assertRaises(TemplateSyntaxError):
559 self.engine.render_to_string("if-tag-error04", {"foo": True})
560
561 @setup({"if-tag-error05": "{% if not foo or %}yes{% else %}no{% endif %}"})
562 def test_if_tag_error05(self):
563 with self.assertRaises(TemplateSyntaxError):
564 self.engine.render_to_string("if-tag-error05", {"foo": True})
565
566 @setup({"if-tag-error06": "{% if abc def %}yes{% endif %}"})
567 def test_if_tag_error06(self):
568 with self.assertRaises(TemplateSyntaxError):
569 self.engine.get_template("if-tag-error06")
570
571 @setup({"if-tag-error07": "{% if not %}yes{% endif %}"})
572 def test_if_tag_error07(self):
573 with self.assertRaises(TemplateSyntaxError):
574 self.engine.get_template("if-tag-error07")
575
576 @setup({"if-tag-error08": "{% if and %}yes{% endif %}"})
577 def test_if_tag_error08(self):
578 with self.assertRaises(TemplateSyntaxError):
579 self.engine.get_template("if-tag-error08")
580
581 @setup({"if-tag-error09": "{% if or %}yes{% endif %}"})
582 def test_if_tag_error09(self):
583 with self.assertRaises(TemplateSyntaxError):
584 self.engine.get_template("if-tag-error09")
585
586 @setup({"if-tag-error10": "{% if == %}yes{% endif %}"})
587 def test_if_tag_error10(self):
588 with self.assertRaises(TemplateSyntaxError):
589 self.engine.get_template("if-tag-error10")
590
591 @setup({"if-tag-error11": "{% if 1 == %}yes{% endif %}"})
592 def test_if_tag_error11(self):
593 with self.assertRaises(TemplateSyntaxError):
594 self.engine.get_template("if-tag-error11")
595
596 @setup({"if-tag-error12": "{% if a not b %}yes{% endif %}"})
597 def test_if_tag_error12(self):
598 with self.assertRaises(TemplateSyntaxError):
599 self.engine.get_template("if-tag-error12")
600
601 @setup(
602 {
603 "else-if-tag-error01": (
604 "{% if foo is bar %} yes {% else if foo is not bar %} no {% endif %}"
605 )
606 }
607 )
608 def test_else_if_tag_error01(self):
609 error_message = 'Malformed template tag at line 1: "else if foo is not bar"'
610 with self.assertRaisesMessage(TemplateSyntaxError, error_message):
611 self.engine.get_template("else-if-tag-error01")
612
613 @setup(
614 {
615 "if-tag-shortcircuit01": (
616 "{% if x.is_true or x.is_bad %}yes{% else %}no{% endif %}"
617 )
618 }
619 )
620 def test_if_tag_shortcircuit01(self):
621 """
622 If evaluations are shortcircuited where possible
623 """
624 output = self.engine.render_to_string("if-tag-shortcircuit01", {"x": TestObj()})
625 self.assertEqual(output, "yes")
626
627 @setup(
628 {
629 "if-tag-shortcircuit02": (
630 "{% if x.is_false and x.is_bad %}yes{% else %}no{% endif %}"
631 )
632 }
633 )
634 def test_if_tag_shortcircuit02(self):
635 """
636 The is_bad() function should not be evaluated. If it is, an
637 exception is raised.
638 """
639 output = self.engine.render_to_string("if-tag-shortcircuit02", {"x": TestObj()})
640 self.assertEqual(output, "no")
641
642 @setup({"if-tag-badarg01": "{% if x|default_if_none:y %}yes{% endif %}"})
643 def test_if_tag_badarg01(self):
644 """Nonexistent args"""
645 output = self.engine.render_to_string("if-tag-badarg01")
646 self.assertEqual(output, "")
647
648 @setup({"if-tag-badarg02": "{% if x|default_if_none:y %}yes{% endif %}"})
649 def test_if_tag_badarg02(self):
650 output = self.engine.render_to_string("if-tag-badarg02", {"y": 0})
651 self.assertEqual(output, "")
652
653 @setup({"if-tag-badarg03": "{% if x|default_if_none:y %}yes{% endif %}"})
654 def test_if_tag_badarg03(self):
655 output = self.engine.render_to_string("if-tag-badarg03", {"y": 1})
656 self.assertEqual(output, "yes")
657
658 @setup(
659 {"if-tag-badarg04": "{% if x|default_if_none:y %}yes{% else %}no{% endif %}"}
660 )
661 def test_if_tag_badarg04(self):
662 output = self.engine.render_to_string("if-tag-badarg04")
663 self.assertEqual(output, "no")
664
665 @setup({"if-tag-single-eq": "{% if foo = bar %}yes{% else %}no{% endif %}"})
666 def test_if_tag_single_eq(self):
667 # A single equals sign is a syntax error.
668 with self.assertRaises(TemplateSyntaxError):
669 self.engine.render_to_string("if-tag-single-eq", {"foo": 1})
670
671 @setup({"template": "{% if foo is True %}yes{% else %}no{% endif %}"})
672 def test_if_is_match(self):
673 output = self.engine.render_to_string("template", {"foo": True})
674 self.assertEqual(output, "yes")
675
676 @setup({"template": "{% if foo is True %}yes{% else %}no{% endif %}"})
677 def test_if_is_no_match(self):
678 output = self.engine.render_to_string("template", {"foo": 1})
679 self.assertEqual(output, "no")
680
681 @setup({"template": "{% if foo is bar %}yes{% else %}no{% endif %}"})
682 def test_if_is_variable_missing(self):
683 output = self.engine.render_to_string("template", {"foo": 1})
684 self.assertEqual(output, "no")
685
686 @setup({"template": "{% if foo is bar %}yes{% else %}no{% endif %}"})
687 def test_if_is_both_variables_missing(self):
688 output = self.engine.render_to_string("template", {})
689 self.assertEqual(output, "yes")
690
691 @setup({"template": "{% if foo is not None %}yes{% else %}no{% endif %}"})
692 def test_if_is_not_match(self):
693 # For this to act as a regression test, it's important not to use
694 # foo=True because True is (not None)
695 output = self.engine.render_to_string("template", {"foo": False})
696 self.assertEqual(output, "yes")
697
698 @setup({"template": "{% if foo is not None %}yes{% else %}no{% endif %}"})
699 def test_if_is_not_no_match(self):
700 output = self.engine.render_to_string("template", {"foo": None})
701 self.assertEqual(output, "no")
702
703 @setup({"template": "{% if foo is not bar %}yes{% else %}no{% endif %}"})
704 def test_if_is_not_variable_missing(self):
705 output = self.engine.render_to_string("template", {"foo": False})
706 self.assertEqual(output, "yes")
707
708 @setup({"template": "{% if foo is not bar %}yes{% else %}no{% endif %}"})
709 def test_if_is_not_both_variables_missing(self):
710 output = self.engine.render_to_string("template", {})
711 self.assertEqual(output, "no")
712
713
714 class IfNodeTests(SimpleTestCase):
715 def test_repr(self):
716 node = IfNode(conditions_nodelists=[])
717 self.assertEqual(repr(node), "<IfNode>")