~ubuntu-branches/ubuntu/trusty/python-babel/trusty

« back to all changes in this revision

Viewing changes to tests/messages/test_mofile.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-10-28 10:11:31 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20131028101131-zwbmm8sc29iemmlr
Tags: 1.3-2ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - debian/rules: Run the testsuite during builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2007-2011 Edgewall Software
 
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 os
 
15
import unittest
 
16
 
 
17
from babel.messages import mofile, Catalog
 
18
from babel._compat import BytesIO, text_type
 
19
from babel.support import Translations
 
20
 
 
21
 
 
22
class ReadMoTestCase(unittest.TestCase):
 
23
 
 
24
    def setUp(self):
 
25
        self.datadir = os.path.join(os.path.dirname(__file__), 'data')
 
26
 
 
27
    def test_basics(self):
 
28
        mo_path = os.path.join(self.datadir, 'project', 'i18n', 'de',
 
29
                               'LC_MESSAGES', 'messages.mo')
 
30
        mo_file = open(mo_path, 'rb')
 
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
 
 
43
class WriteMoTestCase(unittest.TestCase):
 
44
 
 
45
    def test_sorting(self):
 
46
        # Ensure the header is sorted to the first entry so that its charset
 
47
        # can be applied to all subsequent messages by GNUTranslations
 
48
        # (ensuring all messages are safely converted to unicode)
 
49
        catalog = Catalog(locale='en_US')
 
50
        catalog.add(u'', '''\
 
51
"Content-Type: text/plain; charset=utf-8\n"
 
52
"Content-Transfer-Encoding: 8bit\n''')
 
53
        catalog.add(u'foo', 'Voh')
 
54
        catalog.add((u'There is', u'There are'), (u'Es gibt', u'Es gibt'))
 
55
        catalog.add(u'Fizz', '')
 
56
        catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
 
57
        buf = BytesIO()
 
58
        mofile.write_mo(buf, catalog)
 
59
        buf.seek(0)
 
60
        translations = Translations(fp=buf)
 
61
        self.assertEqual(u'Voh', translations.ugettext('foo'))
 
62
        assert isinstance(translations.ugettext('foo'), text_type)
 
63
        self.assertEqual(u'Es gibt', translations.ungettext('There is', 'There are', 1))
 
64
        assert isinstance(translations.ungettext('There is', 'There are', 1), text_type)
 
65
        self.assertEqual(u'Fizz', translations.ugettext('Fizz'))
 
66
        assert isinstance(translations.ugettext('Fizz'), text_type)
 
67
        self.assertEqual(u'Fuzz', translations.ugettext('Fuzz'))
 
68
        assert isinstance(translations.ugettext('Fuzz'), text_type)
 
69
        self.assertEqual(u'Fuzzes', translations.ugettext('Fuzzes'))
 
70
        assert isinstance(translations.ugettext('Fuzzes'), text_type)
 
71
 
 
72
    def test_more_plural_forms(self):
 
73
        catalog2 = Catalog(locale='ru_RU')
 
74
        catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
 
75
        buf = BytesIO()
 
76
        mofile.write_mo(buf, catalog2)