commitmsg: use immediate connections
[git-cola.git] / test / textwrap_test.py
blob5c120ccf9ef07b45679b430d0d37ecef542160eb
1 """Test the textwrap module"""
2 # pylint: disable=redefined-outer-name
3 from __future__ import absolute_import, division, print_function, unicode_literals
5 import pytest
7 from cola import textwrap
10 class WordWrapDefaults(object):
11 def __init__(self):
12 self.tabwidth = 8
13 self.limit = None
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
21 @pytest.fixture
22 def wordwrap():
23 """Provide default word wrap options for tests"""
24 return WordWrapDefaults()
27 def test_word_wrap(wordwrap):
28 wordwrap.limit = 16
29 text = """
30 12345678901 3 56 8 01 3 5 7
32 1 3 5"""
33 expect = """
34 12345678901 3 56
35 8 01 3 5 7
37 1 3 5"""
38 assert expect == wordwrap.wrap(text)
41 def test_word_wrap_dashes(wordwrap):
42 wordwrap.limit = 4
43 text = '123-5'
44 expect = '123-5'
45 assert expect == wordwrap.wrap(text)
48 def test_word_wrap_leading_spaces(wordwrap):
49 wordwrap.limit = 4
50 expect = '1234\n5'
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')
58 expect = '123\n4'
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):
67 wordwrap.limit = 4
68 text = '12--5'
69 expect = '12--\n5'
70 actual = wordwrap.wrap(text, break_on_hyphens=True)
71 assert expect == actual
73 expect = '12--5'
74 actual = wordwrap.wrap(text, break_on_hyphens=False)
75 assert expect == actual
78 def test_word_wrap_many_lines(wordwrap):
79 wordwrap.limit = 2
80 text = """
84 bb cc dd"""
86 expect = """
92 dd"""
93 actual = wordwrap.wrap(text)
94 assert expect == actual
97 def test_word_python_code(wordwrap):
98 wordwrap.limit = 78
99 text = """
100 if True:
101 print "hello world"
102 else:
103 print "hello world"
106 expect = text
107 actual = wordwrap.wrap(text)
108 assert expect == actual
111 def test_word_wrap_spaces(wordwrap):
112 wordwrap.limit = 2
113 text = ' ' * 6
114 expect = ''
115 actual = wordwrap.wrap(text)
116 assert expect == actual
119 def test_word_wrap_special_tag(wordwrap):
120 wordwrap.limit = 2
121 text = """
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
132 expect = """
133 This
134 test
137 meta,
138 even
139 this
140 sentence
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"""
156 wordwrap.limit = 20
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"""
165 wordwrap.limit = 20
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"""
174 wordwrap.limit = 20
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
196 def test_triplets():
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