ยปCore Development>Code coverage>Lib/test/test_tools/test_i18n.py

Python code coverage for Lib/test/test_tools/test_i18n.py

#countcontent
1n/a"""Tests to cover the Tools/i18n package"""
2n/a
3n/aimport os
4n/aimport unittest
5n/a
6n/afrom test.support.script_helper import assert_python_ok
7n/afrom test.test_tools import skip_if_missing, toolsdir
8n/afrom test.support import temp_cwd
9n/a
10n/a
11n/askip_if_missing()
12n/a
13n/a
14n/aclass Test_pygettext(unittest.TestCase):
15n/a """Tests for the pygettext.py tool"""
16n/a
17n/a script = os.path.join(toolsdir,'i18n', 'pygettext.py')
18n/a
19n/a def get_header(self, data):
20n/a """ utility: return the header of a .po file as a dictionary """
21n/a headers = {}
22n/a for line in data.split('\n'):
23n/a if not line or line.startswith(('#', 'msgid','msgstr')):
24n/a continue
25n/a line = line.strip('"')
26n/a key, val = line.split(':',1)
27n/a headers[key] = val.strip()
28n/a return headers
29n/a
30n/a def test_header(self):
31n/a """Make sure the required fields are in the header, according to:
32n/a http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry
33n/a """
34n/a with temp_cwd(None) as cwd:
35n/a assert_python_ok(self.script)
36n/a with open('messages.pot') as fp:
37n/a data = fp.read()
38n/a header = self.get_header(data)
39n/a
40n/a self.assertIn("Project-Id-Version", header)
41n/a self.assertIn("POT-Creation-Date", header)
42n/a self.assertIn("PO-Revision-Date", header)
43n/a self.assertIn("Last-Translator", header)
44n/a self.assertIn("Language-Team", header)
45n/a self.assertIn("MIME-Version", header)
46n/a self.assertIn("Content-Type", header)
47n/a self.assertIn("Content-Transfer-Encoding", header)
48n/a self.assertIn("Generated-By", header)
49n/a
50n/a # not clear if these should be required in POT (template) files
51n/a #self.assertIn("Report-Msgid-Bugs-To", header)
52n/a #self.assertIn("Language", header)
53n/a
54n/a #"Plural-Forms" is optional
55n/a
56n/a
57n/a def test_POT_Creation_Date(self):
58n/a """ Match the date format from xgettext for POT-Creation-Date """
59n/a from datetime import datetime
60n/a with temp_cwd(None) as cwd:
61n/a assert_python_ok(self.script)
62n/a with open('messages.pot') as fp:
63n/a data = fp.read()
64n/a header = self.get_header(data)
65n/a creationDate = header['POT-Creation-Date']
66n/a
67n/a # peel off the escaped newline at the end of string
68n/a if creationDate.endswith('\\n'):
69n/a creationDate = creationDate[:-len('\\n')]
70n/a
71n/a # This will raise if the date format does not exactly match.
72n/a datetime.strptime(creationDate, '%Y-%m-%d %H:%M%z')