cmlenz@162: # -*- coding: utf-8 -*- cmlenz@162: # jruigrok@532: # Copyright (C) 2007-2011 Edgewall Software cmlenz@162: # All rights reserved. cmlenz@162: # cmlenz@162: # This software is licensed as described in the file COPYING, which cmlenz@162: # you should have received as part of this distribution. The terms cmlenz@162: # are also available at http://babel.edgewall.org/wiki/License. cmlenz@162: # cmlenz@162: # This software consists of voluntary contributions made by many cmlenz@162: # individuals. For the exact contribution history, see the revision cmlenz@162: # history and logs, available at http://babel.edgewall.org/log/. cmlenz@162: cmlenz@162: import doctest pjenvey@251: import gettext cmlenz@336: import os cmlenz@162: import unittest pjenvey@251: from StringIO import StringIO cmlenz@162: pjenvey@251: from babel.messages import mofile, Catalog pjenvey@251: cmlenz@336: cmlenz@336: class ReadMoTestCase(unittest.TestCase): cmlenz@336: cmlenz@336: def setUp(self): cmlenz@336: self.datadir = os.path.join(os.path.dirname(__file__), 'data') cmlenz@336: cmlenz@336: def test_basics(self): cmlenz@336: mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de', cmlenz@336: 'LC_MESSAGES', 'messages.mo')) cmlenz@336: try: cmlenz@336: catalog = mofile.read_mo(mo_file) cmlenz@336: self.assertEqual(2, len(catalog)) cmlenz@336: self.assertEqual('TestProject', catalog.project) cmlenz@336: self.assertEqual('0.1', catalog.version) cmlenz@336: self.assertEqual('Stange', catalog['bar'].string) cmlenz@336: self.assertEqual(['Fuhstange', 'Fuhstangen'], cmlenz@336: catalog['foobar'].string) cmlenz@336: finally: cmlenz@336: mo_file.close() cmlenz@336: cmlenz@336: pjenvey@251: class WriteMoTestCase(unittest.TestCase): pjenvey@251: pjenvey@251: def test_sorting(self): pjenvey@251: # Ensure the header is sorted to the first entry so that its charset pjenvey@251: # can be applied to all subsequent messages by GNUTranslations pjenvey@251: # (ensuring all messages are safely converted to unicode) pjenvey@251: catalog = Catalog(locale='en_US') pjenvey@251: catalog.add(u'', '''\ pjenvey@251: "Content-Type: text/plain; charset=utf-8\n" pjenvey@251: "Content-Transfer-Encoding: 8bit\n''') pjenvey@251: catalog.add(u'foo', 'Voh') pjenvey@251: catalog.add((u'There is', u'There are'), (u'Es gibt', u'Es gibt')) pjenvey@251: catalog.add(u'Fizz', '') pjenvey@251: catalog.add(('Fuzz', 'Fuzzes'), ('', '')) pjenvey@251: buf = StringIO() pjenvey@251: mofile.write_mo(buf, catalog) pjenvey@251: buf.seek(0) pjenvey@251: translations = gettext.GNUTranslations(fp=buf) pjenvey@251: self.assertEqual(u'Voh', translations.ugettext('foo')) pjenvey@251: assert isinstance(translations.ugettext('foo'), unicode) pjenvey@251: self.assertEqual(u'Es gibt', translations.ungettext('There is', 'There are', 1)) pjenvey@251: assert isinstance(translations.ungettext('There is', 'There are', 1), unicode) pjenvey@251: self.assertEqual(u'Fizz', translations.ugettext('Fizz')) pjenvey@251: assert isinstance(translations.ugettext('Fizz'), unicode) pjenvey@251: self.assertEqual(u'Fuzz', translations.ugettext('Fuzz')) pjenvey@251: assert isinstance(translations.ugettext('Fuzz'), unicode) pjenvey@251: self.assertEqual(u'Fuzzes', translations.ugettext('Fuzzes')) pjenvey@251: assert isinstance(translations.ugettext('Fuzzes'), unicode) cmlenz@162: cmlenz@332: def test_more_plural_forms(self): cmlenz@332: catalog2 = Catalog(locale='ru_RU') cmlenz@332: catalog2.add(('Fuzz', 'Fuzzes'), ('', '', '')) cmlenz@332: buf = StringIO() cmlenz@332: mofile.write_mo(buf, catalog2) cmlenz@332: cmlenz@332: cmlenz@162: def suite(): cmlenz@162: suite = unittest.TestSuite() fschwarz@546: suite.addTest(doctest.DocTestSuite(mofile, optionflags=doctest.ELLIPSIS)) cmlenz@336: suite.addTest(unittest.makeSuite(ReadMoTestCase)) pjenvey@251: suite.addTest(unittest.makeSuite(WriteMoTestCase)) cmlenz@162: return suite cmlenz@162: cmlenz@162: if __name__ == '__main__': cmlenz@162: unittest.main(defaultTest='suite')