summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Brown <kevin@kevin-brown.com>2020-05-16 15:02:53 -0400
committerKevin Brown <kevin@kevin-brown.com>2020-05-16 15:02:53 -0400
commitc97786d9ddcce48d50c99e0330937022f98d0db8 (patch)
tree2c594687c82a6fc0b44abd1211a85f529b03c1a1
parentf872dcf65b7fa4ef311ad99c6ec408862fec9fd1 (diff)
downloadjinja2-c97786d9ddcce48d50c99e0330937022f98d0db8.tar.gz
Switch some tests to be parameterized
Right now they are using a for loop which makes it more difficult to properly trace what iteration failed on. Additionally, it hides cases where multiple items in the loop fail but not all do.
-rw-r--r--tests/test_lexnparse.py33
-rw-r--r--tests/test_security.py26
2 files changed, 35 insertions, 24 deletions
diff --git a/tests/test_lexnparse.py b/tests/test_lexnparse.py
index c0257cf..4aeb74a 100644
--- a/tests/test_lexnparse.py
+++ b/tests/test_lexnparse.py
@@ -449,8 +449,9 @@ class TestSyntax:
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"),
@@ -466,16 +467,18 @@ class TestSyntax:
(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,) }}",
@@ -484,8 +487,10 @@ class TestSyntax:
"{% 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,} }}")
diff --git a/tests/test_security.py b/tests/test_security.py
index 44ac47a..a8a1f43 100644
--- a/tests/test_security.py
+++ b/tests/test_security.py
@@ -110,19 +110,25 @@ class TestSandbox:
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: