~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
6112.5.8 by Jonathan Riddell
format imports
19
from bzrlib import (i18n, 
20
                    tests, 
21
                    errors, 
22
                    workingtree,
23
                    )
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
24
25
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
26
class ZzzTranslations(object):
27
    """Special Zzz translation for debugging i18n stuff.
28
29
    This class can be used to confirm that the message is properly translated
30
    during black box tests.
31
    """
32
    _null_translation = i18n._gettext.NullTranslations()
33
34
    def zzz(self, s):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
35
        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.
36
37
    def ugettext(self, s):
38
        return self.zzz(self._null_translation.ugettext(s))
39
40
    def ungettext(self, s, p, n):
41
        return self.zzz(self._null_translation.ungettext(s, p, n))
42
43
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
44
class TestZzzTranslation(tests.TestCase):
45
46
    def _check_exact(self, expected, source):
47
        self.assertEqual(expected, source)
48
        self.assertEqual(type(expected), type(source))
49
50
    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.
51
        trans = ZzzTranslations()
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
52
53
        t = trans.zzz('msg')
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
54
        self._check_exact(u'zz\xe5{{msg}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
55
56
        t = trans.ugettext('msg')
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
57
        self._check_exact(u'zz\xe5{{msg}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
58
59
        t = trans.ungettext('msg1', 'msg2', 0)
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
60
        self._check_exact(u'zz\xe5{{msg2}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
61
        t = trans.ungettext('msg1', 'msg2', 2)
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
62
        self._check_exact(u'zz\xe5{{msg2}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
63
64
        t = trans.ungettext('msg1', 'msg2', 1)
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
65
        self._check_exact(u'zz\xe5{{msg1}}', t)
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
66
67
68
class TestGetText(tests.TestCase):
69
70
    def setUp(self):
71
        super(TestGetText, self).setUp()
5875.3.28 by Vincent Ladeuil
Fix more test failures.
72
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
73
74
    def test_oneline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
75
        self.assertEqual(u"zz\xe5{{spam ham eggs}}",
5875.2.2 by INADA Naoki
Add tests for bzrlib.i18n
76
                         i18n.gettext("spam ham eggs"))
77
78
    def test_multiline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
79
        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.
80
                         i18n.gettext("spam\nham\n\neggs\n"))
81
82
83
class TestGetTextPerParagraph(tests.TestCase):
84
85
    def setUp(self):
86
        super(TestGetTextPerParagraph, self).setUp()
5875.3.28 by Vincent Ladeuil
Fix more test failures.
87
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
5875.2.10 by INADA Naoki
Add TestGetTextPerParagraph and fix error on TestGetText.test_multiline.
88
89
    def test_oneline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
90
        self.assertEqual(u"zz\xe5{{spam ham eggs}}",
5875.2.10 by INADA Naoki
Add TestGetTextPerParagraph and fix error on TestGetText.test_multiline.
91
                         i18n.gettext_per_paragraph("spam ham eggs"))
92
93
    def test_multiline(self):
6112.5.12 by Jonathan Riddell
use non-ascii in error i18n tests to make it more realistic
94
        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.
95
                         i18n.gettext_per_paragraph("spam\nham\n\neggs\n"))
6025.1.1 by Jelmer Vernooij
Fix i18n use when no environment variables are set.
96
97
98
class TestInstall(tests.TestCase):
99
6133.3.11 by Jonathan Riddell
make TestInstall tests more consistent
100
    def setUp(self):
101
        super(TestInstall, self).setUp()
102
        # Restore a proper env to test translation installation
103
        self.overrideAttr(i18n, '_translations', None)
104
6025.1.1 by Jelmer Vernooij
Fix i18n use when no environment variables are set.
105
    def test_custom_languages(self):
106
        i18n.install('nl:fy')
6133.3.13 by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__
107
        # Whether we found a valid tranlsation or not doesn't matter, we got
108
        # one and _translations is not None anymore.
109
        self.assertIsInstance(i18n._translations,
110
                              i18n._gettext.NullTranslations)
6025.1.1 by Jelmer Vernooij
Fix i18n use when no environment variables are set.
111
112
    def test_no_env_variables(self):
113
        self.overrideEnv('LANGUAGE', None)
114
        self.overrideEnv('LC_ALL', None)
115
        self.overrideEnv('LC_MESSAGES', None)
116
        self.overrideEnv('LANG', None)
117
        i18n.install()
6133.3.13 by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__
118
        # Whether we found a valid tranlsation or not doesn't matter, we got
119
        # one and _translations is not None anymore.
120
        self.assertIsInstance(i18n._translations,
121
                              i18n._gettext.NullTranslations)
6112.5.3 by Jonathan Riddell
start a test case
122
6131.2.4 by Jonathan Riddell
add test for i18n.disable_i18n()
123
    def test_disable_i18n(self):
124
        i18n.disable_i18n()
125
        i18n.install()
6133.3.13 by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__
126
        # It's disabled, you can't install anything and we fallback to null
127
        self.assertIsInstance(i18n._translations,
128
                              i18n._gettext.NullTranslations)
6133.3.10 by Jonathan Riddell
default _translations back to None so we can tell if it gets installed
129
6131.2.4 by Jonathan Riddell
add test for i18n.disable_i18n()
130
6112.5.3 by Jonathan Riddell
start a test case
131
class TestTranslate(tests.TestCaseWithTransport):
132
133
    def setUp(self):
134
        super(TestTranslate, self).setUp()
135
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
136
137
    def test_error_message_translation(self):
6112.5.5 by Jonathan Riddell
finish test_error_message_translation()
138
        """do errors get translated?"""
6112.5.3 by Jonathan Riddell
start a test case
139
        err = None
140
        tree = self.make_branch_and_tree('.')
141
        try:
142
            workingtree.WorkingTree.open('./foo')
143
        except errors.NotBranchError,e:
144
            err = str(e)
6118.1.7 by Jonathan Riddell
merge in trunk
145
        self.assertContainsRe(err, 
146
                              u"zz\xe5{{Not a branch: .*}}".encode("utf-8"))
6110.7.7 by Jonathan Riddell
add a test for topic help translations
147
148
    def test_topic_help_translation(self):
149
        """does topic help get translated?"""
150
        from bzrlib import help
151
        from StringIO import StringIO
152
        out = StringIO()
153
        help.help("authentication", out)
6110.7.8 by Jonathan Riddell
merge in trunk
154
        self.assertContainsRe(out.getvalue(), "zz\xe5{{Authentication Settings")