/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_i18n.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011, 2016 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 breezy.i18n"""
 
18
 
 
19
import io
 
20
 
 
21
from .. import (
 
22
    i18n,
 
23
    tests,
 
24
    errors,
 
25
    workingtree,
 
26
    )
 
27
from ..sixish import PY3
 
28
 
 
29
 
 
30
class ZzzTranslations(object):
 
31
    """Special Zzz translation for debugging i18n stuff.
 
32
 
 
33
    This class can be used to confirm that the message is properly translated
 
34
    during black box tests.
 
35
    """
 
36
    _null_translation = i18n._gettext.NullTranslations()
 
37
 
 
38
    def zzz(self, s):
 
39
        return u'zz\xe5{{%s}}' % s
 
40
 
 
41
    def gettext(self, s):
 
42
        return self.zzz(self._null_translation.gettext(s))
 
43
 
 
44
    def ngettext(self, s, p, n):
 
45
        return self.zzz(self._null_translation.ngettext(s, p, n))
 
46
 
 
47
    def ugettext(self, s):
 
48
        return self.zzz(self._null_translation.ugettext(s))
 
49
 
 
50
    def ungettext(self, s, p, n):
 
51
        return self.zzz(self._null_translation.ungettext(s, p, n))
 
52
 
 
53
 
 
54
class TestZzzTranslation(tests.TestCase):
 
55
 
 
56
    def _check_exact(self, expected, source):
 
57
        self.assertEqual(expected, source)
 
58
        self.assertEqual(type(expected), type(source))
 
59
 
 
60
    def test_translation(self):
 
61
        trans = ZzzTranslations()
 
62
 
 
63
        t = trans.zzz('msg')
 
64
        self._check_exact(u'zz\xe5{{msg}}', t)
 
65
 
 
66
        if PY3:
 
67
            t = trans.gettext('msg')
 
68
            self._check_exact(u'zz\xe5{{msg}}', t)
 
69
 
 
70
            t = trans.ngettext('msg1', 'msg2', 0)
 
71
            self._check_exact(u'zz\xe5{{msg2}}', t)
 
72
            t = trans.ngettext('msg1', 'msg2', 2)
 
73
            self._check_exact(u'zz\xe5{{msg2}}', t)
 
74
 
 
75
            t = trans.ngettext('msg1', 'msg2', 1)
 
76
            self._check_exact(u'zz\xe5{{msg1}}', t)
 
77
        else:
 
78
            t = trans.ugettext('msg')
 
79
            self._check_exact(u'zz\xe5{{msg}}', t)
 
80
 
 
81
            t = trans.ungettext('msg1', 'msg2', 0)
 
82
            self._check_exact(u'zz\xe5{{msg2}}', t)
 
83
            t = trans.ungettext('msg1', 'msg2', 2)
 
84
            self._check_exact(u'zz\xe5{{msg2}}', t)
 
85
 
 
86
            t = trans.ungettext('msg1', 'msg2', 1)
 
87
            self._check_exact(u'zz\xe5{{msg1}}', t)
 
88
 
 
89
 
 
90
class TestGetText(tests.TestCase):
 
91
 
 
92
    def setUp(self):
 
93
        super(TestGetText, self).setUp()
 
94
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
 
95
 
 
96
    def test_oneline(self):
 
97
        self.assertEqual(u"zz\xe5{{spam ham eggs}}",
 
98
                         i18n.gettext("spam ham eggs"))
 
99
 
 
100
    def test_multiline(self):
 
101
        self.assertEqual(u"zz\xe5{{spam\nham\n\neggs\n}}",
 
102
                         i18n.gettext("spam\nham\n\neggs\n"))
 
103
 
 
104
 
 
105
class TestGetTextPerParagraph(tests.TestCase):
 
106
 
 
107
    def setUp(self):
 
108
        super(TestGetTextPerParagraph, self).setUp()
 
109
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
 
110
 
 
111
    def test_oneline(self):
 
112
        self.assertEqual(u"zz\xe5{{spam ham eggs}}",
 
113
                         i18n.gettext_per_paragraph("spam ham eggs"))
 
114
 
 
115
    def test_multiline(self):
 
116
        self.assertEqual(u"zz\xe5{{spam\nham}}\n\nzz\xe5{{eggs\n}}",
 
117
                         i18n.gettext_per_paragraph("spam\nham\n\neggs\n"))
 
118
 
 
119
 
 
120
class TestInstall(tests.TestCase):
 
121
 
 
122
    def setUp(self):
 
123
        super(TestInstall, self).setUp()
 
124
        # Restore a proper env to test translation installation
 
125
        self.overrideAttr(i18n, '_translations', None)
 
126
 
 
127
    def test_custom_languages(self):
 
128
        i18n.install('nl:fy')
 
129
        # Whether we found a valid tranlsation or not doesn't matter, we got
 
130
        # one and _translations is not None anymore.
 
131
        self.assertIsInstance(i18n._translations,
 
132
                              i18n._gettext.NullTranslations)
 
133
 
 
134
    def test_no_env_variables(self):
 
135
        self.overrideEnv('LANGUAGE', None)
 
136
        self.overrideEnv('LC_ALL', None)
 
137
        self.overrideEnv('LC_MESSAGES', None)
 
138
        self.overrideEnv('LANG', None)
 
139
        i18n.install()
 
140
        # Whether we found a valid tranlsation or not doesn't matter, we got
 
141
        # one and _translations is not None anymore.
 
142
        self.assertIsInstance(i18n._translations,
 
143
                              i18n._gettext.NullTranslations)
 
144
 
 
145
    def test_disable_i18n(self):
 
146
        i18n.disable_i18n()
 
147
        i18n.install()
 
148
        # It's disabled, you can't install anything and we fallback to null
 
149
        self.assertIsInstance(i18n._translations,
 
150
                              i18n._gettext.NullTranslations)
 
151
 
 
152
 
 
153
class TestTranslate(tests.TestCaseWithTransport):
 
154
 
 
155
    def setUp(self):
 
156
        super(TestTranslate, self).setUp()
 
157
        self.overrideAttr(i18n, '_translations', ZzzTranslations())
 
158
 
 
159
    def test_error_message_translation(self):
 
160
        """do errors get translated?"""
 
161
        err = None
 
162
        tree = self.make_branch_and_tree('.')
 
163
        try:
 
164
            workingtree.WorkingTree.open('./foo')
 
165
        except errors.NotBranchError as e:
 
166
            err = str(e)
 
167
        if PY3:
 
168
            self.assertContainsRe(err, u"zz\xe5{{Not a branch: .*}}")
 
169
        else:
 
170
            self.assertContainsRe(
 
171
                err, u"zz\xe5{{Not a branch: .*}}".encode('utf-8'))
 
172
 
 
173
    def test_topic_help_translation(self):
 
174
        """does topic help get translated?"""
 
175
        from .. import help
 
176
        out = io.StringIO()
 
177
        help.help("authentication", out)
 
178
        self.assertContainsRe(
 
179
            out.getvalue(), "zz\xe5{{Authentication Settings")
 
180
 
 
181
 
 
182
class LoadPluginTranslations(tests.TestCase):
 
183
 
 
184
    def test_does_not_exist(self):
 
185
        translation = i18n.load_plugin_translations("doesnotexist")
 
186
        self.assertEqual("foo", translation.gettext("foo"))