Kaydet (Commit) e296de55 authored tarafından Adrian Holovaty's avatar Adrian Holovaty

Changed resolve_variable to resolve integers and floats as integers and floats.…

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
üst 67683016
......@@ -629,7 +629,7 @@ def resolve_variable(path, context):
"""
Returns the resolved variable, which may contain attribute syntax, within
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'}}
>>> resolve_variable('article.section', c)
......@@ -645,7 +645,13 @@ def resolve_variable(path, context):
(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]
else:
current = context
......
......@@ -164,6 +164,20 @@ TEMPLATE_TESTS = {
'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
'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 ########################################################
'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment