/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5820.1.3 by INADA Naoki
Move tests for utextwrap from the module to bzrlib.tests.
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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
18
"""Tests of the breezy.utextwrap."""
5820.1.3 by INADA Naoki
Move tests for utextwrap from the module to bzrlib.tests.
19
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
20
from .. import (
5920.1.1 by Vincent Ladeuil
Skip tests broken by sphinx monkeypatching
21
    tests,
22
    utextwrap,
23
    )
24
5820.1.3 by INADA Naoki
Move tests for utextwrap from the module to bzrlib.tests.
25
26
# Japanese "Good morning".
27
# Each character have double width. So total 8 width on console.
28
_str_D = u'\u304a\u306f\u3088\u3046'
29
30
_str_S = u"hello"
31
32
# Combine single width characters and double width characters.
33
_str_SD = _str_S + _str_D
34
_str_DS = _str_D + _str_S
35
7143.15.2 by Jelmer Vernooij
Run autopep8.
36
5820.1.3 by INADA Naoki
Move tests for utextwrap from the module to bzrlib.tests.
37
class TestUTextWrap(tests.TestCase):
38
5820.1.10 by INADA Naoki
utextwrap: Change a way to split between CJK characters.
39
    def check_width(self, text, expected_width):
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
40
        w = utextwrap.UTextWrapper()
5820.1.3 by INADA Naoki
Move tests for utextwrap from the module to bzrlib.tests.
41
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
42
            w._width(text),
43
            expected_width,
44
            "Width of %r should be %d" % (text, expected_width))
5820.1.3 by INADA Naoki
Move tests for utextwrap from the module to bzrlib.tests.
45
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
46
    def test_width(self):
5820.1.10 by INADA Naoki
utextwrap: Change a way to split between CJK characters.
47
        self.check_width(_str_D, 8)
48
        self.check_width(_str_SD, 13)
49
50
    def check_cut(self, text, width, pos):
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
51
        w = utextwrap.UTextWrapper()
52
        self.assertEqual((text[:pos], text[pos:]), w._cut(text, width))
5820.1.10 by INADA Naoki
utextwrap: Change a way to split between CJK characters.
53
54
    def test_cut(self):
55
        s = _str_SD
56
        self.check_cut(s, 0, 0)
57
        self.check_cut(s, 1, 1)
58
        self.check_cut(s, 5, 5)
59
        self.check_cut(s, 6, 5)
60
        self.check_cut(s, 7, 6)
61
        self.check_cut(s, 12, 8)
62
        self.check_cut(s, 13, 9)
63
        self.check_cut(s, 14, 9)
7143.15.2 by Jelmer Vernooij
Run autopep8.
64
        self.check_cut(u'A' * 5, 3, 3)
5820.1.10 by INADA Naoki
utextwrap: Change a way to split between CJK characters.
65
66
    def test_split(self):
67
        w = utextwrap.UTextWrapper()
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
68
        self.assertEqual(list(_str_D), w._split(_str_D))
7143.15.2 by Jelmer Vernooij
Run autopep8.
69
        self.assertEqual([_str_S] + list(_str_D), w._split(_str_SD))
70
        self.assertEqual(list(_str_D) + [_str_S], w._split(_str_DS))
5820.1.5 by INADA Naoki
Make UTextWrapper support byte string and add tests including Python's
71
5820.1.3 by INADA Naoki
Move tests for utextwrap from the module to bzrlib.tests.
72
    def test_wrap(self):
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
73
        self.assertEqual(list(_str_D), utextwrap.wrap(_str_D, 1))
74
        self.assertEqual(list(_str_D), utextwrap.wrap(_str_D, 2))
75
        self.assertEqual(list(_str_D), utextwrap.wrap(_str_D, 3))
76
        self.assertEqual(list(_str_D),
77
                         utextwrap.wrap(_str_D, 3, break_long_words=False))
