Changed resolve_variable to resolve integers and floats as integers and floats. Added ifequal unit tests. Refs #959

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1690 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-12-16 05:33:24 +00:00
parent 676830165a
commit e296de5500
2 changed files with 22 additions and 2 deletions

View File

@ -629,7 +629,7 @@ def resolve_variable(path, context):
""" """
Returns the resolved variable, which may contain attribute syntax, within Returns the resolved variable, which may contain attribute syntax, within
the given context. The variable may be a hard-coded string (if it begins the given context. The variable may be a hard-coded string (if it begins
and ends with single or double quote marks). and ends with single or double quote marks), or an integer or float literal.
>>> c = {'article': {'section':'News'}} >>> c = {'article': {'section':'News'}}
>>> resolve_variable('article.section', c) >>> resolve_variable('article.section', c)
@ -645,7 +645,13 @@ def resolve_variable(path, context):
(The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
""" """
if path[0] in ('"', "'") and path[0] == path[-1]: if path[0] in '0123456789':
number_type = '.' in path and float or int
try:
current = number_type(path)
except ValueError:
current = ''
elif path[0] in ('"', "'") and path[0] == path[-1]:
current = path[1:-1] current = path[1:-1]
else: else:
current = context current = context

View File

@ -164,6 +164,20 @@ TEMPLATE_TESTS = {
'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"), 'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"), 'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),
# Integers
'ifequal11': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {}, "no"),
'ifequal12': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": 1}, "yes"),
'ifequal13': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": "1"}, "no"),
'ifequal14': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": 1}, "no"),
'ifequal15': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": "1"}, "yes"),
# Floats
'ifequal16': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {}, "no"),
'ifequal17': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "yes"),
'ifequal18': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "no"),
'ifequal19': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "no"),
'ifequal20': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "yes"),
### IFNOTEQUAL TAG ######################################################## ### IFNOTEQUAL TAG ########################################################
'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), 'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""), 'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),