Debian Codegraph @

translation-canary/tests/unittests/test_translatable.py

raw symbols SCIP
1 # Copyright (C) 2015 Red Hat, Inc.
2 #
3 # This copyrighted material is made available to anyone wishing to use,
4 # modify, copy, or redistribute it subject to the terms and conditions of
5 # the GNU Lesser General Public License v.2, or (at your option) any later
6 # version. This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY expressed or implied, including the implied
8 # warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
9 # the GNU Lesser General Public License for more details. You should have
10 # received a copy of the GNU Lesser General Public License along with this
11 # program; if not, write to the Free Software Foundation, Inc., 51 Franklin
12 # Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks
13 # that are incorporated in the source code or documentation are not subject
14 # to the GNU Lesser General Public License and may only be used or
15 # replicated with the express permission of Red Hat, Inc.
16 #
17 # Red Hat Author(s): David Shea <[email protected]>
18
19 import unittest
20 import unittest.mock
21 from polib import POEntry, POFile
22 import tempfile
23
24 from translation_canary.translatable.test_markup import test_markup
25 from translation_canary.translatable.test_comment import test_comment
26
27 from translation_canary.translatable import testString, testPOT
28
29 class TestMarkup(unittest.TestCase):
30 def test_ok(self):
31 # no markup
32 test_markup(POEntry(msgid="test string"))
33
34 # internal markup
35 test_markup(POEntry(msgid="<b>test</b> string"))
36
37 def test_unnecessary_markup(self):
38 self.assertRaises(AssertionError, test_markup, POEntry(msgid="<b>test string</b>"))
39
40 class TestComment(unittest.TestCase):
41 def test_ok(self):
42 # Perfectly fine string
43 test_comment(POEntry(msgid="Hello, I am a test string"))
44
45 # single-character string with a comment
46 test_comment(POEntry(msgid="c", comment="TRANSLATORS: 'c' to continue"))
47
48 def test_no_comment(self):
49 self.assertRaises(AssertionError, test_comment, POEntry(msgid="c"))
50
51 # Test the top-level functions
52 # fake tests for testing with
53 def _true_test(_s):
54 pass
55
56 def _false_test(_s):
57 raise AssertionError("no good")
58
59 def _picky_test(s):
60 if s.msgstr.startswith("p"):
61 raise AssertionError("I don't like this one")
62
63 class TestTestString(unittest.TestCase):
64 @unittest.mock.patch("translation_canary.translatable._tests", [_true_test])
65 def test_success(self):
66 self.assertTrue(testString(POEntry()))
67
68 @unittest.mock.patch("translation_canary.translatable._tests", [_false_test])
69 def test_failure(self):
70 self.assertFalse(testString(POEntry()))
71
72 @unittest.mock.patch("translation_canary.translatable._tests", [_picky_test])
73 class TestTestPOT(unittest.TestCase):
74 def test_success(self):
75 with tempfile.NamedTemporaryFile(suffix=".pot") as potfile:
76 poobj = POFile()
77 poobj.append(POEntry(msgstr="test string"))
78 poobj.save(potfile.name)
79
80 self.assertTrue(testPOT(potfile.name))
81
82 def test_some_failure(self):
83 with tempfile.NamedTemporaryFile(suffix=".pot") as potfile:
84 poobj = POFile()
85 poobj.append(POEntry(msgstr="test string"))
86 poobj.append(POEntry(msgstr="pest string"))
87 poobj.save(potfile.name)
88
89 self.assertFalse(testPOT(potfile.name))
90
91 def test_all_failure(self):
92 with tempfile.NamedTemporaryFile(suffix=".pot") as potfile:
93 poobj = POFile()
94 poobj.append(POEntry(msgstr="pest string"))
95 poobj.append(POEntry(msgstr="past string"))
96 poobj.save(potfile.name)
97
98 self.assertFalse(testPOT(potfile.name))