1 """Test the textwrap module"""
2 # pylint: disable=redefined-outer-name
6 from cola
import textwrap
9 class WordWrapDefaults
:
14 def wrap(self
, text
, break_on_hyphens
=True):
15 return textwrap
.word_wrap(
16 text
, self
.tabwidth
, self
.limit
, break_on_hyphens
=break_on_hyphens
22 """Provide default word wrap options for tests"""
23 return WordWrapDefaults()
26 def test_word_wrap(wordwrap
):
29 12345678901 3 56 8 01 3 5 7
37 assert expect
== wordwrap
.wrap(text
)
40 def test_word_wrap_dashes(wordwrap
):
44 assert expect
== wordwrap
.wrap(text
)
47 def test_word_wrap_leading_spaces(wordwrap
):
51 assert expect
== wordwrap
.wrap('1234 5')
52 assert expect
== wordwrap
.wrap('1234 5')
53 assert expect
== wordwrap
.wrap('1234 5')
54 assert expect
== wordwrap
.wrap('1234 5')
55 assert expect
== wordwrap
.wrap('1234 5')
58 assert expect
== wordwrap
.wrap('123 4')
59 assert expect
== wordwrap
.wrap('123 4')
60 assert expect
== wordwrap
.wrap('123 4')
61 assert expect
== wordwrap
.wrap('123 4')
62 assert expect
== wordwrap
.wrap('123 4')
65 def test_word_wrap_double_dashes(wordwrap
):
69 actual
= wordwrap
.wrap(text
, break_on_hyphens
=True)
70 assert expect
== actual
73 actual
= wordwrap
.wrap(text
, break_on_hyphens
=False)
74 assert expect
== actual
77 def test_word_wrap_many_lines(wordwrap
):
92 actual
= wordwrap
.wrap(text
)
93 assert expect
== actual
96 def test_word_python_code(wordwrap
):
106 actual
= wordwrap
.wrap(text
)
107 assert expect
== actual
110 def test_word_wrap_spaces(wordwrap
):
114 actual
= wordwrap
.wrap(text
)
115 assert expect
== actual
118 def test_word_wrap_special_tag(wordwrap
):
121 This test is so meta, even this sentence
123 Cheered-on-by: Avoids word-wrap
124 C.f. This also avoids word-wrap
125 References: This also avoids word-wrap
126 See-also: This also avoids word-wrap
127 Related-to: This also avoids word-wrap
128 Link: This also avoids word-wrap
141 Cheered-on-by: Avoids word-wrap
142 C.f. This also avoids word-wrap
143 References: This also avoids word-wrap
144 See-also: This also avoids word-wrap
145 Related-to: This also avoids word-wrap
146 Link: This also avoids word-wrap
148 actual
= wordwrap
.wrap(text
)
149 assert expect
== actual
152 def test_word_wrap_space_at_start_of_wrap(wordwrap
):
153 inputs
= """0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 """
154 expect
= """0 1 2 3 4 5 6 7 8 9\n0 1 2 3 4 5 6 7 8"""
157 actual
= wordwrap
.wrap(inputs
)
158 assert expect
== actual
161 def test_word_wrap_keeps_tabs_at_start(wordwrap
):
162 inputs
= """\tfirst line\n\n\tsecond line"""
163 expect
= """\tfirst line\n\n\tsecond line"""
166 actual
= wordwrap
.wrap(inputs
)
167 assert expect
== actual
170 def test_word_wrap_keeps_twospace_indents(wordwrap
):
171 inputs
= """first line\n\n* branch:\n line1\n line2\n"""
172 expect
= """first line\n\n* branch:\n line1\n line2\n"""
175 actual
= wordwrap
.wrap(inputs
)
176 assert expect
== actual
179 def test_word_wrap_ranges():
180 text
= 'a bb ccc dddd\neeeee'
181 expect
= 'a\nbb\nccc\ndddd\neeeee'
182 actual
= textwrap
.word_wrap(text
, 8, 2)
183 assert expect
== actual
185 expect
= 'a bb\nccc\ndddd\neeeee'
186 actual
= textwrap
.word_wrap(text
, 8, 4)
187 assert expect
== actual
189 text
= 'a bb ccc dddd\n\teeeee'
190 expect
= 'a bb\nccc\ndddd\n\t\neeeee'
191 actual
= textwrap
.word_wrap(text
, 8, 4)
192 assert expect
== actual
196 text
= 'xx0 xx1 xx2 xx3 xx4 xx5 xx6 xx7 xx8 xx9 xxa xxb'
198 expect
= 'xx0 xx1 xx2 xx3 xx4 xx5 xx6\nxx7 xx8 xx9 xxa xxb'
199 actual
= textwrap
.word_wrap(text
, 8, 27)
200 assert expect
== actual
202 expect
= 'xx0 xx1 xx2 xx3 xx4 xx5\nxx6 xx7 xx8 xx9 xxa xxb'
203 actual
= textwrap
.word_wrap(text
, 8, 26)
204 assert expect
== actual
206 actual
= textwrap
.word_wrap(text
, 8, 25)
207 assert expect
== actual
209 actual
= textwrap
.word_wrap(text
, 8, 24)
210 assert expect
== actual
212 actual
= textwrap
.word_wrap(text
, 8, 23)
213 assert expect
== actual
215 expect
= 'xx0 xx1 xx2 xx3 xx4\nxx5 xx6 xx7 xx8 xx9\nxxa xxb'
216 actual
= textwrap
.word_wrap(text
, 8, 22)
217 assert expect
== actual