示例#1
0
    def test_assign_expression_syntax_error(self):
        """Test that we handle syntax errors in `assign` expressions using inline
        `if`."""
        env = Environment()
        env.add_tag(InlineIfAssignTag)

        with self.assertRaises(LiquidSyntaxError):
            env.from_string(r"{% assign foo.bar = 'hello' %}")
示例#2
0
    def test_filter_value_error(self):
        """Test unexpected filter values."""
        env = Environment()
        env.add_tag(InlineIfStatement)

        template = env.from_string(r"{{ 123 | first }}")

        self.assertEqual(template.render(), "")

        # and render async
        async def coro(template: BoundTemplate):
            return await template.render_async()

        self.assertEqual(asyncio.run(coro(template)), "")
示例#3
0
    def test_filter_argument_error(self):
        """Test unexpected filter arguments."""
        env = Environment()
        env.add_tag(InlineIfStatement)

        template = env.from_string(r"{{ 123 | divided_by: 0 }}")

        with self.assertRaises(FilterArgumentError):
            template.render()

        # and render async
        async def coro(template: BoundTemplate):
            return await template.render_async()

        with self.assertRaises(FilterArgumentError):
            asyncio.run(coro(template))
示例#4
0
    def test_unknown_filter(self):
        """Test that unknown filters are handled properly."""
        env = Environment(strict_filters=True)
        env.add_tag(InlineIfStatement)

        template = env.from_string(r"{{ 'hello' | nosuchthing }}")

        with self.assertRaises(NoSuchFilterFunc):
            template.render()

        # and render async
        async def coro(template: BoundTemplate):
            return await template.render_async()

        with self.assertRaises(NoSuchFilterFunc):
            asyncio.run(coro(template))

        env.strict_filters = False
        self.assertEqual(template.render(), "hello")
        self.assertEqual(asyncio.run(coro(template)), "hello")
示例#5
0
    def test_unexpected_filter_exception(self):
        """Test unexpected filter exception."""
        env = Environment()
        env.add_tag(InlineIfStatement)

        def func():
            raise Exception(":(")

        env.add_filter("func", func)

        template = env.from_string(r"{{ 123 | func }}")

        with self.assertRaises(Error):
            template.render()

        # and render async
        async def coro(template: BoundTemplate):
            return await template.render_async()

        with self.assertRaises(Error):
            asyncio.run(coro(template))
示例#6
0
    def test_render_with_tag(self):
        """Test that we can render a `with` tag."""
        test_cases = [
            Case(
                description="block scoped variable",
                template=r"{{ x }}{% with x: 'foo' %}{{ x }}{% endwith %}{{ x }}",
                expect="foo",
            ),
            Case(
                description="block scoped alias",
                template=(
                    r"{% with p: collection.products.first %}"
                    r"{{ p.title }}"
                    r"{% endwith %}"
                    r"{{ p.title }}"
                    r"{{ collection.products.first.title }}"
                ),
                expect="A ShoeA Shoe",
                globals={"collection": {"products": [{"title": "A Shoe"}]}},
            ),
            Case(
                description="multiple block scoped variables",
                template=(
                    r"{% with a: 1, b: 3.4 %}"
                    r"{{ a }} + {{ b }} = {{ a | plus: b }}"
                    r"{% endwith %}"
                ),
                expect="1 + 3.4 = 4.4",
            ),
        ]

        env = Environment()
        env.add_tag(WithTag)

        for case in test_cases:
            with self.subTest(msg=case.description):
                template = env.from_string(case.template, globals=case.globals)
                self.assertEqual(template.render(), case.expect)
示例#7
0
    def test_render_echo_inline_if_expression(self):
        """Test that we correctly render echo expressions that use inline `if`."""
        test_cases = [
            RenderCase(
                description="string literal",
                template=r"{% echo 'hello' %}",
                expect="hello",
                globals={},
                partials={},
            ),
            RenderCase(
                description="string literal with true condition",
                template=r"{% echo 'hello' if true %}",
                expect="hello",
                globals={},
                partials={},
            ),
            RenderCase(
                description="default to undefined",
                template=r"{% echo 'hello' if false %}",
                expect="",
                globals={},
                partials={},
            ),
            RenderCase(
                description="early  filter",
                template=r"{% echo 'hello' | upcase if true %}",
                expect="HELLO",
                globals={},
                partials={},
            ),
            RenderCase(
                description="string literal with false condition and alternative",
                template=r"{% echo 'hello' if false else 'goodbye' %}",
                expect="goodbye",
                globals={},
                partials={},
            ),
            RenderCase(
                description="object and condition from context with tail filter",
                template=(r"{% echo greeting if settings.foo else 'bar' | upcase %}"),
                expect="HELLO",
                globals={"settings": {"foo": True}, "greeting": "hello"},
                partials={},
            ),
        ]

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

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

        # Same again using async path.

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

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

            with self.subTest(msg=case.description):
                template = env.from_string(case.template, globals=case.globals)
                result = asyncio.run(coro(template))
                self.assertEqual(result, case.expect)