Exemplo n.º 1
0
    def _test(self, test_cases):
        """Helper method for testing lists of test cases."""
        for case in test_cases:
            env = Environment(loader=DictLoader(case.partials))
            env.add_tag(IfNotTag)

            template = env.from_string(case.template, globals=case.globals)

            with self.subTest(msg=case.description):
                result = template.render()
                self.assertEqual(result, case.expect)
Exemplo n.º 2
0
    def test_render_macro_async(self):
        """Test that we can render a macro asynchronously."""
        env = Environment()
        env.add_tag(MacroTag)
        env.add_tag(CallTag)

        template = env.from_string(r"{% macro 'foo', you: 'World' %}"
                                   r"Hello, {{ you }}!"
                                   r"{% endmacro %}"
                                   r"{% call 'foo' %}"
                                   r"{% call 'foo', you: 'you' %}"
                                   r"{% call 'nosuchthing' %}")

        async def coro():
            return await template.render_async()

        result = asyncio.run(coro())
        self.assertEqual(result, "Hello, World!Hello, you!")
Exemplo n.º 3
0
    def test_render_macro_strict_undefined(self):
        """Test that missing arguments and undefined macros are `Undefined`."""
        test_cases = [
            RenderCase(
                description="missing argument",
                template=(r"{% macro 'func', foo %}"
                          r"{{ foo }}"
                          r"{% endmacro %}"
                          r"{% call 'func' %}"),
                expect="'foo' is undefined, on line 1",
                globals={},
                partials={},
            ),
            RenderCase(
                description="undefined macro",
                template=(r"{% call 'func' %}"),
                expect="'func' is undefined, on line 1",
                globals={},
                partials={},
            ),
        ]

        for case in test_cases:
            env = Environment(
                loader=DictLoader(case.partials),
                undefined=StrictUndefined,
            )
            env.add_tag(MacroTag)
            env.add_tag(CallTag)

            template = env.from_string(case.template, globals=case.globals)

            with self.subTest(msg=case.description):
                with self.assertRaises(UndefinedError) as raised:
                    template.render()
                self.assertEqual(case.expect, str(raised.exception))
Exemplo n.º 4
0
    def test_render_macro(self):
        """Test that we can render `macro` and `call` tags."""
        test_cases = [
            RenderCase(
                description="basic macro no call",
                template=r"{% macro 'func' %}Hello, World!{% endmacro %}",
                expect="",
                globals={},
                partials={},
            ),
            RenderCase(
                description="call basic macro",
                template=(r"{% macro 'func' %}Hello, World!{% endmacro %}"
                          r"{% call 'func' %}"),
                expect="Hello, World!",
                globals={},
                partials={},
            ),
            RenderCase(
                description="call basic macro multiple times",
                template=(r"{% macro 'func' %}Hello, World!{% endmacro %}"
                          r"{% call 'func' %}"
                          r"{% call 'func' %}"),
                expect="Hello, World!Hello, World!",
                globals={},
                partials={},
            ),
            RenderCase(
                description="call macro with argument",
                template=(
                    r"{% macro 'func', you %}Hello, {{ you }}!{% endmacro %}"
                    r"{% call 'func', 'you' %} "
                    r"{% call 'func', 'World' %}"),
                expect="Hello, you! Hello, World!",
                globals={},
                partials={},
            ),
            RenderCase(
                description="call macro with default argument",
                template=
                (r"{% macro 'func' you: 'brian' %}Hello, {{ you }}!{% endmacro %}"
                 r"{% call 'func' %} "
                 r"{% call 'func' you: 'World' %}"),
                expect="Hello, brian! Hello, World!",
                globals={},
                partials={},
            ),
            RenderCase(
                description="boolean literal default argument",
                template=(r"{% macro 'func' foo: false %}"
                          r"{% if foo %}Hello, World!{% endif %}"
                          r"{% endmacro %}"
                          r"{% call 'func' %}"
                          r"{% call 'func' foo: true %}"),
                expect="Hello, World!",
                globals={},
                partials={},
            ),
            RenderCase(
                description="chained default argument from context",
                template=(r"{% macro 'func' greeting: foo.bar %}"
                          r"{{ greeting }}, World!"
                          r"{% endmacro %}"
                          r"{% call 'func' %}"
                          r"{% call 'func' greeting: 'Goodbye' %}"),
                expect="Hello, World!Goodbye, World!",
                globals={"foo": {
                    "bar": "Hello"
                }},
                partials={},
            ),
            RenderCase(
                description="excess arguments",
                template=(r"{% macro 'func' %}"
                          r"{{ args | join: '-' }}"
                          r"{% endmacro %}"
                          r"{% call 'func' 1, 2 %}"),
                expect="1-2",
                globals={},
                partials={},
            ),
            RenderCase(
                description="excess keyword arguments",
                template=(r"{% macro 'func' %}"
                          r"{% for arg in kwargs %}"
                          r"{{ arg.0 }} => {{ arg.1 }}, "
                          r"{% endfor %}"
                          r"{% endmacro %}"
                          r"{% call 'func', a: 1, b: 2 %}"),
                expect="a => 1, b => 2, ",
                globals={},
                partials={},
            ),
            RenderCase(
                description="missing argument",
                template=(r"{% macro 'func', foo %}"
                          r"{{ foo }}"
                          r"{% endmacro %}"
                          r"{% call 'func' %}"),
                expect="",
                globals={},
                partials={},
            ),
            RenderCase(
                description="undefined macro",
                template=(r"{% call 'func' %}"),
                expect="",
                globals={},
                partials={},
            ),
            RenderCase(
                description="default before positional",
                template=(
                    r"{% macro 'func' you: 'brian', greeting %}"
                    r"{{ greeting }}, {{ you }}!"
                    r"{% endmacro %}"
                    r"{% call 'func' %} "
                    r"{% call 'func' you: 'World', greeting: 'Goodbye' %}"),
                expect=", brian! Goodbye, World!",
                globals={},
                partials={},
            ),
        ]

        for case in test_cases:
            env = Environment(loader=DictLoader(case.partials))
            env.add_tag(MacroTag)
            env.add_tag(CallTag)

            template = env.from_string(case.template, globals=case.globals)

            with self.subTest(msg=case.description):
                result = template.render()
                self.assertEqual(result, case.expect)