From c97786d9ddcce48d50c99e0330937022f98d0db8 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 16 May 2020 15:02:53 -0400 Subject: 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. --- tests/test_lexnparse.py | 33 +++++++++++++++++++-------------- tests/test_security.py | 26 ++++++++++++++++---------- 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: -- cgit v1.2.1