Mercurial > babel > old > babel-test
annotate babel/messages/tests/mofile.py @ 544:030ddf3f5b13
catalog.add() now returns the message instance (closes #245)
author | fschwarz |
---|---|
date | Sat, 19 Mar 2011 19:28:59 +0000 |
parents | 85e1beadacb0 |
children |
rev | line source |
---|---|
160 | 1 # -*- coding: utf-8 -*- |
2 # | |
530 | 3 # Copyright (C) 2007-2011 Edgewall Software |
160 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
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 | 16 import os |
160 | 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 | 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 | 22 |
23 class ReadMoTestCase(unittest.TestCase): | |
24 | |
25 def setUp(self): | |
26 self.datadir = os.path.join(os.path.dirname(__file__), 'data') | |
27 | |
28 def test_basics(self): | |
29 mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de', | |
30 'LC_MESSAGES', 'messages.mo')) | |
31 try: | |
32 catalog = mofile.read_mo(mo_file) | |
33 self.assertEqual(2, len(catalog)) | |
34 self.assertEqual('TestProject', catalog.project) | |
35 self.assertEqual('0.1', catalog.version) | |
36 self.assertEqual('Stange', catalog['bar'].string) | |
37 self.assertEqual(['Fuhstange', 'Fuhstangen'], | |
38 catalog['foobar'].string) | |
39 finally: | |
40 mo_file.close() | |
41 | |
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 | 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 | 79 def suite(): |
80 suite = unittest.TestSuite() | |
544
030ddf3f5b13
catalog.add() now returns the message instance (closes #245)
fschwarz
parents:
530
diff
changeset
|
81 suite.addTest(doctest.DocTestSuite(mofile, optionflags=doctest.ELLIPSIS)) |
334 | 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 | 84 return suite |
85 | |
86 if __name__ == '__main__': | |
87 unittest.main(defaultTest='suite') |