annotate babel/messages/tests/mofile.py @ 546:10de195cfb04

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