• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import io
2import os
3
4import pytest
5
6from mako.testing.assertions import eq_
7from mako.testing.config import config
8from mako.testing.exclusions import requires_babel
9from mako.testing.fixtures import TemplateTest
10
11
12class UsesExtract:
13    @pytest.fixture(scope="class")
14    def extract(self):
15        from mako.ext.babelplugin import extract
16
17        return extract
18
19
20@requires_babel
21class PluginExtractTest(UsesExtract):
22    def test_parse_python_expression(self, extract):
23        input_ = io.BytesIO(b'<p>${_("Message")}</p>')
24        messages = list(extract(input_, ["_"], [], {}))
25        eq_(messages, [(1, "_", ("Message"), [])])
26
27    def test_python_gettext_call(self, extract):
28        input_ = io.BytesIO(b'<p>${_("Message")}</p>')
29        messages = list(extract(input_, ["_"], [], {}))
30        eq_(messages, [(1, "_", ("Message"), [])])
31
32    def test_translator_comment(self, extract):
33        input_ = io.BytesIO(
34            b"""
35        <p>
36          ## TRANSLATORS: This is a comment.
37          ${_("Message")}
38        </p>"""
39        )
40        messages = list(extract(input_, ["_"], ["TRANSLATORS:"], {}))
41        eq_(
42            messages,
43            [
44                (
45                    4,
46                    "_",
47                    ("Message"),
48                    [("TRANSLATORS: This is a comment.")],
49                )
50            ],
51        )
52
53
54@requires_babel
55class MakoExtractTest(UsesExtract, TemplateTest):
56    def test_extract(self, extract):
57        with open(
58            os.path.join(config.template_base, "gettext.mako")
59        ) as mako_tmpl:
60            messages = list(
61                extract(
62                    mako_tmpl,
63                    {"_": None, "gettext": None, "ungettext": (1, 2)},
64                    ["TRANSLATOR:"],
65                    {},
66                )
67            )
68            expected = [
69                (1, "_", "Page arg 1", []),
70                (1, "_", "Page arg 2", []),
71                (10, "gettext", "Begin", []),
72                (14, "_", "Hi there!", ["TRANSLATOR: Hi there!"]),
73                (19, "_", "Hello", []),
74                (22, "_", "Welcome", []),
75                (25, "_", "Yo", []),
76                (36, "_", "The", ["TRANSLATOR: Ensure so and", "so, thanks"]),
77                (36, "ungettext", ("bunny", "bunnies", None), []),
78                (41, "_", "Goodbye", ["TRANSLATOR: Good bye"]),
79                (44, "_", "Babel", []),
80                (45, "ungettext", ("hella", "hellas", None), []),
81                (62, "_", "The", ["TRANSLATOR: Ensure so and", "so, thanks"]),
82                (62, "ungettext", ("bunny", "bunnies", None), []),
83                (68, "_", "Goodbye, really!", ["TRANSLATOR: HTML comment"]),
84                (71, "_", "P.S. byebye", []),
85                (77, "_", "Top", []),
86                (83, "_", "foo", []),
87                (83, "_", "hoho", []),
88                (85, "_", "bar", []),
89                (92, "_", "Inside a p tag", ["TRANSLATOR: <p> tag is ok?"]),
90                (95, "_", "Later in a p tag", ["TRANSLATOR: also this"]),
91                (99, "_", "No action at a distance.", []),
92            ]
93        eq_(expected, messages)
94
95    def test_extract_utf8(self, extract):
96        with open(
97            os.path.join(config.template_base, "gettext_utf8.mako"), "rb"
98        ) as mako_tmpl:
99            message = next(
100                extract(mako_tmpl, {"_", None}, [], {"encoding": "utf-8"})
101            )
102            assert message == (1, "_", "K\xf6ln", [])
103
104    def test_extract_cp1251(self, extract):
105        with open(
106            os.path.join(config.template_base, "gettext_cp1251.mako"), "rb"
107        ) as mako_tmpl:
108            message = next(
109                extract(mako_tmpl, {"_", None}, [], {"encoding": "cp1251"})
110            )
111            # "test" in Rusian. File encoding is cp1251 (aka "windows-1251")
112            assert message == (1, "_", "\u0442\u0435\u0441\u0442", [])
113