5820.1.5 by INADA Naoki
Make UTextWrapper support byte string and add tests including Python's
78
7143.15.2 by Jelmer Vernooij
Run autopep8.
79
5820.1.13 by Vincent Ladeuil
Use a dedicated class for 'fill' tests.
80
class TestUTextFill(tests.TestCase):
81
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
82
    def test_fill_simple(self):
5820.1.7 by INADA Naoki
Add test for fill.
83
        # Test only can call fill() because it's just '\n'.join(wrap(text)).
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
84
        self.assertEqual("%s\n%s" % (_str_D[:2], _str_D[2:]),
85
                         utextwrap.fill(_str_D, 4))
5820.1.5 by INADA Naoki
Make UTextWrapper support byte string and add tests including Python's
86
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
87
    def test_fill_with_breaks(self):
5820.1.8 by INADA Naoki
Add two tests for fill demonstrates complicated cases. The second test fails
88
        # Demonstrate complicated case.
7143.15.2 by Jelmer Vernooij
Run autopep8.
89
        text = u"spam ham egg spamhamegg" + _str_D + u" spam" + _str_D * 2
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
90
        self.assertEqual(u'\n'.join(["spam ham",
91
                                     "egg spam",
92
                                     "hamegg" + _str_D[0],
93
                                     _str_D[1:],
94
                                     "spam" + _str_D[:2],
7143.15.2 by Jelmer Vernooij
Run autopep8.
95
                                     _str_D[2:] + _str_D[:2],
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
96
                                     _str_D[2:]]),
97
                         utextwrap.fill(text, 8))
