[pre-commit.ci] auto fixes from pre-commit.com hooks
[git-cola.git] / test / textwrap_test.py
blob0241022f96e99ef83d7656942143fa5b529c19e5
1 """Test the textwrap module"""
2 # pylint: disable=redefined-outer-name
4 import pytest
6 from cola import textwrap
9 class WordWrapDefaults:
10 def __init__(self):
11 self.tabwidth = 8
12 self.limit = None
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
20 @pytest.fixture
21 def wordwrap():
22 """Provide default word wrap options for tests"""
23 return WordWrapDefaults()
26 def test_word_wrap(wordwrap):
27 wordwrap.limit = 16
28 text = """
29 12345678901 3 56 8 01 3 5 7
31 1 3 5"""
32 expect = """
33 12345678901 3 56
34 8 01 3 5 7
36 1 3 5"""
37 assert expect == wordwrap.wrap(text)
40 def test_word_wrap_dashes(wordwrap):
41 wordwrap.limit = 4
42 text = '123-5'
43 expect = '123-5'
44 assert expect == wordwrap.wrap(text)
47 def test_word_wrap_leading_spaces(wordwrap):
48 wordwrap.limit = 4
49 expect = '1234\n5'
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')
57 expect = '123\n4'
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):
66 wordwrap.limit = 4
67 text = '12--5'
68 expect = '12--\n5'
69 actual = wordwrap.wrap(text, break_on_hyphens=True)
70 assert expect == actual
72 expect = '12--5'
73 actual = wordwrap.wrap(text, break_on_hyphens=False)
74 assert expect == actual
77 def test_word_wrap_many_lines(wordwrap):
78 wordwrap.limit = 2
79 text = """
83 bb cc dd"""
85 expect = """
91 dd"""
92 actual = wordwrap.wrap(text)
93 assert expect == actual
96 def test_word_python_code(wordwrap):
97 wordwrap.limit = 78
98 text = """
99 if True:
100 print "hello world"
101 else:
102 print "hello world"
105 expect = text
106 actual = wordwrap.wrap(text)
107 assert expect == actual
110 def test_word_wrap_spaces(wordwrap):
111 wordwrap.limit = 2
112 text = ' ' * 6
113 expect = ''
114 actual = wordwrap.wrap(text)
115 assert expect == actual
118 def test_word_wrap_special_tag(wordwrap):
119 wordwrap.limit = 2
120 text = """
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
131 expect = """
132 This
133 test
136 meta,
137 even
138 this
139 sentence
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"""
155 wordwrap.limit = 20
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"""
164 wordwrap.limit = 20
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"""
173 wordwrap.limit = 20
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
195 def test_triplets():
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