test_syntax.py
This file
Path
tests/test_syntax.py
191 lines · 5.1 KiB · Python
Last changed by
It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f
History of this fileTake it
The walk stopped after 200 commits: lines older than 316157d point at it.
fixed another python2.4 bugArmin Ronacherbefore adoption316157d
lines 1–8why, one step back1# -*- coding: utf-8 -*-2"""3 unit test for expression syntax4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""all calls are proxied by context.call now so that we can inject environment and context as first arguments. This slows calls down a bit but is a lot more user friendly. Added first draft of FAQArmin Ronacherbefore adoptionfd31049
line 9why, one step back9from py.test import raisesfixed another python2.4 bugArmin Ronacherbefore adoption316157d
line 10why, one step back10from jinja2 import Environment, DictLoaderFixed a bug with the loop context of a for loop if the iterator passed has a volatile `__len__` like the listreverseiterator. `else` in inline if-expressions is optional now.Armin Ronacherbefore adoption547d0b6
line 11why, one step back11from jinja2.exceptions import TemplateSyntaxError, UndefinedErrorfixed another python2.4 bugArmin Ronacherbefore adoption316157d
lines 12–28why, one step back121314CALL = '''{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}'''15SLICING = '''{{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}'''16ATTR = '''{{ foo.bar }}|{{ foo['bar'] }}'''17SUBSCRIPT = '''{{ foo[0] }}|{{ foo[-1] }}'''18TUPLE = '''{{ () }}|{{ (1,) }}|{{ (1, 2) }}'''19MATH = '''{{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}'''20DIV = '''{{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}'''21UNARY = '''{{ +3 }}|{{ -3 }}'''22CONCAT = '''{{ [1, 2] ~ 'foo' }}'''23COMPARE = '''{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|{{ 2 == 2 }}|{{ 1 <= 1 }}'''24INOP = '''{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}'''25LITERALS = '''{{ [] }}|{{ {} }}|{{ () }}'''26BOOL = '''{{ true and false }}|{{ false or true }}|{{ not false }}'''27GROUPING = '''{{ (true and false) or (false and true) and not false }}'''28CONDEXPR = '''{{ 0 if true else 1 }}'''It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f
line 29why, one step back29DJANGOATTR = '''{{ [1, 2, 3].0 }}|{{ [[1]].0.0 }}'''fixed another python2.4 bugArmin Ronacherbefore adoption316157d
lines 30–118why, one step back30FILTERPRIORITY = '''{{ "foo"|upper + "bar"|upper }}'''31TUPLETEMPLATES = [32 '{{ () }}',33 '{{ (1, 2) }}',34 '{{ (1, 2,) }}',35 '{{ 1, }}',36 '{{ 1, 2 }}',37 '{% for foo, bar in seq %}...{% endfor %}',38 '{% for x in foo, bar %}...{% endfor %}',39 '{% for x in foo, %}...{% endfor %}'40]41TRAILINGCOMMA = '''{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}'''424344def test_call():45 from jinja2 import Environment46 env = Environment()47 env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g48 tmpl = env.from_string(CALL)49 assert tmpl.render() == 'abdfh'505152def test_slicing(env):53 tmpl = env.from_string(SLICING)54 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'555657def test_attr(env):58 tmpl = env.from_string(ATTR)59 assert tmpl.render(foo={'bar': 42}) == '42|42'606162def test_subscript(env):63 tmpl = env.from_string(SUBSCRIPT)64 assert tmpl.render(foo=[0, 1, 2]) == '0|2'656667def test_tuple(env):68 tmpl = env.from_string(TUPLE)69 assert tmpl.render() == '()|(1,)|(1, 2)'707172def test_math(env):73 tmpl = env.from_string(MATH)74 assert tmpl.render() == '1.5|8'757677def test_div(env):78 tmpl = env.from_string(DIV)79 assert tmpl.render() == '1|1.5|1'808182def test_unary(env):83 tmpl = env.from_string(UNARY)84 assert tmpl.render() == '3|-3'858687def test_concat(env):88 tmpl = env.from_string(CONCAT)89 assert tmpl.render() == '[1, 2]foo'909192def test_compare(env):93 tmpl = env.from_string(COMPARE)94 assert tmpl.render() == 'True|True|True|True|True'959697def test_inop(env):98 tmpl = env.from_string(INOP)99 assert tmpl.render() == 'True|False'100101102def test_literals(env):103 tmpl = env.from_string(LITERALS)104 assert tmpl.render().lower() == '[]|{}|()'105106107def test_bool(env):108 tmpl = env.from_string(BOOL)109 assert tmpl.render() == 'False|True|True'110111112def test_grouping(env):113 tmpl = env.from_string(GROUPING)114 assert tmpl.render() == 'False'115116117def test_django_attr(env):118 tmpl = env.from_string(DJANGOATTR)It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f
line 119why, one step back119 assert tmpl.render() == '1|1'fixed another python2.4 bugArmin Ronacherbefore adoption316157d
lines 120–124why, one step back120121122def test_conditional_expression(env):123 tmpl = env.from_string(CONDEXPR)124 assert tmpl.render() == '0'Fixed a bug with the loop context of a for loop if the iterator passed has a volatile `__len__` like the listreverseiterator. `else` in inline if-expressions is optional now.Armin Ronacherbefore adoption547d0b6
lines 125–132why, one step back125126127def test_short_conditional_expression(env):128 tmpl = env.from_string('<{{ 1 if false }}>')129 assert tmpl.render() == '<>'130131 tmpl = env.from_string('<{{ (1 if false).bar }}>')132 raises(UndefinedError, tmpl.render)fixed another python2.4 bugArmin Ronacherbefore adoption316157d
lines 133–154why, one step back133134135def test_filter_priority(env):136 tmpl = env.from_string(FILTERPRIORITY)137 assert tmpl.render() == 'FOOBAR'138139140def test_function_calls(env):141 tests = [142 (True, '*foo, bar'),143 (True, '*foo, *bar'),144 (True, '*foo, bar=42'),145 (True, '**foo, *bar'),146 (True, '**foo, bar'),147 (False, 'foo, bar'),148 (False, 'foo, bar=42'),149 (False, 'foo, bar=23, *args'),150 (False, 'a, b=c, *d, **e'),151 (False, '*foo, **bar')152 ]153 for should_fail, sig in tests:154 if should_fail:all calls are proxied by context.call now so that we can inject environment and context as first arguments. This slows calls down a bit but is a lot more user friendly. Added first draft of FAQArmin Ronacherbefore adoptionfd31049
line 155why, one step back155 raises(TemplateSyntaxError, env.from_string, '{{ foo(%s) }}' % sig)fixed another python2.4 bugArmin Ronacherbefore adoption316157d
lines 156–161why, one step back156 else:157 env.from_string('foo(%s)' % sig)158159160def test_tuple_expr(env):161 for tmpl in TUPLETEMPLATES:Jinja doesn't have keywords any longer. The reason for this radical change is that the previous keywords where incompatible with the python keywords which made it impossible to name a variable "filter" or call a function with such a keyword parameter.Armin Ronacherbefore adoption115de2e
line 162why, one step back162 print tmplfixed another python2.4 bugArmin Ronacherbefore adoption316157d
lines 163–168why, one step back163 assert env.from_string(tmpl)164165166def test_trailing_comma(env):167 tmpl = env.from_string(TRAILINGCOMMA)168 assert tmpl.render().lower() == '(1, 2)|[1, 2]|{1: 2}'all calls are proxied by context.call now so that we can inject environment and context as first arguments. This slows calls down a bit but is a lot more user friendly. Added first draft of FAQArmin Ronacherbefore adoptionfd31049
lines 169–173why, one step back169170171def test_block_end_name(env):172 env.from_string('{% block foo %}...{% endblock foo %}')173 raises(TemplateSyntaxError, env.from_string, '{% block x %}{% endblock y %}')some more documentation updates and minor code cleanups. Additionally True and true in the template are the same now, same for false/False and none/None.Armin Ronacherbefore adoption9bb7e47
lines 174–181why, one step back174175176def test_contant_casing(env):177 for const in True, False, None:178 tmpl = env.from_string('{{ %s }}|{{ %s }}|{{ %s }}' % (179 str(const), str(const).lower(), str(const).upper()180 ))181 assert tmpl.render() == '%s|%s|' % (const, const)Fixed a broken unittest and fixed a bug that required multiple tests to be put into parentheses for chaning.Armin Ronacherbefore adoptione3290ea
lines 182–186why, one step back182183184def test_test_chaining(env):185 raises(TemplateSyntaxError, env.from_string, '{{ foo is string is sequence }}')186 env.from_string('{{ 42 is string or 42 is number }}').render() == 'True'added support for implicit string literal concatenationArmin Ronacherbefore adoption4778bda
lines 187–191why, one step back187188189def test_string_concatenation(env):190 tmpl = env.from_string('{{ "foo" "bar" "baz" }}')191 assert tmpl.render() == 'foobarbaz'