コード例 #1
0
    def test_render_index_filter(self):
        test_cases = [
            RenderCase(
                description="array of strings",
                template=r"{{ handles | index: 'foo' }}",
                expect="0",
                globals={"handles": ["foo", "bar"]},
                partials={},
            ),
            RenderCase(
                description="array of strings item does not exist",
                template=r"{{ handles | index: 'baz' }}",
                expect="",
                globals={"handles": ["foo", "bar"]},
                partials={},
            ),
        ]

        for case in test_cases:
            env = Environment()
            env.add_filter("index", index)
            env.loader = DictLoader(case.partials)
            template = env.from_string(case.template, globals=case.globals)

            with self.subTest(msg=case.description):
                result = template.render()
                self.assertEqual(result, case.expect)
コード例 #2
0
    def test_render_script_tag_with_autoescape(self):
        test_cases = [
            RenderCase(
                description="relative url",
                template=r"{{ url | script_tag }}",
                expect=(
                    '<script src="assets/assets/app.js" '
                    'type="text/javascript"></script>'
                ),
                globals={"url": "assets/assets/app.js"},
                partials={},
            ),
            RenderCase(
                description="unsafe url from context",
                template=r"{{ url | script_tag }}",
                expect=(
                    '<script src="&lt;b&gt;assets/assets/app.js&lt;/b&gt;" '
                    'type="text/javascript"></script>'
                ),
                globals={"url": "<b>assets/assets/app.js</b>"},
                partials={},
            ),
            RenderCase(
                description="safe url from context",
                template=r"{{ url | script_tag }}",
                expect=(
                    '<script src="&lt;b&gt;assets/assets/app.js&lt;/b&gt;" '
                    'type="text/javascript"></script>'
                ),
                globals={"url": Markup("<b>assets/assets/app.js</b>")},
                partials={},
            ),
        ]

        env = Environment(autoescape=True)
        env.add_filter("script_tag", script_tag)

        for case in test_cases:
            template = env.from_string(case.template, globals=case.globals)

            with self.subTest(msg=case.description):
                result = template.render()
                self.assertEqual(result, case.expect)
コード例 #3
0
    def test_render_stylesheet_tag_with_autoescape(self):
        test_cases = [
            RenderCase(
                description="relative url",
                template=r"{{ url | stylesheet_tag }}",
                expect=(
                    '<link href="assets/style.css" rel="stylesheet" '
                    'type="text/css" media="all" />'
                ),
                globals={"url": "assets/style.css"},
                partials={},
            ),
            RenderCase(
                description="unsafe url from context",
                template=r"{{ url | stylesheet_tag }}",
                expect=(
                    '<link href="&lt;b&gt;assets/style.css&lt;/b&gt;" rel="stylesheet" '
                    'type="text/css" media="all" />'
                ),
                globals={"url": "<b>assets/style.css</b>"},
                partials={},
            ),
            RenderCase(
                description="safe url from context",
                template=r"{{ url | stylesheet_tag }}",
                expect=(
                    '<link href="&lt;b&gt;assets/style.css&lt;/b&gt;" rel="stylesheet" '
                    'type="text/css" media="all" />'
                ),
                globals={"url": Markup("<b>assets/style.css</b>")},
                partials={},
            ),
        ]

        env = Environment(autoescape=True)
        env.add_filter("stylesheet_tag", stylesheet_tag)

        for case in test_cases:
            template = env.from_string(case.template, globals=case.globals)

            with self.subTest(msg=case.description):
                result = template.render()
                self.assertEqual(result, case.expect)
コード例 #4
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))
コード例 #5
0
    def test_render_stylesheet_tag_filter(self):
        test_cases = [
            RenderCase(
                description="from context variable",
                template=r"{{ url | stylesheet_tag }}",
                expect=(
                    '<link href="assets/style.css" rel="stylesheet" '
                    'type="text/css" media="all" />'
                ),
                globals={"url": "assets/style.css"},
                partials={},
            ),
        ]

        for case in test_cases:
            env = Environment()
            env.add_filter("stylesheet_tag", stylesheet_tag)
            env.loader = DictLoader(case.partials)
            template = env.from_string(case.template, globals=case.globals)

            with self.subTest(msg=case.description):
                result = template.render()
                self.assertEqual(result, case.expect)
コード例 #6
0
    def test_render_script_tag_filter(self):
        test_cases = [
            RenderCase(
                description="url from context",
                template=r"{{ url | script_tag }}",
                expect=(
                    '<script src="assets/assets/app.js" '
                    'type="text/javascript"></script>'
                ),
                globals={"url": "assets/assets/app.js"},
                partials={},
            ),
        ]

        # Test new style filter implementation
        for case in test_cases:
            env = Environment()
            env.add_filter("script_tag", script_tag)
            env.loader = DictLoader(case.partials)
            template = env.from_string(case.template, globals=case.globals)

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