~bzr-pqm/bzr/bzr.dev

5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
1
# Copyright (C) 2011 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests for bzrlib.i18n"""
18
6189.1.1 by Jelmer Vernooij
Add a load_plugin_translations method.
19
from bzrlib import (
20
    i18n,
21
    tests,
22
    errors,
23
    workingtree,
24
    )
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
25
26
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
27
class ZzzTranslations(object):
28
    """Special Zzz translation for debugging i18n stuff.
29
30
    This class can be used to confirm that the message is properly translated
31
    during black box tests.
32
    """
33
    _null_translation = i18n._gettext.NullTranslations()
34
35
    def zzz(self, s):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
36
        return u'zz\xe5{{%s}}' % s
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
37
38
    def ugettext(self, s):
39
        return self.zzz(self._null_translation.ugettext(s))
40
41
    def ungettext(self, s, p, n):
42
        return self.zzz(self._null_translation.ungettext(s, p, n))
43
44
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
45
class TestZzzTranslation(tests.TestCase):
46
47
    def _check_exact(self, expected, source):
48
        self.assertEqual(expected, source)
49
        self.assertEqual(type(expected), type(source))
50
51
    def test_translation(self):
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
52
        trans = ZzzTranslations()
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
53
54
        t = trans.zzz('msg')
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
55
        self._check_exact(u'zz\xe5{{msg}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
56
57
        t = trans.ugettext('msg')
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
58
        self._check_exact(u'zz\xe5{{msg}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
59
60
        t = trans.ungettext('msg1', 'msg2', 0)
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
61
        self._check_exact(u'zz\xe5{{msg2}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
62
        t = trans.ungettext('msg1', 'msg2', 2)
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
63
        self._check_exact(u'zz\xe5{{msg2}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
64
65
        t = trans.ungettext('msg1', 'msg2', 1)
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
66
        self._check_exact(u'zz\xe5{{msg1}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
67
68
69
class TestGetText(tests.TestCase):
70
71
    def setUp(self):
72
        super(TestGetText, self).setUp()
5875.3.28 by Vincent Ladeuil
Fix more test failures.
73
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
74
75
    def test_oneline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
76
        self.assertEqual(u"zz\xe5{{spam ham eggs}}",
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
77
                         i18n.gettext("spam ham eggs"))
78
79
    def test_multiline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
80
        self.assertEqual(u"zz\xe5{{spam\nham\n\neggs\n}}",
5875.2.10 by INADA Naoki
Add TestGetTextPerParagraph and fix error on TestGetText.test_multiline.
81
                         i18n.gettext("spam\nham\n\neggs\n"))
82
83
84
class TestGetTextPerParagraph(tests.TestCase):
85
86
    def setUp(self):
87
        super(TestGetTextPerParagraph, self).setUp()
5875.3.28 by Vincent Ladeuil
Fix more test failures.
88
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
5875.2.10 by INADA Naoki
Add TestGetTextPerParagraph and fix error on TestGetText.test_multiline.
89
90
    def test_oneline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
91
        self.assertEqual(u"zz\xe5{{spam ham eggs}}",
5875.2.10 by INADA Naoki
Add TestGetTextPerParagraph and fix error on TestGetText.test_multiline.
92
                         i18n.gettext_per_paragraph("spam ham eggs"))
93
94
    def test_multiline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
95
        self.assertEqual(u"zz\xe5{{spam\nham}}\n\nzz\xe5{{eggs\n}}",
5875.2.10 by INADA Naoki
Add TestGetTextPerParagraph and fix error on TestGetText.test_multiline.
96
                         i18n.gettext_per_paragraph("spam\nham\n\neggs\n"))
6025.1.1 by Jelmer Vernooij
Fix i18n use when no environment variables are set.
97
98
99
class TestInstall(tests.TestCase):
100
6133.3.11 by Jonathan Riddell
make TestInstall tests more consistent
101
    def setUp(self):
102
        super(TestInstall, self).setUp()
103
        # Restore a proper env to test translation installation
104
        self.overrideAttr(i18n, '_translations', None)
105
6025.1.1 by Jelmer Vernooij
Fix i18n use when no environment variables are set.
106
    def test_custom_languages(self):
107
        i18n.install('nl:fy')
6133.3.13 by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__
108
        # Whether we found a valid tranlsation or not doesn't matter, we got
109
        # one and _translations is not None anymore.
110
        self.assertIsInstance(i18n._translations,
111
                              i18n._gettext.NullTranslations)
6025.1.1 by Jelmer Vernooij
Fix i18n use when no environment variables are set.
112
113
    def test_no_env_variables(self):
114
        self.overrideEnv('LANGUAGE', None)
115
        self.overrideEnv('LC_ALL', None)
116
        self.overrideEnv('LC_MESSAGES', None)
117
        self.overrideEnv('LANG', None)
118
        i18n.install()
6133.3.13 by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__
119
        # Whether we found a valid tranlsation or not doesn't matter, we got
120
        # one and _translations is not None anymore.
121
        self.assertIsInstance(i18n._translations,
122
                              i18n._gettext.NullTranslations)
6112.5.3 by Jonathan Riddell
start a test case
123
6131.2.4 by Jonathan Riddell
add test for i18n.disable_i18n()
124
    def test_disable_i18n(self):
125
        i18n.disable_i18n()
126
        i18n.install()
6133.3.13 by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__
127
        # It's disabled, you can't install anything and we fallback to null
128
        self.assertIsInstance(i18n._translations,
129
                              i18n._gettext.NullTranslations)
6133.3.10 by Jonathan Riddell
default _translations back to None so we can tell if it gets installed
130
6131.2.4 by Jonathan Riddell
add test for i18n.disable_i18n()
131
6112.5.3 by Jonathan Riddell
start a test case
132
class TestTranslate(tests.TestCaseWithTransport):
133
134
    def setUp(self):
135
        super(TestTranslate, self).setUp()
136
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
137
138
    def test_error_message_translation(self):
6112.5.5 by Jonathan Riddell
finish test_error_message_translation()
139
        """do errors get translated?"""
6112.5.3 by Jonathan Riddell
start a test case
140
        err = None
141
        tree = self.make_branch_and_tree('.')
142
        try:
143
            workingtree.WorkingTree.open('./foo')
144
        except errors.NotBranchError,e:
145
            err = str(e)
6118.1.7 by Jonathan Riddell
merge in trunk
146
        self.assertContainsRe(err, 
147
                              u"zz\xe5{{Not a branch: .*}}".encode("utf-8"))
6110.7.7 by Jonathan Riddell
add a test for topic help translations
148
149
    def test_topic_help_translation(self):
150
        """does topic help get translated?"""
151
        from bzrlib import help
152
        from StringIO import StringIO
153
        out = StringIO()
154
        help.help("authentication", out)
6110.7.8 by Jonathan Riddell
merge in trunk
155
        self.assertContainsRe(out.getvalue(), "zz\xe5{{Authentication Settings")
6189.1.1 by Jelmer Vernooij
Add a load_plugin_translations method.
156
157
158
class LoadPluginTranslations(tests.TestCase):
159
160
    def test_does_not_exist(self):
161
        translation = i18n.load_plugin_translations("doesnotexist")
162
        self.assertEquals("foo", translation.gettext("foo"))