Skip to content
Myr

test_syntax.py

This file

Path

tests/test_syntax.py

90 lines · 2.3 KiB · Python

Last changed by

[svn] added baseclasses for loaders, added the memcaching loader, updated documentation for loadersArmin Ronacherbefore adoptiondb69d0a

History of this file

Take it

1# -*- 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"""910CALL = '''{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}'''11SLICING = '''{{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}'''12ATTR = '''{{ foo.bar }}|{{ foo['bar'] }}'''13SUBSCRIPT = '''{{ foo[0] }}|{{ foo[-1] }}'''14KEYATTR = '''{{ {'items': 'foo'}.items }}|{{ {}.items() }}'''15TUPLE = '''{{ () }}'''16MATH = '''{{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}'''17DIV = '''{{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}'''18UNARY = '''{{ +3 }}|{{ -3 }}'''19COMPARE = '''{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|{{ 2 == 2 }}|{{ 1 <= 1 }}'''20LITERALS = '''{{ [] }}|{{ {} }}|{{ '' }}'''21BOOL = '''{{ true and false }}|{{ false or true }}|{{ not false }}'''22GROUPING = '''{{ (true and false) or (false and true) and not false }}'''232425def test_call():26    from jinja import Environment27    env = Environment()28    env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g29    tmpl = env.from_string(CALL)30    assert tmpl.render() == 'abdfh'313233def test_slicing(env):34    tmpl = env.from_string(SLICING)35    assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'363738def test_attr(env):39    tmpl = env.from_string(ATTR)40    assert tmpl.render(foo={'bar': 42}) == '42|42'414243def test_subscript(env):44    tmpl = env.from_string(SUBSCRIPT)45    assert tmpl.render(foo=[0, 1, 2]) == '0|2'464748def test_keyattr(env):49    tmpl = env.from_string(KEYATTR)50    assert tmpl.render() == 'foo|[]'515253def test_tuple(env):54    tmpl = env.from_string(TUPLE)55    assert tmpl.render() == '[]'565758def test_math(env):59    tmpl = env.from_string(MATH)60    assert tmpl.render() == '1.5|8'616263def test_div(env):64    tmpl = env.from_string(DIV)65    assert tmpl.render() == '1|1.5|1'666768def test_unary(env):69    tmpl = env.from_string(UNARY)70    assert tmpl.render() == '3|-3'717273def test_compare(env):74    tmpl = env.from_string(COMPARE)75    assert tmpl.render() == 'True|True|True|True|True'767778def test_literals(env):79    tmpl = env.from_string(LITERALS)80    assert tmpl.render() == '[]|{}|'818283def test_bool(env):84    tmpl = env.from_string(BOOL)85    assert tmpl.render() == 'False|True|True'868788def test_grouping(env):89    tmpl = env.from_string(GROUPING)90    assert tmpl.render() == 'False'