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