98
99
    def test_fill_without_breaks(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
100
        text = u"spam ham egg spamhamegg" + _str_D + u" spam" + _str_D * 2
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
101
        self.assertEqual(u'\n'.join(["spam ham",
102
                                     "egg",
7143.15.2 by Jelmer Vernooij
Run autopep8.
103
                                     "spamhamegg",
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
104
                                     # border between single width and double
105
                                     # width.
106
                                     _str_D,
107
                                     "spam" + _str_D[:2],
7143.15.2 by Jelmer Vernooij
Run autopep8.
108
                                     _str_D[2:] + _str_D[:2],
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
109
                                     _str_D[2:]]),
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
110
                         utextwrap.fill(text, 8, break_long_words=False))
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
111
112
    def test_fill_indent_with_breaks(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
113
        w = utextwrap.UTextWrapper(8, initial_indent=' ' * 4,
114
                                   subsequent_indent=' ' * 4)
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
115
        self.assertEqual(u'\n'.join(["    hell",
116
                                     "    o" + _str_D[0],
117
                                     "    " + _str_D[1:3],
118
                                     "    " + _str_D[3]
119
                                     ]),
120
                         w.fill(_str_SD))
121
122
    def test_fill_indent_without_breaks(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
123
        w = utextwrap.UTextWrapper(8, initial_indent=' ' * 4,
124
                                   subsequent_indent=' ' * 4)
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
125
        w.break_long_words = False
126
        self.assertEqual(u'\n'.join(["    hello",
127
                                     "    " + _str_D[:2],
128
                                     "    " + _str_D[2:],
129
                                     ]),
130
                         w.fill(_str_SD))
131
132
    def test_fill_indent_without_breaks_with_fixed_width(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
133
        w = utextwrap.UTextWrapper(8, initial_indent=' ' * 4,
134
                                   subsequent_indent=' ' * 4)
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
135
        w.break_long_words = False
5820.1.11 by INADA Naoki
Add some test cases.
136
        w.width = 3
5820.1.12 by Vincent Ladeuil
Use assert(expected, actual) style and split some tests.
137
        self.assertEqual(u'\n'.join(["    hello",
138
                                     "    " + _str_D[0],
139
                                     "    " + _str_D[1],
140
                                     "    " + _str_D[2],
141
                                     "    " + _str_D[3],
142
                                     ]),
143
                         w.fill(_str_SD))
5820.1.11 by INADA Naoki
Add some test cases.
144
7143.15.2 by Jelmer Vernooij
Run autopep8.
145
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
146
class TestUTextWrapAmbiWidth(tests.TestCase):
7143.15.2 by Jelmer Vernooij
Run autopep8.
147
    _cyrill_char = u"\u0410"  # east_asian_width() == 'A'
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
148
149
    def test_ambiwidth1(self):
150
        w = utextwrap.UTextWrapper(4, ambiguous_width=1)
7143.15.2 by Jelmer Vernooij
Run autopep8.
151
        s = self._cyrill_char * 8
152
        self.assertEqual([self._cyrill_char * 4] * 2, w.wrap(s))
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
153
154
    def test_ambiwidth2(self):
155
        w = utextwrap.UTextWrapper(4, ambiguous_width=2)
7143.15.2 by Jelmer Vernooij
Run autopep8.
156
        s = self._cyrill_char * 8
157
        self.assertEqual([self._cyrill_char * 2] * 4, w.wrap(s))
5820.1.19 by INADA Naoki
Add keyword parameter 'ambiguous_width' that specifies width for character
158
5820.1.8 by INADA Naoki
Add two tests for fill demonstrates complicated cases. The second test fails
159
5820.1.5 by INADA Naoki
Make UTextWrapper support byte string and add tests including Python's
160
# Regression test with Python's test_textwrap
161
# Note that some distribution including Ubuntu doesn't install
162
# Python's test suite.
163
try:
5820.1.14 by Vincent Ladeuil
Properly override test symbols in the imported test module so they are restored after the tests are run.
164
    from test import test_textwrap
165
166
    def override_textwrap_symbols(testcase):
167
        # Override the symbols imported by test_textwrap so it uses our own
168
        # replacements.
169
        testcase.overrideAttr(test_textwrap, 'TextWrapper',
170
                              utextwrap.UTextWrapper)
171
        testcase.overrideAttr(test_textwrap, 'wrap', utextwrap.wrap)
172
        testcase.overrideAttr(test_textwrap, 'fill', utextwrap.fill)
173
174
    def setup_both(testcase, base_class, reused_class):
175
        super(base_class, testcase).setUp()
176
        override_textwrap_symbols(testcase)
177
        reused_class.setUp(testcase)
178
179
    class TestWrap(tests.TestCase, test_textwrap.WrapTestCase):
180
181
        def setUp(self):
182
            setup_both(self, TestWrap, test_textwrap.WrapTestCase)
183
184
    class TestLongWord(tests.TestCase, test_textwrap.LongWordTestCase):
185
186
        def setUp(self):
187
            setup_both(self, TestLongWord, test_textwrap.LongWordTestCase)
188
189
    class TestIndent(tests.TestCase, test_textwrap.IndentTestCases):
190
191
        def setUp(self):
192
            setup_both(self, TestIndent, test_textwrap.IndentTestCases)
193
5820.1.5 by INADA Naoki
Make UTextWrapper support byte string and add tests including Python's
194
except ImportError:
5820.1.28 by INADA Naoki
Notify when test_textwrap is skipped.
195
196
    class TestWrap(tests.TestCase):
197
198
        def test_wrap(self):
5920.1.1 by Vincent Ladeuil
Skip tests broken by sphinx monkeypatching
199
            raise tests.TestSkipped("test.test_textwrap is not available.")
5820.1.28 by INADA Naoki
Notify when test_textwrap is skipped.
200
201
    class TestLongWord(tests.TestCase):
202
203
        def test_longword(self):
5920.1.1 by Vincent Ladeuil
Skip tests broken by sphinx monkeypatching
204
            raise tests.TestSkipped("test.test_textwrap is not available.")
5820.1.28 by INADA Naoki
Notify when test_textwrap is skipped.
205
206
    class TestIndent(tests.TestCase):
207
208
        def test_indent(self):
5920.1.1 by Vincent Ladeuil
Skip tests broken by sphinx monkeypatching
209
            raise tests.TestSkipped("test.test_textwrap is not available.")