~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_utextwrap.py

  • Committer: INADA Naoki
  • Date: 2011-05-17 00:45:09 UTC
  • mfrom: (5875 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5891.
  • Revision ID: songofacandy@gmail.com-20110517004509-q58negjbdjh7t6u1
mergeĀ fromĀ lp:bzr

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
 
 
18
"""Tests of the bzrlib.utextwrap."""
 
19
 
 
20
from bzrlib import tests, utextwrap
 
21
from bzrlib.tests import TestSkipped
 
22
 
 
23
# Japanese "Good morning".
 
24
# Each character have double width. So total 8 width on console.
 
25
_str_D = u'\u304a\u306f\u3088\u3046'
 
26
 
 
27
_str_S = u"hello"
 
28
 
 
29
# Combine single width characters and double width characters.
 
30
_str_SD = _str_S + _str_D
 
31
_str_DS = _str_D + _str_S
 
32
 
 
33
class TestUTextWrap(tests.TestCase):
 
34
 
 
35
    def check_width(self, text, expected_width):
 
36
        w = utextwrap.UTextWrapper()
 
37
        self.assertEqual(
 
38
                w._width(text),
 
39
                expected_width,
 
40
                "Width of %r should be %d" % (text, expected_width))
 
41
 
 
42
    def test_width(self):
 
43
        self.check_width(_str_D, 8)
 
44
        self.check_width(_str_SD, 13)
 
45
 
 
46
    def check_cut(self, text, width, pos):
 
47
        w = utextwrap.UTextWrapper()
 
48
        self.assertEqual((text[:pos], text[pos:]), w._cut(text, width))
 
49
 
 
50
    def test_cut(self):
 
51
        s = _str_SD
 
52
        self.check_cut(s, 0, 0)
 
53
        self.check_cut(s, 1, 1)
 
54
        self.check_cut(s, 5, 5)
 
55
        self.check_cut(s, 6, 5)
 
56
        self.check_cut(s, 7, 6)
 
57
        self.check_cut(s, 12, 8)
 
58
        self.check_cut(s, 13, 9)
 
59
        self.check_cut(s, 14, 9)
 
60
        self.check_cut(u'A'*5, 3, 3)
 
61
 
 
62
    def test_split(self):
 
63
        w = utextwrap.UTextWrapper()
 
64
        self.assertEqual(list(_str_D), w._split(_str_D))
 
65
        self.assertEqual([_str_S]+list(_str_D), w._split(_str_SD))
 
66
        self.assertEqual(list(_str_D)+[_str_S], w._split(_str_DS))
 
67
 
 
68
    def test_wrap(self):
 
69
        self.assertEqual(list(_str_D), utextwrap.wrap(_str_D, 1))
 
70
        self.assertEqual(list(_str_D), utextwrap.wrap(_str_D, 2))
 
71
        self.assertEqual(list(_str_D), utextwrap.wrap(_str_D, 3))
 
72
        self.assertEqual(list(_str_D),
 
73
                         utextwrap.wrap(_str_D, 3, break_long_words=False))
 
74
 
 
75
class TestUTextFill(tests.TestCase):
 
76
 
 
77
    def test_fill_simple(self):
 
78
        # Test only can call fill() because it's just '\n'.join(wrap(text)).
 
79
        self.assertEqual("%s\n%s" % (_str_D[:2], _str_D[2:]),
 
80
                         utextwrap.fill(_str_D, 4))
 
81
 
 
82
    def test_fill_with_breaks(self):
 
83
        # Demonstrate complicated case.
 
84
        text = u"spam ham egg spamhamegg" + _str_D + u" spam" + _str_D*2
 
85
        self.assertEqual(u'\n'.join(["spam ham",
 
86
                                     "egg spam",
 
87
                                     "hamegg" + _str_D[0],
 
88
                                     _str_D[1:],
 
89
                                     "spam" + _str_D[:2],
 
90
                                     _str_D[2:]+_str_D[:2],
 
91
                                     _str_D[2:]]),
 
92
                         utextwrap.fill(text, 8))
 
93
 
 
94
    def test_fill_without_breaks(self):
 
95
        text = u"spam ham egg spamhamegg" + _str_D + u" spam" + _str_D*2
 
96
        self.assertEqual(u'\n'.join(["spam ham",
 
97
                                     "egg",
 
98
                                     "spamhamegg", 
 
99
                                     # border between single width and double
 
100
                                     # width.
 
101
                                     _str_D,
 
102
                                     "spam" + _str_D[:2],
 
103
                                     _str_D[2:]+_str_D[:2],
 
104
                                     _str_D[2:]]),
 
105
                         utextwrap.fill(text, 8, break_long_words=False))
 
106
 
 
107
    def test_fill_indent_with_breaks(self):
 
108
        w = utextwrap.UTextWrapper(8, initial_indent=' '*4,
 
109
                                   subsequent_indent=' '*4)
 
110
        self.assertEqual(u'\n'.join(["    hell",
 
111
                                     "    o" + _str_D[0],
 
112
                                     "    " + _str_D[1:3],
 
113
                                     "    " + _str_D[3]
 
114
                                     ]),
 
115
                         w.fill(_str_SD))
 
116
 
 
117
    def test_fill_indent_without_breaks(self):
 
118
        w = utextwrap.UTextWrapper(8, initial_indent=' '*4,
 
119
                                   subsequent_indent=' '*4)
 
120
        w.break_long_words = False
 
121
        self.assertEqual(u'\n'.join(["    hello",
 
122
                                     "    " + _str_D[:2],
 
123
                                     "    " + _str_D[2:],
 
124
                                     ]),
 
125
                         w.fill(_str_SD))
 
126
 
 
127
    def test_fill_indent_without_breaks_with_fixed_width(self):
 
128
        w = utextwrap.UTextWrapper(8, initial_indent=' '*4,
 
129
                                   subsequent_indent=' '*4)
 
130
        w.break_long_words = False
 
131
        w.width = 3
 
132
        self.assertEqual(u'\n'.join(["    hello",
 
133
                                     "    " + _str_D[0],
 
134
                                     "    " + _str_D[1],
 
135
                                     "    " + _str_D[2],
 
136
                                     "    " + _str_D[3],
 
137
                                     ]),
 
138
                         w.fill(_str_SD))
 
139
 
 
140
class TestUTextWrapAmbiWidth(tests.TestCase):
 
141
    _cyrill_char = u"\u0410" # east_asian_width() == 'A'
 
142
 
 
143
    def test_ambiwidth1(self):
 
144
        w = utextwrap.UTextWrapper(4, ambiguous_width=1)
 
145
        s = self._cyrill_char*8
 
146
        self.assertEqual([self._cyrill_char*4]*2, w.wrap(s))
 
147
 
 
148
    def test_ambiwidth2(self):
 
149
        w = utextwrap.UTextWrapper(4, ambiguous_width=2)
 
150
        s = self._cyrill_char*8
 
151
        self.assertEqual([self._cyrill_char*2]*4, w.wrap(s))
 
152
 
 
153
 
 
154
# Regression test with Python's test_textwrap
 
155
# Note that some distribution including Ubuntu doesn't install
 
156
# Python's test suite.
 
157
try:
 
158
    from test import test_textwrap
 
159
 
 
160
    def override_textwrap_symbols(testcase):
 
161
        # Override the symbols imported by test_textwrap so it uses our own
 
162
        # replacements.
 
163
        testcase.overrideAttr(test_textwrap, 'TextWrapper',
 
164
                              utextwrap.UTextWrapper)
 
165
        testcase.overrideAttr(test_textwrap, 'wrap', utextwrap.wrap)
 
166
        testcase.overrideAttr(test_textwrap, 'fill', utextwrap.fill)
 
167
 
 
168
 
 
169
    def setup_both(testcase, base_class, reused_class):
 
170
        super(base_class, testcase).setUp()
 
171
        override_textwrap_symbols(testcase)
 
172
        reused_class.setUp(testcase)
 
173
 
 
174
 
 
175
    class TestWrap(tests.TestCase, test_textwrap.WrapTestCase):
 
176
 
 
177
        def setUp(self):
 
178
            setup_both(self, TestWrap, test_textwrap.WrapTestCase)
 
179
 
 
180
 
 
181
    class TestLongWord(tests.TestCase, test_textwrap.LongWordTestCase):
 
182
 
 
183
        def setUp(self):
 
184
            setup_both(self, TestLongWord, test_textwrap.LongWordTestCase)
 
185
 
 
186
 
 
187
    class TestIndent(tests.TestCase, test_textwrap.IndentTestCases):
 
188
 
 
189
        def setUp(self):
 
190
            setup_both(self, TestIndent, test_textwrap.IndentTestCases)
 
191
 
 
192
except ImportError:
 
193
 
 
194
    class TestWrap(tests.TestCase):
 
195
 
 
196
        def test_wrap(self):
 
197
            raise TestSkipped("test.test_textwrap is not avialable.")
 
198
 
 
199
    class TestLongWord(tests.TestCase):
 
200
 
 
201
        def test_longword(self):
 
202
            raise TestSkipped("test.test_textwrap is not avialable.")
 
203
 
 
204
    class TestIndent(tests.TestCase):
 
205
 
 
206
        def test_indent(self):
 
207
            raise TestSkipped("test.test_textwrap is not avialable.")