]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Switch some tests to be parameterized
authorKevin Brown <kevin@kevin-brown.com>
Sat, 16 May 2020 19:02:53 +0000 (15:02 -0400)
committerKevin Brown <kevin@kevin-brown.com>
Sat, 16 May 2020 19:02:53 +0000 (15:02 -0400)
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
tests/test_security.py

index c0257cf1d9a4660d5ac9600d34de751debdb6424..4aeb74a317e7fd6e5b2adb3d017210c029b2bebc 100644 (file)
@@ -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,} }}")
index 44ac47ab5efa1680b31714ad0912e21edcfb902a..a8a1f432d4e7a4c9c0c31cd8764ca5cfb759b132 100644 (file)
@@ -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: