forked from git-cola/git-cola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textwrap_test.py
200 lines (158 loc) · 5.22 KB
/
textwrap_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
from __future__ import absolute_import, division, unicode_literals
import unittest
from cola import textwrap
class WordWrapTestCase(unittest.TestCase):
def setUp(self):
self.tabwidth = 8
self.limit = None
def wrap(self, text, break_on_hyphens=True):
return textwrap.word_wrap(text, self.tabwidth, self.limit,
break_on_hyphens=break_on_hyphens)
def test_word_wrap(self):
self.limit = 16
text = """
12345678901 3 56 8 01 3 5 7
1 3 5"""
expect = """
12345678901 3 56
8 01 3 5 7
1 3 5"""
self.assertEqual(expect, self.wrap(text))
def test_word_wrap_dashes(self):
self.limit = 4
text = '123-5'
expect = '123-5'
self.assertEqual(expect, self.wrap(text))
def test_word_wrap_leading_spaces(self):
self.limit = 4
expect = '1234\n5'
self.assertEqual(expect, self.wrap('1234 5'))
self.assertEqual(expect, self.wrap('1234 5'))
self.assertEqual(expect, self.wrap('1234 5'))
self.assertEqual(expect, self.wrap('1234 5'))
self.assertEqual(expect, self.wrap('1234 5'))
expect = '123\n4'
self.assertEqual(expect, self.wrap('123 4'))
self.assertEqual(expect, self.wrap('123 4'))
self.assertEqual(expect, self.wrap('123 4'))
self.assertEqual(expect, self.wrap('123 4'))
self.assertEqual(expect, self.wrap('123 4'))
def test_word_wrap_double_dashes(self):
self.limit = 4
text = '12--5'
expect = '12--\n5'
self.assertEqual(expect, self.wrap(text, break_on_hyphens=True))
expect = '12--5'
self.assertEqual(expect, self.wrap(text, break_on_hyphens=False))
def test_word_wrap_many_lines(self):
self.limit = 2
text = """
aa
bb cc dd"""
expect = """
aa
bb
cc
dd"""
self.assertEqual(expect, self.wrap(text))
def test_word_python_code(self):
self.limit = 78
text = """
if True:
print "hello world"
else:
print "hello world"
"""
self.assertEqual(text, self.wrap(text))
def test_word_wrap_spaces(self):
self.limit = 2
text = ' ' * 6
self.assertEqual('', self.wrap(text))
def test_word_wrap_special_tag(self):
self.limit = 2
text = """
This test is so meta, even this sentence
Cheered-on-by: Avoids word-wrap
C.f. This also avoids word-wrap
References: This also avoids word-wrap
See-also: This also avoids word-wrap
Related-to: This also avoids word-wrap
Link: This also avoids word-wrap
"""
expect = """
This
test
is
so
meta,
even
this
sentence
Cheered-on-by: Avoids word-wrap
C.f. This also avoids word-wrap
References: This also avoids word-wrap
See-also: This also avoids word-wrap
Related-to: This also avoids word-wrap
Link: This also avoids word-wrap
"""
self.assertEqual(self.wrap(text), expect)
def test_word_wrap_space_at_start_of_wrap(self):
inputs = """0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 """
expect = """0 1 2 3 4 5 6 7 8 9\n0 1 2 3 4 5 6 7 8"""
self.limit = 20
actual = self.wrap(inputs)
self.assertEqual(expect, actual)
def test_word_wrap_keeps_tabs_at_start(self):
inputs = """\tfirst line\n\n\tsecond line"""
expect = """\tfirst line\n\n\tsecond line"""
self.limit = 20
actual = self.wrap(inputs)
self.assertEqual(expect, actual)
def test_word_wrap_keeps_twospace_indents(self):
inputs = """first line\n\n* branch:\n line1\n line2\n"""
expect = """first line\n\n* branch:\n line1\n line2\n"""
self.limit = 20
actual = self.wrap(inputs)
self.assertEqual(expect, actual)
def test_word_wrap_ranges(self):
text = 'a bb ccc dddd\neeeee'
expect = 'a\nbb\nccc\ndddd\neeeee'
actual = textwrap.word_wrap(text, 8, 2)
self.assertEqual(expect, actual)
expect = 'a bb\nccc\ndddd\neeeee'
actual = textwrap.word_wrap(text, 8, 4)
self.assertEqual(expect, actual)
text = 'a bb ccc dddd\n\teeeee'
expect = 'a bb\nccc\ndddd\n\t\neeeee'
actual = textwrap.word_wrap(text, 8, 4)
self.assertEqual(expect, actual)
def test_triplets(self):
text = 'xx0 xx1 xx2 xx3 xx4 xx5 xx6 xx7 xx8 xx9 xxa xxb'
expect = (
'xx0 xx1 xx2 xx3 xx4 xx5 xx6\n'
'xx7 xx8 xx9 xxa xxb'
)
actual = textwrap.word_wrap(text, 8, 27)
self.assertEqual(expect, actual)
expect = (
'xx0 xx1 xx2 xx3 xx4 xx5\n'
'xx6 xx7 xx8 xx9 xxa xxb'
)
actual = textwrap.word_wrap(text, 8, 26)
self.assertEqual(expect, actual)
actual = textwrap.word_wrap(text, 8, 25)
self.assertEqual(expect, actual)
actual = textwrap.word_wrap(text, 8, 24)
self.assertEqual(expect, actual)
actual = textwrap.word_wrap(text, 8, 23)
self.assertEqual(expect, actual)
expect = (
'xx0 xx1 xx2 xx3 xx4\n'
'xx5 xx6 xx7 xx8 xx9\n'
'xxa xxb'
)
actual = textwrap.word_wrap(text, 8, 22)
self.assertEqual(expect, actual)
if __name__ == '__main__':
unittest.main()