from jinja2.utils import evalcontextfunction
- @pytest.mark.api
- @pytest.mark.extended
-class TestExtendedAPI(object):
+class TestExtendedAPI:
def test_item_and_attribute(self, env):
from jinja2.sandbox import SandboxedEnvironment
t.render(total=MAX_RANGE + 1)
- @pytest.mark.api
- @pytest.mark.meta
-class TestMeta(object):
+class TestMeta:
def test_find_undeclared_variables(self, env):
ast = env.parse("{% set foo = 42 %}{{ bar + foo }}")
x = meta.find_undeclared_variables(ast)
assert list(i) == ["foo.html", "bar.html", None]
- @pytest.mark.api
- @pytest.mark.streaming
-class TestStreaming(object):
+class TestStreaming:
def test_basic_streaming(self, env):
t = env.from_string(
"<ul>{% for item in seq %}<li>{{ loop.index }} - {{ item }}</li>"
shutil.rmtree(tmp)
- @pytest.mark.api
- @pytest.mark.undefined
-class TestUndefined(object):
+class TestUndefined:
def test_stopiteration_is_undefined(self):
def test():
raise StopIteration()
Undefined(obj=42, name="upper")()
- @pytest.mark.api
- @pytest.mark.lowlevel
-class TestLowLevel(object):
+class TestLowLevel:
def test_custom_code_generator(self):
class CustomCodeGenerator(CodeGenerator):
def visit_Const(self, node, frame=None):
return env
- @pytest.mark.imports
-class TestAsyncImports(object):
+class TestAsyncImports:
def test_context_imports(self, test_env_async):
t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')
assert t.render(foo=42) == "[|23]"
assert not hasattr(m, "notthere")
- @pytest.mark.imports
- @pytest.mark.includes
-class TestAsyncIncludes(object):
+class TestAsyncIncludes:
def test_context_include(self, test_env_async):
t = test_env_async.from_string('{% include "header" %}')
assert t.render(foo=42) == "[42|23]"
assert t.render().strip() == "(FOO)"
- @pytest.mark.core_tags
- @pytest.mark.for_loop
-class TestAsyncForLoop(object):
+class TestAsyncForLoop:
def test_simple(self, test_env_async):
tmpl = test_env_async.from_string("{% for item in seq %}{{ item }}{% endfor %}")
assert tmpl.render(seq=list(range(10))) == "0123456789"
return Environment(loader=package_loader, bytecode_cache=bytecode_cache)
- @pytest.mark.byte_code_cache
-class TestByteCodeCache(object):
+class TestByteCodeCache:
def test_simple(self, env):
tmpl = env.get_template("test.html")
assert tmpl.render().strip() == "BAR"
return Environment(trim_blocks=True)
- @pytest.mark.core_tags
- @pytest.mark.for_loop
-class TestForLoop(object):
+class TestForLoop:
def test_simple(self, env):
tmpl = env.from_string("{% for item in seq %}{{ item }}{% endfor %}")
assert tmpl.render(seq=list(range(10))) == "0123456789"
assert tmpl.render(x=0, seq=[1, 2, 3]) == "919293"
- @pytest.mark.core_tags
- @pytest.mark.if_condition
-class TestIfCondition(object):
+class TestIfCondition:
def test_simple(self, env):
tmpl = env.from_string("""{% if true %}...{% endif %}""")
assert tmpl.render() == "..."
assert tmpl.render() == "1"
- @pytest.mark.core_tags
- @pytest.mark.macros
-class TestMacros(object):
+class TestMacros:
def test_simple(self, env_trim):
tmpl = env_trim.from_string(
"""\
assert tmpl.module.m(1, x=7) == "1|7|7"
- @pytest.mark.core_tags
- @pytest.mark.set
-class TestSet(object):
+class TestSet:
def test_normal(self, env_trim):
tmpl = env_trim.from_string("{% set foo = 1 %}{{ foo }}")
assert tmpl.render() == "1"
"{{ foo }}"
)
assert tmpl.render() == "11"
- assert tmpl.module.foo == u"11"
+ assert tmpl.module.foo == "11"
- @pytest.mark.core_tags
- @pytest.mark.with_
-class TestWith(object):
+class TestWith:
def test_with(self, env):
tmpl = env.from_string(
"""\
return Environment(loader=filesystem_loader)
- @pytest.mark.debug
-class TestDebug(object):
+class TestDebug:
def assert_traceback_matches(self, callback, expected_tb):
with pytest.raises(Exception) as exc_info:
callback()
yield Token(lineno, "data", token.value[pos:])
- @pytest.mark.ext
-class TestExtensions(object):
+class TestExtensions:
def test_extend_late(self):
env = Environment()
env.add_extension("jinja2.ext.autoescape")
out = t.render()
for value in ("context", "cycler", "filters", "abs", "tests", "!="):
- assert "'{}'".format(value) in out
+ assert f"'{value}'" in out
- @pytest.mark.ext
-class TestInternationalization(object):
+class TestInternationalization:
def test_trans(self):
tmpl = i18n_env.get_template("child.html")
assert tmpl.render(LANGUAGE="de") == "<title>fehlend</title>pass auf"
]
- @pytest.mark.ext
-class TestScope(object):
+class TestScope:
def test_basic_scope_behavior(self):
# This is what the old with statement compiled down to
class ScopeExt(Extension):
assert tmpl.render(b=3, e=4) == "1|2|2|4|5"
- @pytest.mark.ext
-class TestNewstyleInternationalization(object):
+class TestNewstyleInternationalization:
def test_trans(self):
tmpl = newstyle_i18n_env.get_template("child.html")
assert tmpl.render(LANGUAGE="de") == "<title>fehlend</title>pass auf"
assert t.render() == "%(foo)s"
- @pytest.mark.ext
-class TestAutoEscape(object):
+class TestAutoEscape:
def test_scoped_setting(self):
env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)
tmpl = env.from_string(
self.value2 = value2
def __str__(self):
- return u"(%s,%s)" % (text_type(self.value1), text_type(self.value2))
+ return f"({self.value1},{self.value2})"
- @pytest.mark.filter
-class TestFilter(object):
+class TestFilter:
def test_filter_calling(self, env):
rv = env.call_filter("sum", [1, 2, 3])
assert rv == 6
return env
- @pytest.mark.imports
-class TestImports(object):
+class TestImports:
def test_context_imports(self, test_env):
t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')
assert t.render(foo=42) == "[|23]"
assert m.variable == 42
assert not hasattr(m, "notthere")
+ def test_not_exported(self, test_env):
+ t = test_env.from_string("{% from 'module' import nothing %}{{ nothing() }}")
-class TestIncludes(object):
+ with pytest.raises(UndefinedError, match="does not export the requested name"):
+ t.render()
+
+
- @pytest.mark.imports
- @pytest.mark.includes
+class TestIncludes:
def test_context_include(self, test_env):
t = test_env.from_string('{% include "header" %}')
assert t.render(foo=42) == "[42|23]"
)
- @pytest.mark.inheritance
-class TestInheritance(object):
+class TestInheritance:
def test_layout(self, env):
tmpl = env.get_template("layout")
assert tmpl.render() == (
assert rv == ["43", "44", "45"]
- @pytest.mark.inheritance
-class TestBugFix(object):
+class TestBugFix:
def test_fixed_macro_scoping_bug(self, env):
assert (
Environment(
from jinja2.lexer import TokenStream
- @pytest.mark.lexnparse
- @pytest.mark.tokenstream
-# how does a string look like in jinja syntax?
-if PY2:
-
- def jinja_string_repr(string):
- return repr(string)[1:]
-
-
-else:
- jinja_string_repr = repr
-
-
-class TestTokenStream(object):
+class TestTokenStream:
test_tokens = [
Token(1, TOKEN_BLOCK_BEGIN, ""),
Token(2, TOKEN_BLOCK_END, ""),
]
- @pytest.mark.lexnparse
- @pytest.mark.lexer
-class TestLexer(object):
+class TestLexer:
def test_raw1(self, env):
tmpl = env.from_string(
"{% raw %}foo{% endraw %}|"
pytest.raises(TemplateSyntaxError, env.from_string, t)
- @pytest.mark.lexnparse
- @pytest.mark.parser
-class TestParser(object):
+class TestParser:
def test_php_syntax(self, env):
env = Environment("<?", "?>", "<?=", "?>", "<!--", "-->")
tmpl = env.from_string(
assert_error("{% unknown_tag %}", "Encountered unknown tag 'unknown_tag'.")
- @pytest.mark.lexnparse
- @pytest.mark.syntax
-class TestSyntax(object):
+class TestSyntax:
def test_call(self, env):
env = Environment()
env.globals["foo"] = lambda a, b, c, e, g: a + b + c + e + g
assert tmpl.render(foo={"bar": 42}) == "42"
- @pytest.mark.lexnparse
- @pytest.mark.lstripblocks
-class TestLstripBlocks(object):
+class TestLstripBlocks:
def test_lstrip(self, env):
env = Environment(lstrip_blocks=True, trim_blocks=False)
tmpl = env.from_string(""" {% if True %}\n {% endif %}""")
from jinja2.loaders import split_template_path
- @pytest.mark.loaders
-class TestLoaders(object):
+class TestLoaders:
def test_dict_loader(self, dict_loader):
env = Environment(loader=dict_loader)
tmpl = env.get_template("justdict.html")
pytest.raises(TemplateNotFound, split_template_path, "../foo")
- @pytest.mark.loaders
- @pytest.mark.filesystemloader
-class TestFileSystemLoader(object):
+class TestFileSystemLoader:
searchpath = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "res", "templates"
)
assert t.render() == expect
- @pytest.mark.loaders
- @pytest.mark.moduleloader
-class TestModuleLoader(object):
+class TestModuleLoader:
archive = None
- def compile_down(self, prefix_loader, zip="deflated", py_compile=False):
+ def compile_down(self, prefix_loader, zip="deflated"):
log = []
self.reg_env = Environment(loader=prefix_loader)
if zip is not None:
from jinja2 import TemplateAssertionError
from jinja2 import TemplateNotFound
from jinja2 import TemplateSyntaxError
-from jinja2._compat import text_type
- @pytest.mark.regression
-class TestCorner(object):
+class TestCorner:
def test_assigned_scoping(self, env):
t = env.from_string(
"""
assert t.render(wrapper=23) == "[1][2][3][4]23"
- @pytest.mark.regression
-class TestBug(object):
+class TestBug:
def test_keyword_folding(self, env):
env = Environment()
env.filters["testing"] = lambda value, some: value + some
return "PublicStuff"
- @pytest.mark.sandbox
-class TestSandbox(object):
+class TestSandbox:
def test_unsafe(self, env):
env = SandboxedEnvironment()
pytest.raises(
t.render(ctx)
- @pytest.mark.sandbox
-class TestStringFormat(object):
+class TestStringFormat:
def test_basic_format_safety(self):
env = SandboxedEnvironment()
t = env.from_string('{{ "a{0.__class__}b".format(42) }}')
assert t.render() == "a42b<foo>"
- @pytest.mark.sandbox
-@pytest.mark.skipif(
- not hasattr(str, "format_map"), reason="requires str.format_map method"
-)
-class TestStringFormatMap(object):
+class TestStringFormatMap:
def test_basic_format_safety(self):
env = SandboxedEnvironment()
t = env.from_string('{{ "a{x.__class__}b".format_map({"x":42}) }}')
pass
- @pytest.mark.test_tests
-class TestTestsCase(object):
+class TestTestsCase:
def test_defined(self, env):
tmpl = env.from_string("{{ missing is defined }}|{{ true is defined }}")
assert tmpl.render() == "False|True"
from jinja2.utils import urlize
- @pytest.mark.utils
- @pytest.mark.lrucache
-class TestLRUCache(object):
+class TestLRUCache:
def test_simple(self):
d = LRUCache(3)
d["a"] = 1
assert len(d) == 2
- @pytest.mark.utils
- @pytest.mark.helpers
-class TestHelpers(object):
+class TestHelpers:
def test_object_type_repr(self):
- class X(object):
+ class X:
pass
assert object_type_repr(42) == "int object"
assert not func("FOO.TXT")
- @pytest.mark.utils
- @pytest.mark.escapeUrlizeTarget
-class TestEscapeUrlizeTarget(object):
+class TestEscapeUrlizeTarget:
def test_escape_urlize_target(self):
url = "http://example.org"
target = "<script>"
)
- @pytest.mark.utils
- @pytest.mark.loremIpsum
-class TestLoremIpsum(object):
+class TestLoremIpsum:
def test_lorem_ipsum_markup(self):
"""Test that output of lorem_ipsum is Markup by default."""
assert isinstance(generate_lorem_ipsum(), Markup)