test_features.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import sys
  2. import pytest
  3. from jinja2 import contextfilter
  4. from jinja2 import Environment
  5. from jinja2 import Template
  6. from jinja2._compat import text_type
  7. @pytest.mark.skipif(sys.version_info < (3, 5), reason="Requires 3.5 or later")
  8. def test_generator_stop():
  9. class X(object):
  10. def __getattr__(self, name):
  11. raise StopIteration()
  12. t = Template("a{{ bad.bar() }}b")
  13. with pytest.raises(RuntimeError):
  14. t.render(bad=X())
  15. @pytest.mark.skipif(sys.version_info[0] > 2, reason="Feature only supported on 2.x")
  16. def test_ascii_str():
  17. @contextfilter
  18. def assert_func(context, value):
  19. assert type(value) is context["expected_type"]
  20. env = Environment()
  21. env.filters["assert"] = assert_func
  22. env.policies["compiler.ascii_str"] = False
  23. t = env.from_string('{{ "foo"|assert }}')
  24. t.render(expected_type=text_type)
  25. env.policies["compiler.ascii_str"] = True
  26. t = env.from_string('{{ "foo"|assert }}')
  27. t.render(expected_type=str)
  28. for val in True, False:
  29. env.policies["compiler.ascii_str"] = val
  30. t = env.from_string(u'{{ "\N{SNOWMAN}"|assert }}')
  31. t.render(expected_type=text_type)