blob: f36213ccc0d6ba0087760c7c0acdbb3e8c3d79fd [file] [log] [blame]
Greg Ward3dc94e12002-08-22 18:37:50 +00001#
2# Test script for the textwrap module.
3#
4# Original tests written by Greg Ward <gward@python.net>.
5# Converted to PyUnit by Peter Hansen <peter@engcorp.com>.
6# Currently maintained by Greg Ward.
7#
8# $Id$
9#
10
Greg Ward90c0b072002-08-22 18:11:10 +000011import unittest
Greg Wardf6765782002-08-22 18:35:49 +000012from test import test_support
Greg Ward90c0b072002-08-22 18:11:10 +000013
Greg Ward90c0b072002-08-22 18:11:10 +000014from textwrap import TextWrapper, wrap, fill
15
16
17class WrapperTestCase(unittest.TestCase):
18 '''Parent class with utility methods for textwrap tests.'''
19
20 def show(self, textin):
21 if isinstance(textin, list):
22 result = []
23 for i in range(len(textin)):
24 result.append(" %d: %r" % (i, textin[i]))
25 result = '\n'.join(result)
26 elif isinstance(textin, (str, unicode)):
27 result = " %s\n" % repr(textin)
28 return result
29
30
31 def check(self, result, expect):
32 self.assertEquals(result, expect,
33 'Expected:\n%s\nbut got:\n%s' % (
34 self.show(result), self.show(expect)))
35
36
Greg Ward90c0b072002-08-22 18:11:10 +000037class WrapTestCase(WrapperTestCase):
38
Greg Ward90c0b072002-08-22 18:11:10 +000039 def setUp(self):
40 self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
41
Greg Wardf6765782002-08-22 18:35:49 +000042 def test_simple(self):
Greg Ward90c0b072002-08-22 18:11:10 +000043 '''Simple case: just words, spaces, and a bit of punctuation.'''
44
45 t = "Hello there, how are you this fine day? I'm glad to hear it!"
46
Greg Ward90c0b072002-08-22 18:11:10 +000047 subcases = [
48 ( (t, 12), [
49 "Hello there,",
50 "how are you",
51 "this fine",
52 "day? I'm",
53 "glad to hear",
54 "it!"
55 ] ),
56 ( (t, 42), [
57 "Hello there, how are you this fine day?",
58 "I'm glad to hear it!"
59 ] ),
60 ( (t, 80), [
61 t
62 ] ),
63 ]
64
65 for test, expect in subcases:
66 result = wrap(*test)
67 self.check(result, expect)
68
69
Greg Wardf6765782002-08-22 18:35:49 +000070 def test_whitespace(self):
Greg Ward90c0b072002-08-22 18:11:10 +000071 '''Whitespace munging and end-of-sentence detection.'''
72
73 t = """\
74This is a paragraph that already has
75line breaks. But some of its lines are much longer than the others,
76so it needs to be wrapped.
77Some lines are \ttabbed too.
78What a mess!
79"""
80
Greg Ward90c0b072002-08-22 18:11:10 +000081 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
Greg Ward90c0b072002-08-22 18:11:10 +0000101 subcases = [
102 ( (t, 20), [
103 "This is a short",
104 "paragraph."
105 ] ),
106 ( (t, 40), [
107 "This is a short paragraph."
108 ] ),
109 ]
110
111 for test, expect in subcases:
112 result = wrap(*test)
113 self.check(result, expect)
114
115
Greg Wardf6765782002-08-22 18:35:49 +0000116 def test_hyphenated(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000117 '''Test breaking hyphenated words.'''
118
119 t = "this-is-a-useful-feature-for-reformatting-posts-from-tim-peters'ly"
120
121 subcases = [
122 ( (t, 40), [
123 "this-is-a-useful-feature-for-",
124 "reformatting-posts-from-tim-peters'ly"
125 ] ),
126 ( (t, 41), [
127 "this-is-a-useful-feature-for-",
128 "reformatting-posts-from-tim-peters'ly"
129 ] ),
130 ( (t, 42), [
131 "this-is-a-useful-feature-for-reformatting-",
132 "posts-from-tim-peters'ly"
133 ] ),
134 ]
135
136 for test, expect in subcases:
137 result = wrap(*test)
138 self.check(result, expect)
139
140
141 def test_split(self):
142 '''Ensure that the standard _split() method works as advertised
Greg Wardf6765782002-08-22 18:35:49 +0000143 in the comments.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000144
145 t = "Hello there -- you goof-ball, use the -b option!"
146
147 result = self.wrapper._split(t)
148 self.check(result,
149 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
150 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
151
152
Greg Wardf6765782002-08-22 18:35:49 +0000153 def test_funky_punc(self):
154 '''Wrap text with long words and lots of punctuation.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000155
156 t = '''
157Did you say "supercalifragilisticexpialidocious?"
158How *do* you spell that odd word, anyways?
159'''
Greg Ward90c0b072002-08-22 18:11:10 +0000160 subcases = [
161 ( (t, 30), [
162 'Did you say "supercalifragilis',
163 'ticexpialidocious?" How *do*',
164 'you spell that odd word,',
165 'anyways?'
166 ] ),
167 ( (t, 50), [
168 'Did you say "supercalifragilisticexpialidocious?"',
169 'How *do* you spell that odd word, anyways?'
170 ] ),
171 ]
172
173 for test, expect in subcases:
174 result = wrap(*test)
175 self.check(result, expect)
176
177
Greg Wardf6765782002-08-22 18:35:49 +0000178 def test_long_words(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000179 '''Test with break_long_words disabled.'''
180 t = '''
181Did you say "supercalifragilisticexpialidocious?"
182How *do* you spell that odd word, anyways?
183'''
184 self.wrapper.break_long_words = 0
185 self.wrapper.width = 30
186 result = self.wrapper.wrap(t)
187 expect = [
188 'Did you say',
189 '"supercalifragilisticexpialidocious?"',
190 'How *do* you spell that odd',
191 'word, anyways?'
192 ]
193 self.check(result, expect)
194
195 # Same thing with kwargs passed to standalone wrap() function.
196 result = wrap(t, width=30, break_long_words=0)
197 self.check(result, expect)
198
199
200
201class IndentTestCases(WrapperTestCase):
202
203 # called before each test method
204 def setUp(self):
205 self.testString = '''\
206This paragraph will be filled, first without any indentation,
207and then with some (including a hanging indent).'''
208
209
Greg Wardf6765782002-08-22 18:35:49 +0000210 def test_fill(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000211 '''Test the fill() method.'''
212
213 expect = '''\
214This paragraph will be filled, first
215without any indentation, and then with
216some (including a hanging indent).'''
217
218 result = fill(self.testString, 40)
219 self.check(result, expect)
220
221
Greg Wardf6765782002-08-22 18:35:49 +0000222 def test_initial_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000223 '''Test initial_indent parameter.'''
224
225 expect = [
226 " This paragraph will be filled,",
227 "first without any indentation, and then",
228 "with some (including a hanging indent)."]
229
230 result = wrap(self.testString, 40, initial_indent=" ")
231 self.check(result, expect)
232
233 expect = '''\
234 This paragraph will be filled,
235first without any indentation, and then
236with some (including a hanging indent).'''
237
238 result = fill(self.testString, 40, initial_indent=" ")
239 self.check(result, expect)
240
241
Greg Wardf6765782002-08-22 18:35:49 +0000242 def test_subsequent_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000243 '''Test subsequent_indent parameter.'''
244
245 expect = '''\
246 * This paragraph will be filled, first
247 without any indentation, and then
248 with some (including a hanging
249 indent).'''
250
251 result = fill(self.testString, 40, initial_indent=" * ",
252 subsequent_indent=" ")
253 self.check(result, expect)
254
255
Greg Wardf6765782002-08-22 18:35:49 +0000256def test_main():
257 suite = unittest.TestSuite()
258 suite.addTest(unittest.makeSuite(WrapTestCase))
259 suite.addTest(unittest.makeSuite(IndentTestCases))
260 test_support.run_suite(suite)
261
Greg Ward90c0b072002-08-22 18:11:10 +0000262if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000263 test_main()