1 """Test the textwrap module"""
2 # pylint: disable=redefined-outer-name
3 from __future__
import absolute_import
, division
, print_function
, unicode_literals
7 from cola
import textwrap
10 class WordWrapDefaults(object):
15 def wrap(self
, text
, break_on_hyphens
=True):
16 return textwrap
.word_wrap(
17 text
, self
.tabwidth
, self
.limit
, break_on_hyphens
=break_on_hyphens
23 """Provide default word wrap options for tests"""
24 return WordWrapDefaults()
27 def test_word_wrap(wordwrap
):
30 12345678901 3 56 8 01 3 5 7
38 assert expect
== wordwrap
.wrap(text
)
41 def test_word_wrap_dashes(wordwrap
):
45 assert expect
== wordwrap
.wrap(text
)
48 def test_word_wrap_leading_spaces(wordwrap
):
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')
56 assert expect
== wordwrap
.wrap('1234 5')
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')
63 assert expect
== wordwrap
.wrap('123 4')
66 def test_word_wrap_double_dashes(wordwrap
):
70 actual
= wordwrap
.wrap(text
, break_on_hyphens
=True)
71 assert expect
== actual
74 actual
= wordwrap
.wrap(text
, break_on_hyphens
=False)
75 assert expect
== actual
78 def test_word_wrap_many_lines(wordwrap
):
93 actual
= wordwrap
.wrap(text
)
94 assert expect
== actual
97 def test_word_python_code(wordwrap
):
107 actual
= wordwrap
.wrap(text
)
108 assert expect
== actual
111 def test_word_wrap_spaces(wordwrap
):
115 actual
= wordwrap
.wrap(text
)
116 assert expect
== actual
119 def test_word_wrap_special_tag(wordwrap
):
122 This test is so meta, even this sentence
124 Cheered-on-by: Avoids word-wrap
125 C.f. This also avoids word-wrap
126 References: This also avoids word-wrap
127 See-also: This also avoids word-wrap
128 Related-to: This also avoids word-wrap
129 Link: This also avoids word-wrap
142 Cheered-on-by: Avoids word-wrap
143 C.f. This also avoids word-wrap
144 References: This also avoids word-wrap
145 See-also: This also avoids word-wrap
146 Related-to: This also avoids word-wrap
147 Link: This also avoids word-wrap
149 actual
= wordwrap
.wrap(text
)
150 assert expect
== actual
153 def test_word_wrap_space_at_start_of_wrap(wordwrap
):
154 inputs
= """0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 """
155 expect
= """0 1 2 3 4 5 6 7 8 9\n0 1 2 3 4 5 6 7 8"""
158 actual
= wordwrap
.wrap(inputs
)
159 assert expect
== actual
162 def test_word_wrap_keeps_tabs_at_start(wordwrap
):
163 inputs
= """\tfirst line\n\n\tsecond line"""
164 expect
= """\tfirst line\n\n\tsecond line"""
167 actual
= wordwrap
.wrap(inputs
)
168 assert expect
== actual
171 def test_word_wrap_keeps_twospace_indents(wordwrap
):
172 inputs
= """first line\n\n* branch:\n line1\n line2\n"""
173 expect
= """first line\n\n* branch:\n line1\n line2\n"""
176 actual
= wordwrap
.wrap(inputs
)
177 assert expect
== actual
180 def test_word_wrap_ranges():
181 text
= 'a bb ccc dddd\neeeee'
182 expect
= 'a\nbb\nccc\ndddd\neeeee'
183 actual
= textwrap
.word_wrap(text
, 8, 2)
184 assert expect
== actual
186 expect
= 'a bb\nccc\ndddd\neeeee'
187 actual
= textwrap
.word_wrap(text
, 8, 4)
188 assert expect
== actual
190 text
= 'a bb ccc dddd\n\teeeee'
191 expect
= 'a bb\nccc\ndddd\n\t\neeeee'
192 actual
= textwrap
.word_wrap(text
, 8, 4)
193 assert expect
== actual
197 text
= 'xx0 xx1 xx2 xx3 xx4 xx5 xx6 xx7 xx8 xx9 xxa xxb'
199 expect
= 'xx0 xx1 xx2 xx3 xx4 xx5 xx6\nxx7 xx8 xx9 xxa xxb'
200 actual
= textwrap
.word_wrap(text
, 8, 27)
201 assert expect
== actual
203 expect
= 'xx0 xx1 xx2 xx3 xx4 xx5\nxx6 xx7 xx8 xx9 xxa xxb'
204 actual
= textwrap
.word_wrap(text
, 8, 26)
205 assert expect
== actual
207 actual
= textwrap
.word_wrap(text
, 8, 25)
208 assert expect
== actual
210 actual
= textwrap
.word_wrap(text
, 8, 24)
211 assert expect
== actual
213 actual
= textwrap
.word_wrap(text
, 8, 23)
214 assert expect
== actual
216 expect
= 'xx0 xx1 xx2 xx3 xx4\nxx5 xx6 xx7 xx8 xx9\nxxa xxb'
217 actual
= textwrap
.word_wrap(text
, 8, 22)
218 assert expect
== actual