Django

Code

Changeset 1944

Show
Ignore:
Timestamp:
01/13/06 11:22:44 (3 years ago)
Author:
adrian
Message:

magic-removal: Fixed #1217 -- Added some template-tag unit tests. Thanks, nick at efford.net

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/tests/othertests/templates.py

    r1699 r1944  
    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"), 
     158 
     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), 
    163197 
    164198    ### FOR TAG ############################################################### 
     
    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"), 
     205 
     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'), 
    171216 
    172217    ### IFEQUAL TAG ########################################################### 
     
    265310    'inheritance23': ("{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}", {}, '1_ab3_'), 
    266311 
    267     ### EXCEPTIONS ############################################################ 
    268  
    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), 
     312    ### I18N ################################################################## 
     313 
     314    # simple translation of a string delimited by ' 
     315    'i18n01': ("{% load i18n %}{% trans 'xxxyyyxxx' %}", {}, "xxxyyyxxx"), 
     316 
     317    # simple translation of a string delimited by " 
     318    'i18n02': ('{% load i18n %}{% trans "xxxyyyxxx" %}', {}, "xxxyyyxxx"), 
     319 
     320    # simple translation of a variable 
     321    'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': 'xxxyyyxxx'}, "xxxyyyxxx"), 
     322 
     323    # simple translation of a variable and filter 
     324    'i18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'XXXYYYXXX'}, "xxxyyyxxx"), 
     325 
     326    # simple translation of a string with interpolation 
     327    'i18n05': ('{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}', {'anton': 'yyy'}, "xxxyyyxxx"), 
     328 
     329    # simple translation of a string to german 
     330    'i18n06': ('{% load i18n %}{% trans "Page not found" %}', {'LANGUAGE_CODE': 'de'}, "Seite nicht gefunden"), 
     331 
     332    # translation of singular form 
     333    'i18n07': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}plural{% endblocktrans %}', {'number': 1}, "singular"), 
     334 
     335    # translation of plural form 
     336    'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}plural{% endblocktrans %}', {'number': 2}, "plural"), 
     337 
     338    # simple non-translation (only marking) of a string to german 
     339    'i18n09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"), 
     340 
     341    # translation of a variable with a translated filter 
     342    'i18n10': ('{{ bool|yesno:_("ja,nein") }}', {'bool': True}, 'ja'), 
     343 
     344    # translation of a variable with a non-translated filter 
     345    'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'), 
     346 
     347    # usage of the get_available_languages tag 
     348    'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), 
     349 
     350    # translation of a constant string 
     351    'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'), 
     352 
     353    ### MULTILINE ############################################################# 
    280354 
    281355    'multiline01': (""" 
     
    297371                    """), 
    298372 
    299     # simple translation of a string delimited by ' 
    300     'i18n01': ("{% load i18n %}{% trans 'xxxyyyxxx' %}", {}, "xxxyyyxxx"), 
    301  
    302     # simple translation of a string delimited by " 
    303     'i18n02': ('{% load i18n %}{% trans "xxxyyyxxx" %}', {}, "xxxyyyxxx"), 
    304  
    305     # simple translation of a variable 
    306     'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': 'xxxyyyxxx'}, "xxxyyyxxx"), 
    307  
    308     # simple translation of a variable and filter 
    309     'i18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'XXXYYYXXX'}, "xxxyyyxxx"), 
    310  
    311     # simple translation of a string with interpolation 
    312     'i18n05': ('{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}', {'anton': 'yyy'}, "xxxyyyxxx"), 
    313  
    314     # simple translation of a string to german 
    315     'i18n06': ('{% load i18n %}{% trans "Page not found" %}', {'LANGUAGE_CODE': 'de'}, "Seite nicht gefunden"), 
    316  
    317     # translation of singular form 
    318     'i18n07': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}plural{% endblocktrans %}', {'number': 1}, "singular"), 
    319  
    320     # translation of plural form 
    321     'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}plural{% endblocktrans %}', {'number': 2}, "plural"), 
    322  
    323     # simple non-translation (only marking) of a string to german 
    324     'i18n09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"), 
    325  
    326     # translation of a variable with a translated filter 
    327     'i18n10': ('{{ bool|yesno:_("ja,nein") }}', {'bool': True}, 'ja'), 
    328  
    329     # translation of a variable with a non-translated filter 
    330     'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'), 
    331  
    332     # usage of the get_available_languages tag 
    333     'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), 
    334  
    335     # translation of a constant string 
    336     'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'), 
     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