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