blob: cb63387bb046072cb27a07c5b8801c06abc3ab6e [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 = [
Greg Ward9ebba9a2002-08-22 18:45:02 +000048 (12, ["Hello there,",
49 "how are you",
50 "this fine",
51 "day? I'm",
52 "glad to hear",
53 "it!"]),
54 (42, ["Hello there, how are you this fine day?",
55 "I'm glad to hear it!"]),
56 (80, [t]),
Greg Ward90c0b072002-08-22 18:11:10 +000057 ]
58
Greg Ward9ebba9a2002-08-22 18:45:02 +000059 for width, expect in subcases:
60 result = wrap(t, width)
Greg Ward90c0b072002-08-22 18:11:10 +000061 self.check(result, expect)
62
63
Greg Wardf6765782002-08-22 18:35:49 +000064 def test_whitespace(self):
Greg Ward90c0b072002-08-22 18:11:10 +000065 '''Whitespace munging and end-of-sentence detection.'''
66
67 t = """\
68This is a paragraph that already has
69line breaks. But some of its lines are much longer than the others,
70so it needs to be wrapped.
71Some lines are \ttabbed too.
72What a mess!
73"""
74
Greg Ward90c0b072002-08-22 18:11:10 +000075 expect = [
76 "This is a paragraph that already has line",
77 "breaks. But some of its lines are much",
78 "longer than the others, so it needs to be",
79 "wrapped. Some lines are tabbed too. What a",
80 "mess!"
81 ]
82
83 result = self.wrapper.wrap(t)
84 self.check(result, expect)
85
86 result = self.wrapper.fill(t)
87 self.check(result, '\n'.join(expect))
88
89
Greg Wardf6765782002-08-22 18:35:49 +000090 def test_wrap_short(self):
Greg Ward90c0b072002-08-22 18:11:10 +000091 '''Wrapping to make short lines longer.'''
92
93 t = "This is a\nshort paragraph."
94
Greg Ward90c0b072002-08-22 18:11:10 +000095 subcases = [
Greg Ward9ebba9a2002-08-22 18:45:02 +000096 (20, ["This is a short",
97 "paragraph."]),
98 (40, ["This is a short paragraph."]),
Greg Ward90c0b072002-08-22 18:11:10 +000099 ]
100
Greg Ward9ebba9a2002-08-22 18:45:02 +0000101 for width, expect in subcases:
102 result = wrap(t, width)
Greg Ward90c0b072002-08-22 18:11:10 +0000103 self.check(result, expect)
104
105
Greg Wardf6765782002-08-22 18:35:49 +0000106 def test_hyphenated(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000107 '''Test breaking hyphenated words.'''
108
109 t = "this-is-a-useful-feature-for-reformatting-posts-from-tim-peters'ly"
110
111 subcases = [
Greg Ward9ebba9a2002-08-22 18:45:02 +0000112 (40, ["this-is-a-useful-feature-for-",
113 "reformatting-posts-from-tim-peters'ly"]),
114 (41, ["this-is-a-useful-feature-for-",
115 "reformatting-posts-from-tim-peters'ly"]),
116 (42, ["this-is-a-useful-feature-for-reformatting-",
117 "posts-from-tim-peters'ly"]),
Greg Ward90c0b072002-08-22 18:11:10 +0000118 ]
119
Greg Ward9ebba9a2002-08-22 18:45:02 +0000120 for width, expect in subcases:
121 result = wrap(t, width)
Greg Ward90c0b072002-08-22 18:11:10 +0000122 self.check(result, expect)
123
124
125 def test_split(self):
126 '''Ensure that the standard _split() method works as advertised
Greg Wardf6765782002-08-22 18:35:49 +0000127 in the comments.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000128
129 t = "Hello there -- you goof-ball, use the -b option!"
130
131 result = self.wrapper._split(t)
132 self.check(result,
133 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
134 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
135
136
Greg Wardf6765782002-08-22 18:35:49 +0000137 def test_funky_punc(self):
138 '''Wrap text with long words and lots of punctuation.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000139
140 t = '''
141Did you say "supercalifragilisticexpialidocious?"
142How *do* you spell that odd word, anyways?
143'''
Greg Ward90c0b072002-08-22 18:11:10 +0000144 subcases = [
Greg Ward9ebba9a2002-08-22 18:45:02 +0000145 (30, ['Did you say "supercalifragilis',
146 'ticexpialidocious?" How *do*',
147 'you spell that odd word,',
148 'anyways?']),
149 (50, ['Did you say "supercalifragilisticexpialidocious?"',
150 'How *do* you spell that odd word, anyways?']),
Greg Ward90c0b072002-08-22 18:11:10 +0000151 ]
152
Greg Ward9ebba9a2002-08-22 18:45:02 +0000153 for width, expect in subcases:
154 result = wrap(t, width)
Greg Ward90c0b072002-08-22 18:11:10 +0000155 self.check(result, expect)
156
157
Greg Wardf6765782002-08-22 18:35:49 +0000158 def test_long_words(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000159 '''Test with break_long_words disabled.'''
160 t = '''
161Did you say "supercalifragilisticexpialidocious?"
162How *do* you spell that odd word, anyways?
163'''
164 self.wrapper.break_long_words = 0
165 self.wrapper.width = 30
166 result = self.wrapper.wrap(t)
167 expect = [
168 'Did you say',
169 '"supercalifragilisticexpialidocious?"',
170 'How *do* you spell that odd',
171 'word, anyways?'
172 ]
173 self.check(result, expect)
174
175 # Same thing with kwargs passed to standalone wrap() function.
176 result = wrap(t, width=30, break_long_words=0)
177 self.check(result, expect)
178
179
180
181class IndentTestCases(WrapperTestCase):
182
183 # called before each test method
184 def setUp(self):
185 self.testString = '''\
186This paragraph will be filled, first without any indentation,
187and then with some (including a hanging indent).'''
188
189
Greg Wardf6765782002-08-22 18:35:49 +0000190 def test_fill(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000191 '''Test the fill() method.'''
192
193 expect = '''\
194This paragraph will be filled, first
195without any indentation, and then with
196some (including a hanging indent).'''
197
198 result = fill(self.testString, 40)
199 self.check(result, expect)
200
201
Greg Wardf6765782002-08-22 18:35:49 +0000202 def test_initial_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000203 '''Test initial_indent parameter.'''
204
205 expect = [
206 " This paragraph will be filled,",
207 "first without any indentation, and then",
208 "with some (including a hanging indent)."]
209
210 result = wrap(self.testString, 40, initial_indent=" ")
211 self.check(result, expect)
212
213 expect = '''\
214 This paragraph will be filled,
215first without any indentation, and then
216with some (including a hanging indent).'''
217
218 result = fill(self.testString, 40, initial_indent=" ")
219 self.check(result, expect)
220
221
Greg Wardf6765782002-08-22 18:35:49 +0000222 def test_subsequent_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000223 '''Test subsequent_indent parameter.'''
224
225 expect = '''\
226 * This paragraph will be filled, first
227 without any indentation, and then
228 with some (including a hanging
229 indent).'''
230
231 result = fill(self.testString, 40, initial_indent=" * ",
232 subsequent_indent=" ")
233 self.check(result, expect)
234
235
Greg Wardf6765782002-08-22 18:35:49 +0000236def test_main():
237 suite = unittest.TestSuite()
238 suite.addTest(unittest.makeSuite(WrapTestCase))
239 suite.addTest(unittest.makeSuite(IndentTestCases))
240 test_support.run_suite(suite)
241
Greg Ward90c0b072002-08-22 18:11:10 +0000242if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000243 test_main()