tmpl = env.from_string('{{ "foo"|upper + "bar"|upper }}')
assert tmpl.render() == "FOOBAR"
- def test_function_calls(self, env):
- tests = [
+ @pytest.mark.parametrize(
+ "should_fail,sig",
+ (
(True, "*foo, bar"),
(True, "*foo, *bar"),
(True, "**foo, *bar"),
(False, "*foo, **bar"),
(False, "*foo, bar=42, **baz"),
(False, "foo, *args, bar=23, **baz"),
- ]
- for should_fail, sig in tests:
- if should_fail:
- with pytest.raises(TemplateSyntaxError):
- env.from_string(f"{{{{ foo({sig}) }}}}")
- else:
- env.from_string(f"foo({sig})")
-
- def test_tuple_expr(self, env):
- for tmpl in [
+ )
+ )
+ def test_function_calls(self, env, should_fail, sig):
+ if should_fail:
+ with pytest.raises(TemplateSyntaxError):
+ env.from_string(f"{{{{ foo({sig}) }}}}")
+ else:
+ env.from_string(f"foo({sig})")
+
+ @pytest.mark.parametrize(
+ "tmpl",
+ (
"{{ () }}",
"{{ (1, 2) }}",
"{{ (1, 2,) }}",
"{% for foo, bar in seq %}...{% endfor %}",
"{% for x in foo, bar %}...{% endfor %}",
"{% for x in foo, %}...{% endfor %}",
- ]:
- assert env.from_string(tmpl)
+ )
+ )
+ def test_tuple_expr(self, env, tmpl):
+ assert env.from_string(tmpl)
def test_trailing_comma(self, env):
tmpl = env.from_string("{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}")
with pytest.raises(TemplateRuntimeError):
t.render(ctx)
- def test_unary_operator_intercepting(self, env):
+ @pytest.mark.parametrize(
+ "expr,ctx,rv",
+ (
+ ("-1", {}, "-1"),
+ ("-a", {"a": 2}, "-2")
+ )
+ )
+ def test_unary_operator_intercepting(self, env, expr, ctx, rv):
def disable_op(arg):
raise TemplateRuntimeError("that operator so does not work")
- for expr, ctx, rv in ("-1", {}, "-1"), ("-a", {"a": 2}, "-2"):
- env = SandboxedEnvironment()
- env.unop_table["-"] = disable_op
- t = env.from_string(f"{{{{ {expr} }}}}")
- assert t.render(ctx) == rv
- env.intercepted_unops = frozenset(["-"])
- t = env.from_string(f"{{{{ {expr} }}}}")
- with pytest.raises(TemplateRuntimeError):
- t.render(ctx)
+ env = SandboxedEnvironment()
+ env.unop_table["-"] = disable_op
+ t = env.from_string(f"{{{{ {expr} }}}}")
+ assert t.render(ctx) == rv
+ env.intercepted_unops = frozenset(["-"])
+ t = env.from_string(f"{{{{ {expr} }}}}")
+ with pytest.raises(TemplateRuntimeError):
+ t.render(ctx)
class TestStringFormat: