diff options
Diffstat (limited to 'test/ext/test_babelplugin.py')
-rw-r--r-- | test/ext/test_babelplugin.py | 143 |
1 files changed, 63 insertions, 80 deletions
diff --git a/test/ext/test_babelplugin.py b/test/ext/test_babelplugin.py index be3c37a..de3e461 100644 --- a/test/ext/test_babelplugin.py +++ b/test/ext/test_babelplugin.py @@ -1,39 +1,25 @@ import io import os -import unittest -from ..util.exclusions import skip_if -from ..util.fixtures import template_base -from ..util.fixtures import TemplateTest +from mako.ext.babelplugin import extract +from mako.testing.assertions import eq_ +from mako.testing.config import config +from mako.testing.exclusions import requires_babel +from mako.testing.fixtures import TemplateTest -try: - import babel.messages.extract as babel - from mako.ext.babelplugin import extract -except ImportError: - babel = None - - -def skip(): - return skip_if( - lambda: not babel, "babel not installed: skipping babelplugin test" - ) - - -class Test_extract(unittest.TestCase): - @skip() +@requires_babel +class PluginExtractTest: def test_parse_python_expression(self): input_ = io.BytesIO(b'<p>${_("Message")}</p>') messages = list(extract(input_, ["_"], [], {})) - self.assertEqual(messages, [(1, "_", ("Message"), [])]) + eq_(messages, [(1, "_", ("Message"), [])]) - @skip() def test_python_gettext_call(self): input_ = io.BytesIO(b'<p>${_("Message")}</p>') messages = list(extract(input_, ["_"], [], {})) - self.assertEqual(messages, [(1, "_", ("Message"), [])]) + eq_(messages, [(1, "_", ("Message"), [])]) - @skip() def test_translator_comment(self): input_ = io.BytesIO( b""" @@ -43,7 +29,7 @@ class Test_extract(unittest.TestCase): </p>""" ) messages = list(extract(input_, ["_"], ["TRANSLATORS:"], {})) - self.assertEqual( + eq_( messages, [ ( @@ -56,65 +42,62 @@ class Test_extract(unittest.TestCase): ) -class ExtractMakoTestCase(TemplateTest): - @skip() +@requires_babel +class MakoExtractTest(TemplateTest): def test_extract(self): - mako_tmpl = open(os.path.join(template_base, "gettext.mako")) - self.addCleanup(mako_tmpl.close) - messages = list( - extract( - mako_tmpl, - {"_": None, "gettext": None, "ungettext": (1, 2)}, - ["TRANSLATOR:"], - {}, + with open( + os.path.join(config.template_base, "gettext.mako") + ) as mako_tmpl: + messages = list( + extract( + mako_tmpl, + {"_": None, "gettext": None, "ungettext": (1, 2)}, + ["TRANSLATOR:"], + {}, + ) ) - ) - expected = [ - (1, "_", "Page arg 1", []), - (1, "_", "Page arg 2", []), - (10, "gettext", "Begin", []), - (14, "_", "Hi there!", ["TRANSLATOR: Hi there!"]), - (19, "_", "Hello", []), - (22, "_", "Welcome", []), - (25, "_", "Yo", []), - (36, "_", "The", ["TRANSLATOR: Ensure so and", "so, thanks"]), - (36, "ungettext", ("bunny", "bunnies", None), []), - (41, "_", "Goodbye", ["TRANSLATOR: Good bye"]), - (44, "_", "Babel", []), - (45, "ungettext", ("hella", "hellas", None), []), - (62, "_", "The", ["TRANSLATOR: Ensure so and", "so, thanks"]), - (62, "ungettext", ("bunny", "bunnies", None), []), - (68, "_", "Goodbye, really!", ["TRANSLATOR: HTML comment"]), - (71, "_", "P.S. byebye", []), - (77, "_", "Top", []), - (83, "_", "foo", []), - (83, "_", "hoho", []), - (85, "_", "bar", []), - (92, "_", "Inside a p tag", ["TRANSLATOR: <p> tag is ok?"]), - (95, "_", "Later in a p tag", ["TRANSLATOR: also this"]), - (99, "_", "No action at a distance.", []), - ] - self.assertEqual(expected, messages) + expected = [ + (1, "_", "Page arg 1", []), + (1, "_", "Page arg 2", []), + (10, "gettext", "Begin", []), + (14, "_", "Hi there!", ["TRANSLATOR: Hi there!"]), + (19, "_", "Hello", []), + (22, "_", "Welcome", []), + (25, "_", "Yo", []), + (36, "_", "The", ["TRANSLATOR: Ensure so and", "so, thanks"]), + (36, "ungettext", ("bunny", "bunnies", None), []), + (41, "_", "Goodbye", ["TRANSLATOR: Good bye"]), + (44, "_", "Babel", []), + (45, "ungettext", ("hella", "hellas", None), []), + (62, "_", "The", ["TRANSLATOR: Ensure so and", "so, thanks"]), + (62, "ungettext", ("bunny", "bunnies", None), []), + (68, "_", "Goodbye, really!", ["TRANSLATOR: HTML comment"]), + (71, "_", "P.S. byebye", []), + (77, "_", "Top", []), + (83, "_", "foo", []), + (83, "_", "hoho", []), + (85, "_", "bar", []), + (92, "_", "Inside a p tag", ["TRANSLATOR: <p> tag is ok?"]), + (95, "_", "Later in a p tag", ["TRANSLATOR: also this"]), + (99, "_", "No action at a distance.", []), + ] + eq_(expected, messages) - @skip() def test_extract_utf8(self): - mako_tmpl = open( - os.path.join(template_base, "gettext_utf8.mako"), "rb" - ) - self.addCleanup(mako_tmpl.close) - message = next( - extract(mako_tmpl, {"_", None}, [], {"encoding": "utf-8"}) - ) - assert message == (1, "_", "K\xf6ln", []) + with open( + os.path.join(config.template_base, "gettext_utf8.mako"), "rb" + ) as mako_tmpl: + message = next( + extract(mako_tmpl, {"_", None}, [], {"encoding": "utf-8"}) + ) + assert message == (1, "_", "K\xf6ln", []) - @skip() def test_extract_cp1251(self): - mako_tmpl = open( - os.path.join(template_base, "gettext_cp1251.mako"), "rb" - ) - self.addCleanup(mako_tmpl.close) - message = next( - extract(mako_tmpl, {"_", None}, [], {"encoding": "cp1251"}) - ) - # "test" in Rusian. File encoding is cp1251 (aka "windows-1251") - assert message == (1, "_", "\u0442\u0435\u0441\u0442", []) + with open( + os.path.join(config.template_base, "gettext_cp1251.mako"), "rb" + ) as mako_tmpl: + message = next( + extract(mako_tmpl, {"_", None}, [], {"encoding": "cp1251"}) + ) + # "test" in Rusian. File encoding is cp1251 (aka "windows-1251") + assert message == (1, "_", "\u0442\u0435\u0441\u0442", []) |