From afc3d36c3a4d58ec2bdf50dedc81c4e45370c64e Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 30 Dec 2015 19:29:48 +0200 Subject: [PATCH] Move JavaScript extraction test cases into separate file And remove unittest boilerplate while at it --- tests/messages/test_extract.py | 91 ---------------------------- tests/messages/test_js_extract.py | 99 +++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 91 deletions(-) create mode 100644 tests/messages/test_js_extract.py diff --git a/tests/messages/test_extract.py b/tests/messages/test_extract.py index fa03207c4..cf6162d15 100644 --- a/tests/messages/test_extract.py +++ b/tests/messages/test_extract.py @@ -388,97 +388,6 @@ def test_extract_strip_comment_tags(self): u'a prefix too'], messages[1][2]) -class ExtractJavaScriptTestCase(unittest.TestCase): - - def test_simple_extract(self): - buf = BytesIO(b"""\ -msg1 = _('simple') -msg2 = gettext('simple') -msg3 = ngettext('s', 'p', 42) - """) - messages = \ - list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS, - [], {})) - - self.assertEqual([(1, 'simple', [], None), - (2, 'simple', [], None), - (3, ('s', 'p'), [], None)], messages) - - def test_various_calls(self): - buf = BytesIO(b"""\ -msg1 = _(i18n_arg.replace(/"/, '"')) -msg2 = ungettext(i18n_arg.replace(/"/, '"'), multi_arg.replace(/"/, '"'), 2) -msg3 = ungettext("Babel", multi_arg.replace(/"/, '"'), 2) -msg4 = ungettext(i18n_arg.replace(/"/, '"'), "Babels", 2) -msg5 = ungettext('bunny', 'bunnies', parseInt(Math.random() * 2 + 1)) -msg6 = ungettext(arg0, 'bunnies', rparseInt(Math.random() * 2 + 1)) -msg7 = _(hello.there) -msg8 = gettext('Rabbit') -msg9 = dgettext('wiki', model.addPage()) -msg10 = dngettext(domain, 'Page', 'Pages', 3) -""") - messages = \ - list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS, [], - {})) - self.assertEqual([(5, (u'bunny', u'bunnies'), [], None), - (8, u'Rabbit', [], None), - (10, (u'Page', u'Pages'), [], None)], messages) - - def test_message_with_line_comment(self): - buf = BytesIO(u"""\ -// NOTE: hello -msg = _('Bonjour à tous') -""".encode('utf-8')) - messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) - self.assertEqual(u'Bonjour à tous', messages[0][2]) - self.assertEqual([u'NOTE: hello'], messages[0][3]) - - def test_message_with_multiline_comment(self): - buf = BytesIO(u"""\ -/* NOTE: hello - and bonjour - and servus */ -msg = _('Bonjour à tous') -""".encode('utf-8')) - messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) - self.assertEqual(u'Bonjour à tous', messages[0][2]) - self.assertEqual([u'NOTE: hello', 'and bonjour', ' and servus'], messages[0][3]) - - def test_ignore_function_definitions(self): - buf = BytesIO(b"""\ -function gettext(value) { - return translations[language][value] || value; -}""") - - messages = list(extract.extract_javascript(buf, ('gettext',), [], {})) - self.assertEqual(messages, []) - - def test_misplaced_comments(self): - buf = BytesIO(b"""\ -/* NOTE: this won't show up */ -foo() - -/* NOTE: this will */ -msg = _('Something') - -// NOTE: this will show up -// too. -msg = _('Something else') - -// NOTE: but this won't -bar() - -_('no comment here') -""") - messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) - self.assertEqual(u'Something', messages[0][2]) - self.assertEqual([u'NOTE: this will'], messages[0][3]) - self.assertEqual(u'Something else', messages[1][2]) - self.assertEqual([u'NOTE: this will show up', 'too.'], messages[1][3]) - self.assertEqual(u'no comment here', messages[2][2]) - self.assertEqual([], messages[2][3]) - - class ExtractTestCase(unittest.TestCase): def test_invalid_filter(self): diff --git a/tests/messages/test_js_extract.py b/tests/messages/test_js_extract.py new file mode 100644 index 000000000..299240e21 --- /dev/null +++ b/tests/messages/test_js_extract.py @@ -0,0 +1,99 @@ +# -- encoding: UTF-8 -- +from babel._compat import BytesIO +from babel.messages import extract + + +def test_simple_extract(): + buf = BytesIO(b"""\ +msg1 = _('simple') +msg2 = gettext('simple') +msg3 = ngettext('s', 'p', 42) + """) + messages = \ + list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS, + [], {})) + + assert messages == [(1, 'simple', [], None), + (2, 'simple', [], None), + (3, ('s', 'p'), [], None)] + + +def test_various_calls(): + buf = BytesIO(b"""\ +msg1 = _(i18n_arg.replace(/"/, '"')) +msg2 = ungettext(i18n_arg.replace(/"/, '"'), multi_arg.replace(/"/, '"'), 2) +msg3 = ungettext("Babel", multi_arg.replace(/"/, '"'), 2) +msg4 = ungettext(i18n_arg.replace(/"/, '"'), "Babels", 2) +msg5 = ungettext('bunny', 'bunnies', parseInt(Math.random() * 2 + 1)) +msg6 = ungettext(arg0, 'bunnies', rparseInt(Math.random() * 2 + 1)) +msg7 = _(hello.there) +msg8 = gettext('Rabbit') +msg9 = dgettext('wiki', model.addPage()) +msg10 = dngettext(domain, 'Page', 'Pages', 3) +""") + messages = \ + list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS, [], + {})) + assert messages == [ + (5, (u'bunny', u'bunnies'), [], None), + (8, u'Rabbit', [], None), + (10, (u'Page', u'Pages'), [], None) + ] + + +def test_message_with_line_comment(): + buf = BytesIO(u"""\ +// NOTE: hello +msg = _('Bonjour à tous') +""".encode('utf-8')) + messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) + assert messages[0][2] == u'Bonjour à tous' + assert messages[0][3] == [u'NOTE: hello'] + + +def test_message_with_multiline_comment(): + buf = BytesIO(u"""\ +/* NOTE: hello +and bonjour + and servus */ +msg = _('Bonjour à tous') +""".encode('utf-8')) + messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) + assert messages[0][2] == u'Bonjour à tous' + assert messages[0][3] == [u'NOTE: hello', 'and bonjour', ' and servus'] + + +def test_ignore_function_definitions(): + buf = BytesIO(b"""\ +function gettext(value) { +return translations[language][value] || value; +}""") + + messages = list(extract.extract_javascript(buf, ('gettext',), [], {})) + assert not messages + + +def test_misplaced_comments(): + buf = BytesIO(b"""\ +/* NOTE: this won't show up */ +foo() + +/* NOTE: this will */ +msg = _('Something') + +// NOTE: this will show up +// too. +msg = _('Something else') + +// NOTE: but this won't +bar() + +_('no comment here') +""") + messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) + assert messages[0][2] == u'Something' + assert messages[0][3] == [u'NOTE: this will'] + assert messages[1][2] == u'Something else' + assert messages[1][3] == [u'NOTE: this will show up', 'too.'] + assert messages[2][2] == u'no comment here' + assert messages[2][3] == []