annotate babel/messages/tests/mofile.py @ 530:85e1beadacb0

Update the copyright line.
author jruigrok
date Sat, 05 Mar 2011 15:22:28 +0000
parents 9c41fe73e2e6
children 030ddf3f5b13
rev   line source
160
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
2 #
530
85e1beadacb0 Update the copyright line.
jruigrok
parents: 335
diff changeset
3 # Copyright (C) 2007-2011 Edgewall Software
160
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
4 # All rights reserved.
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
5 #
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
9 #
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
13
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
14 import doctest
249
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
15 import gettext
334
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
16 import os
160
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
17 import unittest
249
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
18 from StringIO import StringIO
160
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
19
249
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
20 from babel.messages import mofile, Catalog
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
21
334
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
22
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
23 class ReadMoTestCase(unittest.TestCase):
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
24
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
25 def setUp(self):
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
26 self.datadir = os.path.join(os.path.dirname(__file__), 'data')
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
27
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
28 def test_basics(self):
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
29 mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de',
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
30 'LC_MESSAGES', 'messages.mo'))
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
31 try:
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
32 catalog = mofile.read_mo(mo_file)
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
33 self.assertEqual(2, len(catalog))
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
34 self.assertEqual('TestProject', catalog.project)
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
35 self.assertEqual('0.1', catalog.version)
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
36 self.assertEqual('Stange', catalog['bar'].string)
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
37 self.assertEqual(['Fuhstange', 'Fuhstangen'],
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
38 catalog['foobar'].string)
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
39 finally:
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
40 mo_file.close()
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
41
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
42
249
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
43 class WriteMoTestCase(unittest.TestCase):
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
44
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
45 def test_sorting(self):
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
46 # Ensure the header is sorted to the first entry so that its charset
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
47 # can be applied to all subsequent messages by GNUTranslations
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
48 # (ensuring all messages are safely converted to unicode)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
49 catalog = Catalog(locale='en_US')
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
50 catalog.add(u'', '''\
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
51 "Content-Type: text/plain; charset=utf-8\n"
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
52 "Content-Transfer-Encoding: 8bit\n''')
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
53 catalog.add(u'foo', 'Voh')
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
54 catalog.add((u'There is', u'There are'), (u'Es gibt', u'Es gibt'))
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
55 catalog.add(u'Fizz', '')
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
56 catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
57 buf = StringIO()
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
58 mofile.write_mo(buf, catalog)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
59 buf.seek(0)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
60 translations = gettext.GNUTranslations(fp=buf)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
61 self.assertEqual(u'Voh', translations.ugettext('foo'))
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
62 assert isinstance(translations.ugettext('foo'), unicode)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
63 self.assertEqual(u'Es gibt', translations.ungettext('There is', 'There are', 1))
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
64 assert isinstance(translations.ungettext('There is', 'There are', 1), unicode)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
65 self.assertEqual(u'Fizz', translations.ugettext('Fizz'))
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
66 assert isinstance(translations.ugettext('Fizz'), unicode)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
67 self.assertEqual(u'Fuzz', translations.ugettext('Fuzz'))
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
68 assert isinstance(translations.ugettext('Fuzz'), unicode)
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
69 self.assertEqual(u'Fuzzes', translations.ugettext('Fuzzes'))
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
70 assert isinstance(translations.ugettext('Fuzzes'), unicode)
160
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
71
330
5f67aa47609c Fix for #97, compilation of message catalogs for locales with more than two plural forms where the translations were empty was failing.
cmlenz
parents: 249
diff changeset
72 def test_more_plural_forms(self):
5f67aa47609c Fix for #97, compilation of message catalogs for locales with more than two plural forms where the translations were empty was failing.
cmlenz
parents: 249
diff changeset
73 catalog2 = Catalog(locale='ru_RU')
5f67aa47609c Fix for #97, compilation of message catalogs for locales with more than two plural forms where the translations were empty was failing.
cmlenz
parents: 249
diff changeset
74 catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
5f67aa47609c Fix for #97, compilation of message catalogs for locales with more than two plural forms where the translations were empty was failing.
cmlenz
parents: 249
diff changeset
75 buf = StringIO()
5f67aa47609c Fix for #97, compilation of message catalogs for locales with more than two plural forms where the translations were empty was failing.
cmlenz
parents: 249
diff changeset
76 mofile.write_mo(buf, catalog2)
5f67aa47609c Fix for #97, compilation of message catalogs for locales with more than two plural forms where the translations were empty was failing.
cmlenz
parents: 249
diff changeset
77
5f67aa47609c Fix for #97, compilation of message catalogs for locales with more than two plural forms where the translations were empty was failing.
cmlenz
parents: 249
diff changeset
78
160
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
79 def suite():
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
80 suite = unittest.TestSuite()
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
81 suite.addTest(doctest.DocTestSuite(mofile))
334
aa8457401353 Add basic MO file reading in preparation for #54.
cmlenz
parents: 330
diff changeset
82 suite.addTest(unittest.makeSuite(ReadMoTestCase))
249
00960ab0a631 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 160
diff changeset
83 suite.addTest(unittest.makeSuite(WriteMoTestCase))
160
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
84 return suite
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
85
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
86 if __name__ == '__main__':
80e51aabc440 Add MO file generation. Closes #21.
cmlenz
parents:
diff changeset
87 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software