133
144
'basic-syntax31': (r'{{ var|default_if_none:var2 }}', {"var": None, "var2": "happy"}, 'happy'),
135
146
# Default argument testing
136
'basic-syntax32' : (r'{{ var|yesno:"yup,nup,mup" }} {{ var|yesno }}', {"var": True}, 'yup yes'),
138
### IF TAG ################################################################
139
'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"),
140
'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"),
141
'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"),
147
'basic-syntax32': (r'{{ var|yesno:"yup,nup,mup" }} {{ var|yesno }}', {"var": True}, 'yup yes'),
149
# Fail silently for methods that raise an exception with a "silent_variable_failure" attribute
150
'basic-syntax33': (r'1{{ var.method3 }}2', {"var": SomeClass()}, "12"),
152
# In methods that raise an exception without a "silent_variable_attribute" set to True,
153
# the exception propogates
154
'basic-syntax34': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException),
156
# Escaped backslash in argument
157
'basic-syntax35': (r'{{ var|default_if_none:"foo\bar" }}', {"var": None}, r'foo\bar'),
159
# Escaped backslash using known escape char
160
'basic-syntax35': (r'{{ var|default_if_none:"foo\now" }}', {"var": None}, r'foo\now'),
143
162
### COMMENT TAG ###########################################################
144
163
'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"),
149
168
'comment-tag04': ("foo{% comment %} {% endblock %} {% endcomment %}", {}, "foo"),
150
169
'comment-tag05': ("foo{% comment %} {% somerandomtag %} {% endcomment %}", {}, "foo"),
171
### CYCLE TAG #############################################################
172
#'cycleXX': ('', {}, ''),
173
'cycle01': ('{% cycle a, %}', {}, 'a'),
174
'cycle02': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'),
175
'cycle03': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'),
176
'cycle04': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'),
177
'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
178
'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
179
'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError),
181
### EXCEPTIONS ############################################################
183
# Raise exception for invalid template name
184
'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
186
# Raise exception for invalid template name (in variable)
187
'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
189
# Raise exception for extra {% extends %} tags
190
'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
192
# Raise exception for custom tags used in child with {% load %} tag in parent, not in child
193
'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
195
### FILTER TAG ############################################################
196
#'filterXX': ('', {}, ''),
197
'filter01': ('{% filter upper %}{% endfilter %}', {}, ''),
198
'filter02': ('{% filter upper %}django{% endfilter %}', {}, 'DJANGO'),
199
'filter03': ('{% filter upper|lower %}django{% endfilter %}', {}, 'django'),
201
### FIRSTOF TAG ###########################################################
202
#'firstofXX': ('', {}, ''),
203
'firstof01': ('{% firstof a b c %}', {'a':0,'b':0,'c':0}, ''),
204
'firstof02': ('{% firstof a b c %}', {'a':1,'b':0,'c':0}, '1'),
205
'firstof03': ('{% firstof a b c %}', {'a':0,'b':2,'c':0}, '2'),
206
'firstof04': ('{% firstof a b c %}', {'a':0,'b':0,'c':3}, '3'),
207
'firstof05': ('{% firstof a b c %}', {'a':1,'b':2,'c':3}, '1'),
208
'firstof06': ('{% firstof %}', {}, template.TemplateSyntaxError),
152
210
### FOR TAG ###############################################################
153
211
'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"),
154
212
'for-tag02': ("{% for val in values reversed %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "321"),
157
215
'for-tag-vars03': ("{% for val in values %}{{ forloop.revcounter }}{% endfor %}", {"values": [6, 6, 6]}, "321"),
158
216
'for-tag-vars04': ("{% for val in values %}{{ forloop.revcounter0 }}{% endfor %}", {"values": [6, 6, 6]}, "210"),
218
### IF TAG ################################################################
219
'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"),
220
'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"),
221
'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"),
223
### IFCHANGED TAG #########################################################
224
#'ifchangedXX': ('', {}, ''),
225
'ifchanged01': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', { 'num': (1,2,3) }, '123'),
226
'ifchanged02': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', { 'num': (1,1,3) }, '13'),
227
'ifchanged03': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', { 'num': (1,1,1) }, '1'),
160
229
### IFEQUAL TAG ###########################################################
161
230
'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""),
162
231
'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
169
238
'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
170
239
'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),
173
'ifequal11': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {}, "no"),
174
'ifequal12': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": 1}, "yes"),
175
'ifequal13': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": "1"}, "no"),
176
'ifequal14': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": 1}, "no"),
177
'ifequal15': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": "1"}, "yes"),
180
'ifequal16': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {}, "no"),
181
'ifequal17': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "yes"),
182
'ifequal18': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "no"),
183
'ifequal19': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "no"),
184
'ifequal20': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "yes"),
186
241
### IFNOTEQUAL TAG ########################################################
187
242
'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
188
243
'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),
266
321
# Three-level inheritance with {{ block.super }} from parent and grandparent
267
322
'inheritance23': ("{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}", {}, '1_ab3_'),
269
### EXCEPTIONS ############################################################
271
# Raise exception for invalid template name
272
'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
274
# Raise exception for invalid template name (in variable)
275
'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
277
# Raise exception for extra {% extends %} tags
278
'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
280
# Raise exception for custom tags used in child with {% load %} tag in parent, not in child
281
'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
324
### I18N ##################################################################
301
326
# {% spaceless %} tag
302
327
'spaceless01': ("{% spaceless %} <b> <i> text </i> </b> {% endspaceless %}", {}, "<b> <i> text </i> </b>"),
342
367
# translation of a constant string
343
368
'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'),
370
### MULTILINE #############################################################
390
### REGROUP TAG ###########################################################
391
#'regroupXX': ('', {}, ''),
392
'regroup01': ('{% regroup data by bar as grouped %}' + \
393
'{% for group in grouped %}' + \
394
'{{ group.grouper }}:' + \
395
'{% for item in group.list %}' + \
399
{'data': [ {'foo':'c', 'bar':1},
400
{'foo':'d', 'bar':1},
401
{'foo':'a', 'bar':2},
402
{'foo':'b', 'bar':2},
403
{'foo':'x', 'bar':3} ]},
406
# Test for silent failure when target variable isn't found
407
'regroup02': ('{% regroup data by bar as grouped %}' + \
408
'{% for group in grouped %}' + \
409
'{{ group.grouper }}:' + \
410
'{% for item in group.list %}' + \
416
### TEMPLATETAG TAG #######################################################
417
#'templatetagXX': ('', {}, ''),
418
'templatetag01': ('{% templatetag openblock %}', {}, '{%'),
419
'templatetag02': ('{% templatetag closeblock %}', {}, '%}'),
420
'templatetag03': ('{% templatetag openvariable %}', {}, '{{'),
421
'templatetag04': ('{% templatetag closevariable %}', {}, '}}'),
422
'templatetag05': ('{% templatetag %}', {}, template.TemplateSyntaxError),
423
'templatetag06': ('{% templatetag foo %}', {}, template.TemplateSyntaxError),
425
### WIDTHRATIO TAG ########################################################
426
#'widthratioXX': ('', {}, ''),
427
'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
428
'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),
429
'widthratio03': ('{% widthratio a b 100 %}', {'a':0,'b':100}, '0'),
430
'widthratio04': ('{% widthratio a b 100 %}', {'a':50,'b':100}, '50'),
431
'widthratio05': ('{% widthratio a b 100 %}', {'a':100,'b':100}, '100'),
433
# 62.5 should round to 63
434
'widthratio06': ('{% widthratio a b 100 %}', {'a':50,'b':80}, '63'),
436
# 71.4 should round to 71
437
'widthratio07': ('{% widthratio a b 100 %}', {'a':50,'b':70}, '71'),
439
# Raise exception if we don't have 3 args, last one an integer
440
'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError),
441
'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError),
442
'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError),
444
### NOW TAG ########################################################
446
'now01' : ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)),
448
# Check parsing of escaped and special characters
449
'now02' : ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError),
450
# 'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)),
451
# 'now04' : ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year))
346
454
def test_template_loader(template_name, template_dirs=None):