/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
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
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
18
import textwrap
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
19
20
from bzrlib import (
5875.3.20 by INADA Naoki
Add test for exporting command help.
21
    commands,
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
22
    export_pot,
23
    tests,
24
    )
25
5875.3.20 by INADA Naoki
Add test for exporting command help.
26
import re
27
28
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
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
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
70
class PoEntryTestCase(tests.TestCase):
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
71
72
    def setUp(self):
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
73
        super(PoEntryTestCase, self).setUp()
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
74
        self.exporter = export_pot._PotExporter(StringIO())
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
75
76
    def check_output(self, expected):
77
        self.assertEqual(
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
78
                self.exporter.outf.getvalue(),
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
79
                textwrap.dedent(expected)
80
                )
81
82
class TestPoEntry(PoEntryTestCase):
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
83
84
    def test_simple(self):
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
85
        self.exporter.poentry('dummy', 1, "spam")
86
        self.exporter.poentry('dummy', 2, "ham", 'EGG')
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
87
        self.check_output('''\
88
                #: dummy:1
89
                msgid "spam"
90
                msgstr ""
91
92
                #: dummy:2
93
                # EGG
94
                msgid "ham"
95
                msgstr ""
96
97
                ''')
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
98
99
    def test_duplicate(self):
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
100
        self.exporter.poentry('dummy', 1, "spam")
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
101
        # This should be ignored.
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
102
        self.exporter.poentry('dummy', 2, "spam", 'EGG')
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
103
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
104
        self.check_output('''\
105
                #: dummy:1
106
                msgid "spam"
107
                msgstr ""\n
108
                ''')
109
110
111
class TestPoentryPerPergraph(PoEntryTestCase):
112
113
    def test_single(self):
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
114
        self.exporter.poentry_per_paragraph(
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
115
                'dummy',
116
                10,
117
                '''foo\nbar\nbaz\n'''
118
                )
119
        self.check_output('''\
120
                #: dummy:10
121
                msgid ""
122
                "foo\\n"
123
                "bar\\n"
124
                "baz\\n"
125
                msgstr ""\n
126
                ''')
127
128
    def test_multi(self):
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
129
        self.exporter.poentry_per_paragraph(
5830.2.20 by INADA Naoki
Add test for _poentry_per_peragraph()
130
                'dummy',
131
                10,
132
                '''spam\nham\negg\n\nSPAM\nHAM\nEGG\n'''
133
                )
134
        self.check_output('''\
135
                #: dummy:10
136
                msgid ""
137
                "spam\\n"
138
                "ham\\n"
139
                "egg"
140
                msgstr ""
141
142
                #: dummy:14
143
                msgid ""
144
                "SPAM\\n"
145
                "HAM\\n"
146
                "EGG\\n"
147
                msgstr ""\n
148
                ''')
5875.3.20 by INADA Naoki
Add test for exporting command help.
149
150
151
class TestExportCommandHelp(PoEntryTestCase):
152
153
    def test_command_help(self):
154
155
        class cmd_Demo(commands.Command):
156
            __doc__ = """A sample command.
157
158
            :Usage:
159
                bzr demo
160
161
            :Examples:
162
                Example 1::
163
164
                    cmd arg1
165
166
            Blah Blah Blah
167
            """
168
6282.2.1 by Martin Packman
Add export_pot._PotExporter class to avoid module global, and other minor cleanups
169
        export_pot._write_command_help(self.exporter, cmd_Demo())
170
        result = self.exporter.outf.getvalue()
5993.1.2 by Vincent Ladeuil
Fix python2.6 compatibility.
171
        # We don't care about filename and lineno here.
172
        result = re.sub(r'(?m)^#: [^\n]+\n', '', result)
5875.3.20 by INADA Naoki
Add test for exporting command help.
173
174
        self.assertEqualDiff(
175
                'msgid "A sample command."\n'
176
                'msgstr ""\n'
177
                '\n'                # :Usage: should not be translated.
178
                'msgid ""\n'
179
                '":Examples:\\n"\n'
180
                '"    Example 1::"\n'
181
                'msgstr ""\n'
182
                '\n'
183
                'msgid "        cmd arg1"\n'
184
                'msgstr ""\n'
185
                '\n'
186
                'msgid "Blah Blah Blah"\n'
187
                'msgstr ""\n'
188
                '\n',
189
                result
190
                )