From: David Shea dshea@redhat.com
Make sure the test functions actually blow up on the errors they're supposed to be looking for. --- Makefile | 3 +- tests/unittests/test_translatable.py | 45 ++++++++++++++ tests/unittests/test_translated.py | 116 +++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 tests/unittests/test_translatable.py create mode 100644 tests/unittests/test_translated.py
diff --git a/Makefile b/Makefile index ff31c18..d9b633e 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,5 @@ all: @echo "nothing to build"
check: - tests/pylint/runpylint.py + PYTHONPATH=. tests/pylint/runpylint.py + python3 -m unittest discover tests/unittests diff --git a/tests/unittests/test_translatable.py b/tests/unittests/test_translatable.py new file mode 100644 index 0000000..ac53e3d --- /dev/null +++ b/tests/unittests/test_translatable.py @@ -0,0 +1,45 @@ +# Copyright (C) 2015 Red Hat, Inc. +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# the GNU General Public License v.2, or (at your option) any later version. +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY expressed or implied, including the implied warranties of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. You should have received a copy of the +# GNU General Public License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the +# source code or documentation are not subject to the GNU General Public +# License and may only be used or replicated with the express permission of +# Red Hat, Inc. +# +# Red Hat Author(s): David Shea dshea@redhat.com + +import unittest +from polib import POEntry + +from translatable.test_markup import test_markup +from translatable.test_comment import test_comment + +class TestMarkup(unittest.TestCase): + def test_ok(self): + # no markup + test_markup(POEntry(msgid="test string")) + + # internal markup + test_markup(POEntry(msgid="<b>test</b> string")) + + def test_unnecessary_markup(self): + self.assertRaises(AssertionError, test_markup, POEntry(msgid="<b>test string</b>")) + +class TestComment(unittest.TestCase): + def test_ok(self): + # Perfectly fine string + test_comment(POEntry(msgid="Hello, I am a test string")) + + # single-character string with a comment + test_comment(POEntry(msgid="c", comment="TRANSLATORS: 'c' to continue")) + + def test_no_comment(self): + self.assertRaises(AssertionError, test_comment, POEntry(msgid="c")) diff --git a/tests/unittests/test_translated.py b/tests/unittests/test_translated.py new file mode 100644 index 0000000..7011cb7 --- /dev/null +++ b/tests/unittests/test_translated.py @@ -0,0 +1,116 @@ +# Copyright (C) 2015 Red Hat, Inc. +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# the GNU General Public License v.2, or (at your option) any later version. +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY expressed or implied, including the implied warranties of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. You should have received a copy of the +# GNU General Public License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the +# source code or documentation are not subject to the GNU General Public # License and may only be used or replicated with the express permission of # Red Hat, Inc. +# +# Red Hat Author(s): David Shea dshea@redhat.com + +import unittest +import tempfile +import warnings +import shutil +import os +import polib + +from translated.test_markup import test_markup +from translated.test_percentage import test_percentage +from translated.test_usability import test_usability + +# convert a polib.MOFile into a NamedTemporaryFile +def mofile(moobj): + f = tempfile.NamedTemporaryFile(suffix='.mo') + moobj.save(f.name) + return f + +# convenience function for creating a single-entry mofile +def mofile_from_entry(*args, **kwargs): + moobj = polib.MOFile() + moobj.append(polib.MOEntry(*args, **kwargs)) + return mofile(moobj) + +class TestMarkup(unittest.TestCase): + def test_ok(self): + # no markup + with mofile_from_entry(msgid="test string", msgstr="estay ingstray") as m: + test_markup(m.name) + + # matching markup + with mofile_from_entry(msgid="<b>bold</b> string", msgstr="<b>oldbay</b> ingstray") as m: + test_markup(m.name) + + # matching plural + with mofile_from_entry(msgid="%d <b>bold</b> string", msgid_plural="%d <b>bold</b> strings", + msgstr_plural={0: "%d <b>oldbay</b> ingstray", 1: "%d <b>oldbay</b> instrays"}) as m: + test_markup(m.name) + + def test_missing(self): + with mofile_from_entry(msgid="<b>bold</b> string", msgstr="oldbay ingstray") as m: + self.assertRaises(AssertionError, test_markup, m.name) + + def test_mismatch(self): + with mofile_from_entry(msgid="<b>bold</b> string", msgstr="<i>oldbay</i> ingstray") as m: + self.assertRaises(AssertionError, test_markup, m.name) + + def test_typo(self): + with mofile_from_entry(msgid="<b>bold</b> string", msgstr="<boldbay</b> ingstray") as m: + self.assertRaises(AssertionError, test_markup, m.name) + + def test_mismatch_plural(self): + with mofile_from_entry(msgid="%d <b>bold</b> string", msgid_plural="%d <b>bold</b> strings", + msgstr_plural={0: "%d <b>olbday</b> ingstray", 1: "%d oldbay ingstrays"}) as m: + self.assertRaises(AssertionError, test_markup, m.name) + +class TestPercentage(unittest.TestCase): + # test_percentage actually looks at .po files, so the tests need to create + # both a .po and a .mo in self.tmpdir + + def setUp(self): + self.tmpdir = tempfile.mkdtemp() + self.popath = os.path.join(self.tmpdir, "test.po") + self.mopath = os.path.join(self.tmpdir, "test.mo") + warnings.simplefilter("error") + + def tearDown(self): + shutil.rmtree(self.tmpdir) + warnings.resetwarnings() + + def test_ok(self): + # 100% + pofile = polib.POFile() + pofile.append(polib.POEntry(msgid="test string", msgstr="estay ingstray")) + pofile.save(self.popath) + pofile.save_as_mofile(self.mopath) + test_percentage(self.mopath) + + def test_not_ok(self): + # 0% + pofile = polib.POFile() + pofile.append(polib.POEntry(msgid="test string", msgstr="")) + pofile.save(self.popath) + pofile.save_as_mofile(self.mopath) + +class TestUsability(unittest.TestCase): + def test_ok(self): + # what lt's Plural-Forms is supposed to look like + moobj = polib.MOFile() + moobj.metadata["Plural-Forms"] = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2)\n" + + with mofile(moobj) as m: + test_usability(m.name) + + def test_busted_plural_forms(self): + # https://bugzilla.redhat.com/show_bug.cgi?id=1283599 + moobj = polib.MOFile() + moobj.metadata["Plural-Forms"] = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 or n%100>=20) ? 1 : 2)\n" + + with mofile(moobj) as m: + self.assertRaises(Exception, test_usability, m.name)
anaconda-patches@lists.fedorahosted.org