Exemple #1
0
 def test_urlencode(self):
     env = Environment()
     tmpl = env.from_string('{{ "Hello, world!"|urlencode }}')
     tmpl.assert_render() == "Hello%2C%20world%21"
     tmpl = env.from_string("{{ o|urlencode }}")
     tmpl.assert_render(o=u"Hello, world\u203d") == "Hello%2C%20world%E2%80%BD"
     tmpl.assert_render(o=(("f", 1),)) == "f=1"
     tmpl.assert_render(o=(("f", 1), ("z", 2))) == "f=1&z=2"
     tmpl.assert_render(o=((u"\u203d", 1),)) == "%E2%80%BD=1"
     tmpl.assert_render(o={u"\u203d": 1}) == "%E2%80%BD=1"
     tmpl.assert_render(o={0: 1}) == "0=1"
Exemple #2
0
 def test_super_in_scoped_block(self):
     env = Environment(loader=DictLoader({
         'master.html': '{% for item in seq %}[{% block item scoped %}'
                        '{{ item }}{% endblock %}]{% endfor %}'
     }))
     t = env.from_string('{% extends "master.html" %}{% block item %}'
                         '{{ super() }}|{{ item * 2 }}{% endblock %}')
     t.assert_render(seq=range(5)) == '[0|0][1|2][2|4][3|6][4|8]'
Exemple #3
0
    def test_extends_output_bugs(self):
        env = Environment(loader=DictLoader({
            'parent.html': '(({% block title %}{% endblock %}))'
        }))

        t = env.from_string('{% if expr %}{% extends "parent.html" %}{% endif %}'
                            '[[{% block title %}title{% endblock %}]]'
                            '{% for item in [1, 2, 3] %}({{ item }}){% endfor %}')
        t.assert_render(expr=False) == '[[title]](1)(2)(3)'
        t.assert_render(expr=True) == '((title))'
Exemple #4
0
    def test_stacked_locals_scoping_bug(self):
        env = Environment(line_statement_prefix='#')
        t = env.from_string('''\
# for j in [1, 2]:
#   set x = 1
#   for i in [1, 2]:
#     print x
#     if i % 2 == 0:
#       set x = x + 1
#     endif
#   endfor
# endfor
# if a
#   print 'A'
# elif b
#   print 'B'
# elif c == d
#   print 'C'
# else
#   print 'D'
# endif
    ''')
        t.assert_render(a=0, b=False, c=42, d=42.0) == '1111C'
Exemple #5
0
        t = test_env.from_string('{% include ["missing", "missing2"] %}')
        self.assert_raises(TemplateNotFound, t.render)
        try:
            t.assert_render()
        except TemplatesNotFound, e:
            assert e.templates == ['missing', 'missing2']
            assert e.name == 'missing2'
        else:
            assert False, 'thou shalt raise'

        def test_includes(t, **ctx):
            ctx['foo'] = 42
            t.assert_render(ctx) == '[42|23]'

        t = test_env.from_string('{% include ["missing", "header"] %}')
        test_includes(t)
        t = test_env.from_string('{% include x %}')
        test_includes(t, x=['missing', 'header'])
        t = test_env.from_string('{% include [x, "header"] %}')
        test_includes(t, x='missing')
        t = test_env.from_string('{% include x %}')
        test_includes(t, x='header')
        t = test_env.from_string('{% include x %}')
        test_includes(t, x='header')
        t = test_env.from_string('{% include [x] %}')
        test_includes(t, x='header')

    def test_include_ignoring_missing(self):
        t = test_env.from_string('{% include "missing" %}')
        self.assert_raises(TemplateNotFound, t.render)
Exemple #6
0
 def test_keyword_folding(self):
     env = Environment()
     env.filters['testing'] = lambda value, some: value + some
     assert env.from_string("{{ 'test'|testing(some='stuff') }}") \
            .assert_render() == 'teststuff'
Exemple #7
0
 def test_replace(self):
     env = Environment()
     tmpl = env.from_string('{{ string|replace("o", 42) }}')
     tmpl.assert_render(string="<foo>") == "<f4242>"