~kkubasik/django/aggregation-branch

« back to all changes in this revision

Viewing changes to tests/othertests/templates.py

  • Committer: adrian
  • Date: 2006-05-02 01:31:56 UTC
  • Revision ID: vcs-imports@canonical.com-20060502013156-2941fcd40d080649
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from django.conf import settings
2
2
 
3
 
# Turn TEMPLATE_DEBUG off, because tests assume that.
4
 
settings.TEMPLATE_DEBUG = False
5
3
 
6
 
from django.core import template
7
 
from django.core.template import loader
 
4
from django import template
 
5
from django.template import loader
8
6
from django.utils.translation import activate, deactivate, install
 
7
from datetime import datetime
9
8
import traceback
10
9
 
11
10
#################################
32
31
# Helper objects for template tests #
33
32
#####################################
34
33
 
 
34
class SomeException(Exception):
 
35
    silent_variable_failure = True
 
36
 
 
37
class SomeOtherException(Exception):
 
38
    pass
 
39
 
35
40
class SomeClass:
36
41
    def __init__(self):
37
42
        self.otherclass = OtherClass()
42
47
    def method2(self, o):
43
48
        return o
44
49
 
 
50
    def method3(self):
 
51
        raise SomeException
 
52
 
 
53
    def method4(self):
 
54
        raise SomeOtherException
 
55
 
45
56
class OtherClass:
46
57
    def method(self):
47
58
        return "OtherClass.method"
133
144
    'basic-syntax31': (r'{{ var|default_if_none:var2 }}', {"var": None, "var2": "happy"}, 'happy'),
134
145
 
135
146
    # Default argument testing
136
 
    'basic-syntax32' : (r'{{ var|yesno:"yup,nup,mup" }} {{ var|yesno }}', {"var": True}, 'yup yes'),
137
 
 
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'),
 
148
 
 
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"),
 
151
 
 
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),
 
155
 
 
156
    # Escaped backslash in argument
 
157
    'basic-syntax35': (r'{{ var|default_if_none:"foo\bar" }}', {"var": None}, r'foo\bar'),
 
158
 
 
159
    # Escaped backslash using known escape char
 
160
    'basic-syntax35': (r'{{ var|default_if_none:"foo\now" }}', {"var": None}, r'foo\now'),
142
161
 
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"),
151
170
 
 
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),
 
180
 
 
181
    ### EXCEPTIONS ############################################################
 
182
 
 
183
    # Raise exception for invalid template name
 
184
    'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
 
185
 
 
186
    # Raise exception for invalid template name (in variable)
 
187
    'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
 
188
 
 
189
    # Raise exception for extra {% extends %} tags
 
190
    'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
 
191
 
 
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),
 
194
 
 
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'),
 
200
 
 
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),
 
209
 
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"),
159
217
 
 
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"),
 
222
 
 
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'),
 
228
 
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"),
171
240
 
172
 
    # Integers
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"),
178
 
 
179
 
    # Floats
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"),
185
 
 
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_'),
268
323
 
269
 
    ### EXCEPTIONS ############################################################
270
 
 
271
 
    # Raise exception for invalid template name
272
 
    'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
273
 
 
274
 
    # Raise exception for invalid template name (in variable)
275
 
    'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
276
 
 
277
 
    # Raise exception for extra {% extends %} tags
278
 
    'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
279
 
 
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),
282
 
 
283
 
    'multiline01': ("""
284
 
                    Hello,
285
 
                    boys.
286
 
                    How
287
 
                    are
288
 
                    you
289
 
                    gentlemen.
290
 
                    """,
291
 
                    {},
292
 
                    """
293
 
                    Hello,
294
 
                    boys.
295
 
                    How
296
 
                    are
297
 
                    you
298
 
                    gentlemen.
299
 
                    """),
 
324
    ### I18N ##################################################################
300
325
 
301
326
    # {% spaceless %} tag
302
327
    'spaceless01': ("{% spaceless %} <b>    <i> text </i>    </b> {% endspaceless %}", {}, "<b> <i> text </i> </b>"),
341
366
 
342
367
    # translation of a constant string
343
368
    'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'),
 
369
 
 
370
    ### MULTILINE #############################################################
 
371
 
 
372
    'multiline01': ("""
 
373
                    Hello,
 
374
                    boys.
 
375
                    How
 
376
                    are
 
377
                    you
 
378
                    gentlemen.
 
379
                    """,
 
380
                    {},
 
381
                    """
 
382
                    Hello,
 
383
                    boys.
 
384
                    How
 
385
                    are
 
386
                    you
 
387
                    gentlemen.
 
388
                    """),
 
389
 
 
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 %}' + \
 
396
                  '{{ item.foo }}' + \
 
397
                  '{% endfor %},' + \
 
398
                  '{% endfor %}',
 
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}  ]},
 
404
                  '1:cd,2:ab,3:x,'),
 
405
 
 
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 %}' + \
 
411
                  '{{ item.foo }}' + \
 
412
                  '{% endfor %},' + \
 
413
                  '{% endfor %}',
 
414
                  {}, ''),
 
415
 
 
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),
 
424
 
 
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'),
 
432
 
 
433
    # 62.5 should round to 63
 
434
    'widthratio06': ('{% widthratio a b 100 %}', {'a':50,'b':80}, '63'),
 
435
 
 
436
    # 71.4 should round to 71
 
437
    'widthratio07': ('{% widthratio a b 100 %}', {'a':50,'b':70}, '71'),
 
438
 
 
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),
 
443
    
 
444
    ### NOW TAG ########################################################
 
445
    # Simple case
 
446
    'now01' : ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)),
 
447
    
 
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))
344
452
}
345
453
 
346
454
def test_template_loader(template_name, template_dirs=None):
358
466
    failed_tests = []
359
467
    tests = TEMPLATE_TESTS.items()
360
468
    tests.sort()
 
469
 
 
470
    # Turn TEMPLATE_DEBUG off, because tests assume that.
 
471
    old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, False
361
472
    for name, vals in tests:
362
473
        install()
363
474
        if 'LANGUAGE_CODE' in vals[1]:
387
498
            failed_tests.append(name)
388
499
    loader.template_source_loaders = old_template_loaders
389
500
    deactivate()
 
501
    settings.TEMPLATE_DEBUG = old_td
 
502
 
390
503
    if failed_tests and not standalone:
391
504
        msg = "Template tests %s failed." % failed_tests
392
505
        if not verbosity: