Пример #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 __call__(
        self,
        key: object,
        *,
        context: Context,
        environment: Environment,
        **kwargs: Any,
    ) -> str:
        locale = context.resolve("locale", default="default")
        translations: Mapping[str, object] = self.locales.get(locale, {})

        key = str(key)
        path = key.split(".")

        val = get_item(translations, *path, default=key)  # type: ignore
        return environment.from_string(val).render(**kwargs)
Пример #5
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")
Пример #6
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))