blob: b4810db39e9bb2679cad728d635ab904a1b3b370 [file] [log] [blame]
Greg Ward90c0b072002-08-22 18:11:10 +00001import unittest
Greg Wardf6765782002-08-22 18:35:49 +00002from test import test_support
Greg Ward90c0b072002-08-22 18:11:10 +00003
Greg Ward90c0b072002-08-22 18:11:10 +00004from textwrap import TextWrapper, wrap, fill
5
6
7class WrapperTestCase(unittest.TestCase):
8 '''Parent class with utility methods for textwrap tests.'''
9
10 def show(self, textin):
11 if isinstance(textin, list):
12 result = []
13 for i in range(len(textin)):
14 result.append(" %d: %r" % (i, textin[i]))
15 result = '\n'.join(result)
16 elif isinstance(textin, (str, unicode)):
17 result = " %s\n" % repr(textin)
18 return result
19
20
21 def check(self, result, expect):
22 self.assertEquals(result, expect,
23 'Expected:\n%s\nbut got:\n%s' % (
24 self.show(result), self.show(expect)))
25
26
27
28# Note: a new TestCase instance is created before running each
29# test method.
30class WrapTestCase(WrapperTestCase):
31
32 # called before each test method
33 def setUp(self):
34 self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
35
36
37 # Note: any methods that start with "test" are called automatically
38 # by the unittest framework.
39
Greg Wardf6765782002-08-22 18:35:49 +000040 def test_simple(self):
Greg Ward90c0b072002-08-22 18:11:10 +000041 '''Simple case: just words, spaces, and a bit of punctuation.'''
42
43 t = "Hello there, how are you this fine day? I'm glad to hear it!"
44
45 # bizarre formatting intended to increase maintainability
46 subcases = [
47 ( (t, 12), [
48 "Hello there,",
49 "how are you",
50 "this fine",
51 "day? I'm",
52 "glad to hear",
53 "it!"
54 ] ),
55 ( (t, 42), [
56 "Hello there, how are you this fine day?",
57 "I'm glad to hear it!"
58 ] ),
59 ( (t, 80), [
60 t
61 ] ),
62 ]
63
64 for test, expect in subcases:
65 result = wrap(*test)
66 self.check(result, expect)
67
68
Greg Wardf6765782002-08-22 18:35:49 +000069 def test_whitespace(self):
Greg Ward90c0b072002-08-22 18:11:10 +000070 '''Whitespace munging and end-of-sentence detection.'''
71
72 t = """\
73This is a paragraph that already has
74line breaks. But some of its lines are much longer than the others,
75so it needs to be wrapped.
76Some lines are \ttabbed too.
77What a mess!
78"""
79
80 # bizarre formatting intended to increase maintainability
81 expect = [
82 "This is a paragraph that already has line",
83 "breaks. But some of its lines are much",
84 "longer than the others, so it needs to be",
85 "wrapped. Some lines are tabbed too. What a",
86 "mess!"
87 ]
88
89 result = self.wrapper.wrap(t)
90 self.check(result, expect)
91
92 result = self.wrapper.fill(t)
93 self.check(result, '\n'.join(expect))
94
95
Greg Wardf6765782002-08-22 18:35:49 +000096 def test_wrap_short(self):
Greg Ward90c0b072002-08-22 18:11:10 +000097 '''Wrapping to make short lines longer.'''
98
99 t = "This is a\nshort paragraph."
100
101 # bizarre formatting intended to increase maintainability
102 subcases = [
103 ( (t, 20), [
104 "This is a short",
105 "paragraph."
106 ] ),
107 ( (t, 40), [
108 "This is a short paragraph."
109 ] ),
110 ]
111
112 for test, expect in subcases:
113 result = wrap(*test)
114 self.check(result, expect)
115
116
Greg Wardf6765782002-08-22 18:35:49 +0000117 def test_hyphenated(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000118 '''Test breaking hyphenated words.'''
119
120 t = "this-is-a-useful-feature-for-reformatting-posts-from-tim-peters'ly"
121
122 subcases = [
123 ( (t, 40), [
124 "this-is-a-useful-feature-for-",
125 "reformatting-posts-from-tim-peters'ly"
126 ] ),
127 ( (t, 41), [
128 "this-is-a-useful-feature-for-",
129 "reformatting-posts-from-tim-peters'ly"
130 ] ),
131 ( (t, 42), [
132 "this-is-a-useful-feature-for-reformatting-",
133 "posts-from-tim-peters'ly"
134 ] ),
135 ]
136
137 for test, expect in subcases:
138 result = wrap(*test)
139 self.check(result, expect)
140
141
142 def test_split(self):
143 '''Ensure that the standard _split() method works as advertised
Greg Wardf6765782002-08-22 18:35:49 +0000144 in the comments.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000145
146 t = "Hello there -- you goof-ball, use the -b option!"
147
148 result = self.wrapper._split(t)
149 self.check(result,
150 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
151 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
152
153
Greg Wardf6765782002-08-22 18:35:49 +0000154 def test_funky_punc(self):
155 '''Wrap text with long words and lots of punctuation.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000156
157 t = '''
158Did you say "supercalifragilisticexpialidocious?"
159How *do* you spell that odd word, anyways?
160'''
161 # bizarre formatting intended to increase maintainability
162 subcases = [
163 ( (t, 30), [
164 'Did you say "supercalifragilis',
165 'ticexpialidocious?" How *do*',
166 'you spell that odd word,',
167 'anyways?'
168 ] ),
169 ( (t, 50), [
170 'Did you say "supercalifragilisticexpialidocious?"',
171 'How *do* you spell that odd word, anyways?'
172 ] ),
173 ]
174
175 for test, expect in subcases:
176 result = wrap(*test)
177 self.check(result, expect)
178
179
Greg Wardf6765782002-08-22 18:35:49 +0000180 def test_long_words(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000181 '''Test with break_long_words disabled.'''
182 t = '''
183Did you say "supercalifragilisticexpialidocious?"
184How *do* you spell that odd word, anyways?
185'''
186 self.wrapper.break_long_words = 0
187 self.wrapper.width = 30
188 result = self.wrapper.wrap(t)
189 expect = [
190 'Did you say',
191 '"supercalifragilisticexpialidocious?"',
192 'How *do* you spell that odd',
193 'word, anyways?'
194 ]
195 self.check(result, expect)
196
197 # Same thing with kwargs passed to standalone wrap() function.
198 result = wrap(t, width=30, break_long_words=0)
199 self.check(result, expect)
200
201
202
203class IndentTestCases(WrapperTestCase):
204
205 # called before each test method
206 def setUp(self):
207 self.testString = '''\
208This paragraph will be filled, first without any indentation,
209and then with some (including a hanging indent).'''
210
211
Greg Wardf6765782002-08-22 18:35:49 +0000212 def test_fill(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000213 '''Test the fill() method.'''
214
215 expect = '''\
216This paragraph will be filled, first
217without any indentation, and then with
218some (including a hanging indent).'''
219
220 result = fill(self.testString, 40)
221 self.check(result, expect)
222
223
Greg Wardf6765782002-08-22 18:35:49 +0000224 def test_initial_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000225 '''Test initial_indent parameter.'''
226
227 expect = [
228 " This paragraph will be filled,",
229 "first without any indentation, and then",
230 "with some (including a hanging indent)."]
231
232 result = wrap(self.testString, 40, initial_indent=" ")
233 self.check(result, expect)
234
235 expect = '''\
236 This paragraph will be filled,
237first without any indentation, and then
238with some (including a hanging indent).'''
239
240 result = fill(self.testString, 40, initial_indent=" ")
241 self.check(result, expect)
242
243
Greg Wardf6765782002-08-22 18:35:49 +0000244 def test_subsequent_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000245 '''Test subsequent_indent parameter.'''
246
247 expect = '''\
248 * This paragraph will be filled, first
249 without any indentation, and then
250 with some (including a hanging
251 indent).'''
252
253 result = fill(self.testString, 40, initial_indent=" * ",
254 subsequent_indent=" ")
255 self.check(result, expect)
256
257
Greg Wardf6765782002-08-22 18:35:49 +0000258def test_main():
259 suite = unittest.TestSuite()
260 suite.addTest(unittest.makeSuite(WrapTestCase))
261 suite.addTest(unittest.makeSuite(IndentTestCases))
262 test_support.run_suite(suite)
263
Greg Ward90c0b072002-08-22 18:11:10 +0000264if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000265 test_main()