/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 bzrlib/tests/test_export_pot.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
from cStringIO import StringIO
 
18
import textwrap
 
19
 
 
20
from bzrlib import (
 
21
    commands,
 
22
    export_pot,
 
23
    tests,
 
24
    )
 
25
 
 
26
import re
 
27
 
 
28
 
 
29
class TestEscape(tests.TestCase):
 
30
 
 
31
    def test_simple_escape(self):
 
32
        self.assertEqual(
 
33
                export_pot._escape('foobar'),
 
34
                'foobar')
 
35
 
 
36
        s = '''foo\nbar\r\tbaz\\"spam"'''
 
37
        e = '''foo\\nbar\\r\\tbaz\\\\\\"spam\\"'''
 
38
        self.assertEqual(export_pot._escape(s), e)
 
39
 
 
40
    def test_complex_escape(self):
 
41
        s = '''\\r \\\n'''
 
42
        e = '''\\\\r \\\\\\n'''
 
43
        self.assertEqual(export_pot._escape(s), e)
 
44
 
 
45
 
 
46
class TestNormalize(tests.TestCase):
 
47
 
 
48
    def test_single_line(self):
 
49
        s = 'foobar'
 
50
        e = '"foobar"'
 
51
        self.assertEqual(export_pot._normalize(s), e)
 
52
 
 
53
        s = 'foo"bar'
 
54
        e = '"foo\\"bar"'
 
55
        self.assertEqual(export_pot._normalize(s), e)
 
56
 
 
57
    def test_multi_lines(self):
 
58
        s = 'foo\nbar\n'
 
59
        e = '""\n"foo\\n"\n"bar\\n"'
 
60
        self.assertEqual(export_pot._normalize(s), e)
 
61
 
 
62
        s = '\nfoo\nbar\n'
 
63
        e = ('""\n'
 
64
             '"\\n"\n'
 
65
             '"foo\\n"\n'
 
66
             '"bar\\n"')
 
67
        self.assertEqual(export_pot._normalize(s), e)
 
68
 
 
69
 
 
70
class PoEntryTestCase(tests.TestCase):
 
71
 
 
72
    def setUp(self):
 
73
        self.overrideAttr(export_pot, '_FOUND_MSGID', set())
 
74
        self._outf = StringIO()
 
75
        super(PoEntryTestCase, self).setUp()
 
76
 
 
77
    def check_output(self, expected):
 
78
        self.assertEqual(
 
79
                self._outf.getvalue(),
 
80
                textwrap.dedent(expected)
 
81
                )
 
82
 
 
83
class TestPoEntry(PoEntryTestCase):
 
84
 
 
85
    def test_simple(self):
 
86
        export_pot._poentry(self._outf, 'dummy', 1, "spam")
 
87
        export_pot._poentry(self._outf, 'dummy', 2, "ham", 'EGG')
 
88
        self.check_output('''\
 
89
                #: dummy:1
 
90
                msgid "spam"
 
91
                msgstr ""
 
92
 
 
93
                #: dummy:2
 
94
                # EGG
 
95
                msgid "ham"
 
96
                msgstr ""
 
97
 
 
98
                ''')
 
99
 
 
100
    def test_duplicate(self):
 
101
        export_pot._poentry(self._outf, 'dummy', 1, "spam")
 
102
        # This should be ignored.
 
103
        export_pot._poentry(self._outf, 'dummy', 2, "spam", 'EGG')
 
104
 
 
105
        self.check_output('''\
 
106
                #: dummy:1
 
107
                msgid "spam"
 
108
                msgstr ""\n
 
109
                ''')
 
110
 
 
111
 
 
112
class TestPoentryPerPergraph(PoEntryTestCase):
 
113
 
 
114
    def test_single(self):
 
115
        export_pot._poentry_per_paragraph(
 
116
                self._outf,
 
117
                'dummy',
 
118
                10,
 
119
                '''foo\nbar\nbaz\n'''
 
120
                )
 
121
        self.check_output('''\
 
122
                #: dummy:10
 
123
                msgid ""
 
124
                "foo\\n"
 
125
                "bar\\n"
 
126
                "baz\\n"
 
127
                msgstr ""\n
 
128
                ''')
 
129
 
 
130
    def test_multi(self):
 
131
        export_pot._poentry_per_paragraph(
 
132
                self._outf,
 
133
                'dummy',
 
134
                10,
 
135
                '''spam\nham\negg\n\nSPAM\nHAM\nEGG\n'''
 
136
                )
 
137
        self.check_output('''\
 
138
                #: dummy:10
 
139
                msgid ""
 
140
                "spam\\n"
 
141
                "ham\\n"
 
142
                "egg"
 
143
                msgstr ""
 
144
 
 
145
                #: dummy:14
 
146
                msgid ""
 
147
                "SPAM\\n"
 
148
                "HAM\\n"
 
149
                "EGG\\n"
 
150
                msgstr ""\n
 
151
                ''')
 
152
 
 
153
 
 
154
class TestExportCommandHelp(PoEntryTestCase):
 
155
 
 
156
    def test_command_help(self):
 
157
 
 
158
        class cmd_Demo(commands.Command):
 
159
            __doc__ = """A sample command.
 
160
 
 
161
            :Usage:
 
162
                bzr demo
 
163
 
 
164
            :Examples:
 
165
                Example 1::
 
166
 
 
167
                    cmd arg1
 
168
 
 
169
            Blah Blah Blah
 
170
            """
 
171
 
 
172
        export_pot._write_command_help(self._outf, cmd_Demo())
 
173
        result = self._outf.getvalue()
 
174
        # We don't care about filename and lineno here.
 
175
        result = re.sub(r'(?m)^#: [^\n]+\n', '', result)
 
176
 
 
177
        self.assertEqualDiff(
 
178
                'msgid "A sample command."\n'
 
179
                'msgstr ""\n'
 
180
                '\n'                # :Usage: should not be translated.
 
181
                'msgid ""\n'
 
182
                '":Examples:\\n"\n'
 
183
                '"    Example 1::"\n'
 
184
                'msgstr ""\n'
 
185
                '\n'
 
186
                'msgid "        cmd arg1"\n'
 
187
                'msgstr ""\n'
 
188
                '\n'
 
189
                'msgid "Blah Blah Blah"\n'
 
190
                'msgstr ""\n'
 
191
                '\n',
 
192
                result
 
193
                )