~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_utextwrap.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

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