aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Jinja2/py2/tests/test_features.py
blob: 34b6f200deab22533911fb1ec800ff8770254792 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys

import pytest

from jinja2 import contextfilter
from jinja2 import Environment
from jinja2 import Template
from jinja2._compat import text_type


@pytest.mark.skipif(sys.version_info < (3, 5), reason="Requires 3.5 or later")
def test_generator_stop():
    class X(object):
        def __getattr__(self, name):
            raise StopIteration()

    t = Template("a{{ bad.bar() }}b")
    with pytest.raises(RuntimeError):
        t.render(bad=X())


@pytest.mark.skipif(sys.version_info[0] > 2, reason="Feature only supported on 2.x")
def test_ascii_str():
    @contextfilter
    def assert_func(context, value):
        assert type(value) is context["expected_type"]

    env = Environment()
    env.filters["assert"] = assert_func

    env.policies["compiler.ascii_str"] = False
    t = env.from_string('{{ "foo"|assert }}')
    t.render(expected_type=text_type)

    env.policies["compiler.ascii_str"] = True
    t = env.from_string('{{ "foo"|assert }}')
    t.render(expected_type=str)

    for val in True, False:
        env.policies["compiler.ascii_str"] = val
        t = env.from_string(u'{{ "\N{SNOWMAN}"|assert }}')
        t.render(expected_type=text_type)