Skip to content
Myr

test_syntax.py

This file

Path

tests/test_syntax.py

205 lines · 5.4 KiB · Python

Last changed by

Fixed a bug in the parser that made ``{{ foo[1, 2] }}`` impossible.Armin Ronacherbefore adoptionee2d3c4

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    unit test for expression syntax4    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56    :copyright: (c) 2009 by the Jinja Team.7    :license: BSD, see LICENSE for more details.8"""9from py.test import raises10from jinja2 import Environment, DictLoader11from jinja2.exceptions import TemplateSyntaxError, UndefinedError121314CALL = '''{{ 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 }}'''29DJANGOATTR = '''{{ [1, 2, 3].0 }}|{{ [[1]].0.0 }}'''30FILTERPRIORITY = '''{{ "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)119    assert tmpl.render() == '1|1'120121122def test_conditional_expression(env):123    tmpl = env.from_string(CONDEXPR)124    assert tmpl.render() == '0'125126127def 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)133134135def 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:155            raises(TemplateSyntaxError, env.from_string, '{{ foo(%s) }}' % sig)156        else:157            env.from_string('foo(%s)' % sig)158159160def test_tuple_expr(env):161    for tmpl in TUPLETEMPLATES:162        print tmpl163        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}'169170171def test_block_end_name(env):172    env.from_string('{% block foo %}...{% endblock foo %}')173    raises(TemplateSyntaxError, env.from_string, '{% block x %}{% endblock y %}')174175176def 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)182183184def 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'187188189def test_string_concatenation(env):190    tmpl = env.from_string('{{ "foo" "bar" "baz" }}')191    assert tmpl.render() == 'foobarbaz'192193194def test_notin(env):195    bar = xrange(100)196    tmpl = env.from_string('''{{ not 42 in bar }}''')197    assert tmpl.render(bar=bar) == unicode(not 42 in bar)198199200def test_implicit_subscribed_tuple(env):201    class Foo(object):202        def __getitem__(self, x):203            return x204    t = env.from_string('{{ foo[1, 2] }}')205    assert t.render(foo=Foo()) == u'(1, 2)'