/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: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

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