Ticket #1217: templates.diff

File templates.diff, 9.8 KB (added by nick@…, 18 years ago)

[patch] Additional unit tests for default template tags

  • templates.py

     
    152152    # the exception propogates
    153153    'basic-syntax34': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException),
    154154
    155     ### IF TAG ################################################################
    156     'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"),
    157     'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"),
    158     'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"),
    159 
    160155    ### COMMENT TAG ###########################################################
    161156    'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"),
    162157    'comment-tag02': ("{% comment %}this is hidden{% endcomment %}hello{% comment %}foo{% endcomment %}", {}, "hello"),
    163158
     159    ### CYCLE TAG #############################################################
     160    #'cycleXX': ('', {}, ''),
     161    'cycle01': ('{% cycle a, %}', {}, 'a'),
     162    'cycle02': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'),
     163    'cycle03': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'),
     164    'cycle04': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'),
     165    'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
     166    'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
     167    'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError),
     168
     169    ### EXCEPTIONS ############################################################
     170
     171    # Raise exception for invalid template name
     172    'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
     173
     174    # Raise exception for invalid template name (in variable)
     175    'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
     176
     177    # Raise exception for extra {% extends %} tags
     178    'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
     179
     180    # Raise exception for custom tags used in child with {% load %} tag in parent, not in child
     181    'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
     182
     183    ### FILTER TAG ############################################################
     184    #'filterXX': ('', {}, ''),
     185    'filter01': ('{% filter upper %}{% endfilter %}', {}, ''),
     186    'filter02': ('{% filter upper %}django{% endfilter %}', {}, 'DJANGO'),
     187    'filter03': ('{% filter upper|lower %}django{% endfilter %}', {}, 'django'),
     188
     189    ### FIRSTOF TAG ###########################################################
     190    #'firstofXX': ('', {}, ''),
     191    'firstof01': ('{% firstof a b c %}', {'a':0,'b':0,'c':0}, ''),
     192    'firstof02': ('{% firstof a b c %}', {'a':1,'b':0,'c':0}, '1'),
     193    'firstof03': ('{% firstof a b c %}', {'a':0,'b':2,'c':0}, '2'),
     194    'firstof04': ('{% firstof a b c %}', {'a':0,'b':0,'c':3}, '3'),
     195    'firstof05': ('{% firstof a b c %}', {'a':1,'b':2,'c':3}, '1'),
     196    'firstof06': ('{% firstof %}', {}, template.TemplateSyntaxError),
     197
    164198    ### FOR TAG ###############################################################
    165199    'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"),
    166200    'for-tag02': ("{% for val in values reversed %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "321"),
     
    169203    'for-tag-vars03': ("{% for val in values %}{{ forloop.revcounter }}{% endfor %}", {"values": [6, 6, 6]}, "321"),
    170204    'for-tag-vars04': ("{% for val in values %}{{ forloop.revcounter0 }}{% endfor %}", {"values": [6, 6, 6]}, "210"),
    171205
     206    ### IF TAG ################################################################
     207    'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"),
     208    'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"),
     209    'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"),
     210
     211    ### IFCHANGED TAG #########################################################
     212    #'ifchangedXX': ('', {}, ''),
     213    'ifchanged01': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', { 'num': (1,2,3) }, '123'),
     214    'ifchanged02': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', { 'num': (1,1,3) }, '13'),
     215    'ifchanged03': ('{% for n in num %}{% ifchanged %}{{ n }}{% endifchanged %}{% endfor %}', { 'num': (1,1,1) }, '1'),
     216
    172217    ### IFEQUAL TAG ###########################################################
    173218    'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""),
    174219    'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
     
    264309    # Three-level inheritance with {{ block.super }} from parent and grandparent
    265310    'inheritance23': ("{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}", {}, '1_ab3_'),
    266311
    267     ### EXCEPTIONS ############################################################
     312    ### I18N ##################################################################
    268313
    269     # Raise exception for invalid template name
    270     'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
    271 
    272     # Raise exception for invalid template name (in variable)
    273     'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
    274 
    275     # Raise exception for extra {% extends %} tags
    276     'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
    277 
    278     # Raise exception for custom tags used in child with {% load %} tag in parent, not in child
    279     'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
    280 
    281     'multiline01': ("""
    282                     Hello,
    283                     boys.
    284                     How
    285                     are
    286                     you
    287                     gentlemen.
    288                     """,
    289                     {},
    290                     """
    291                     Hello,
    292                     boys.
    293                     How
    294                     are
    295                     you
    296                     gentlemen.
    297                     """),
    298 
    299314    # simple translation of a string delimited by '
    300315    'i18n01': ("{% load i18n %}{% trans 'xxxyyyxxx' %}", {}, "xxxyyyxxx"),
    301316
     
    334349
    335350    # translation of a constant string
    336351    'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'),
     352
     353    ### MULTILINE #############################################################
     354
     355    'multiline01': ("""
     356                    Hello,
     357                    boys.
     358                    How
     359                    are
     360                    you
     361                    gentlemen.
     362                    """,
     363                    {},
     364                    """
     365                    Hello,
     366                    boys.
     367                    How
     368                    are
     369                    you
     370                    gentlemen.
     371                    """),
     372
     373    ### REGROUP TAG ###########################################################
     374    #'regroupXX': ('', {}, ''),
     375    'regroup01': ('{% regroup data by bar as grouped %}' + \
     376                  '{% for group in grouped %}' + \
     377                  '{{ group.grouper }}:' + \
     378                  '{% for item in group.list %}' + \
     379                  '{{ item.foo }}' + \
     380                  '{% endfor %},' + \
     381                  '{% endfor %}',
     382                  {'data': [ {'foo':'c', 'bar':1},
     383                             {'foo':'d', 'bar':1},
     384                             {'foo':'a', 'bar':2},
     385                             {'foo':'b', 'bar':2},
     386                             {'foo':'x', 'bar':3}  ]},
     387                  '1:cd,2:ab,3:x,'),
     388
     389    # Test for silent failure when target variable isn't found
     390    'regroup02': ('{% regroup data by bar as grouped %}' + \
     391                  '{% for group in grouped %}' + \
     392                  '{{ group.grouper }}:' + \
     393                  '{% for item in group.list %}' + \
     394                  '{{ item.foo }}' + \
     395                  '{% endfor %},' + \
     396                  '{% endfor %}',
     397                  {}, ''),
     398
     399    ### TEMPLATETAG TAG #######################################################
     400    #'templatetagXX': ('', {}, ''),
     401    'templatetag01': ('{% templatetag openblock %}', {}, '{%'),
     402    'templatetag02': ('{% templatetag closeblock %}', {}, '%}'),
     403    'templatetag03': ('{% templatetag openvariable %}', {}, '{{'),
     404    'templatetag04': ('{% templatetag closevariable %}', {}, '}}'),
     405    'templatetag05': ('{% templatetag %}', {}, template.TemplateSyntaxError),
     406    'templatetag06': ('{% templatetag foo %}', {}, template.TemplateSyntaxError),
     407
     408    ### WIDTHRATIO TAG ########################################################
     409    #'widthratioXX': ('', {}, ''),
     410    'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
     411    'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),
     412    'widthratio03': ('{% widthratio a b 100 %}', {'a':0,'b':100}, '0'),
     413    'widthratio04': ('{% widthratio a b 100 %}', {'a':50,'b':100}, '50'),
     414    'widthratio05': ('{% widthratio a b 100 %}', {'a':100,'b':100}, '100'),
     415
     416    # 62.5 should round to 63
     417    'widthratio06': ('{% widthratio a b 100 %}', {'a':50,'b':80}, '63'),
     418
     419    # 71.4 should round to 71
     420    'widthratio07': ('{% widthratio a b 100 %}', {'a':50,'b':70}, '71'),
     421
     422    # Raise exception if we don't have 3 args, last one an integer
     423    'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError),
     424    'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError),
     425    'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError),
    337426}
    338427
    339428def test_template_loader(template_name, template_dirs=None):
Back to Top