~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright (C) 2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for bzrlib.i18n"""

from bzrlib import i18n, tests




class ZzzTranslations(object):
    """Special Zzz translation for debugging i18n stuff.

    This class can be used to confirm that the message is properly translated
    during black box tests.
    """
    _null_translation = i18n._gettext.NullTranslations()

    def zzz(self, s):
        return u'zz{{%s}}' % s

    def ugettext(self, s):
        return self.zzz(self._null_translation.ugettext(s))

    def ungettext(self, s, p, n):
        return self.zzz(self._null_translation.ungettext(s, p, n))


class TestZzzTranslation(tests.TestCase):

    def _check_exact(self, expected, source):
        self.assertEqual(expected, source)
        self.assertEqual(type(expected), type(source))

    def test_translation(self):
        trans = ZzzTranslations()

        t = trans.zzz('msg')
        self._check_exact(u'zz{{msg}}', t)

        t = trans.ugettext('msg')
        self._check_exact(u'zz{{msg}}', t)

        t = trans.ungettext('msg1', 'msg2', 0)
        self._check_exact(u'zz{{msg2}}', t)
        t = trans.ungettext('msg1', 'msg2', 2)
        self._check_exact(u'zz{{msg2}}', t)

        t = trans.ungettext('msg1', 'msg2', 1)
        self._check_exact(u'zz{{msg1}}', t)


class TestGetText(tests.TestCase):

    def setUp(self):
        super(TestGetText, self).setUp()
        self.overrideAttr(i18n, '_translations', ZzzTranslations())

    def test_oneline(self):
        self.assertEqual(u"zz{{spam ham eggs}}",
                         i18n.gettext("spam ham eggs"))

    def test_multiline(self):
        self.assertEqual(u"zz{{spam\nham\n\neggs\n}}",
                         i18n.gettext("spam\nham\n\neggs\n"))


class TestGetTextPerParagraph(tests.TestCase):

    def setUp(self):
        super(TestGetTextPerParagraph, self).setUp()
        self.overrideAttr(i18n, '_translations', ZzzTranslations())

    def test_oneline(self):
        self.assertEqual(u"zz{{spam ham eggs}}",
                         i18n.gettext_per_paragraph("spam ham eggs"))

    def test_multiline(self):
        self.assertEqual(u"zz{{spam\nham}}\n\nzz{{eggs\n}}",
                         i18n.gettext_per_paragraph("spam\nham\n\neggs\n"))