blob: d6bd47c31b7d34a5c053659a0bec6b01dd7a8fc3 [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
Greg Ward13c53c62002-08-22 18:57:26 +000017class BaseTestCase(unittest.TestCase):
Greg Ward90c0b072002-08-22 18:11:10 +000018 '''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):
Guido van Rossum327af772002-08-22 20:13:47 +000032 self.assertEquals(result, expect,
Greg Ward9ad15a32002-08-22 19:47:27 +000033 'expected:\n%s\nbut got:\n%s' % (
34 self.show(expect), self.show(result)))
Greg Ward90c0b072002-08-22 18:11:10 +000035
Greg Wardee413842002-08-22 18:55:38 +000036 def check_wrap (self, text, width, expect):
37 result = wrap(text, width)
38 self.check(result, expect)
39
Greg Ward90c0b072002-08-22 18:11:10 +000040
Greg Ward13c53c62002-08-22 18:57:26 +000041class WrapTestCase(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +000042
Greg Ward90c0b072002-08-22 18:11:10 +000043 def setUp(self):
44 self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
45
Greg Wardf6765782002-08-22 18:35:49 +000046 def test_simple(self):
Guido van Rossum327af772002-08-22 20:13:47 +000047 # Simple case: just words, spaces, and a bit of punctuation
Greg Ward90c0b072002-08-22 18:11:10 +000048
Greg Wardee413842002-08-22 18:55:38 +000049 text = "Hello there, how are you this fine day? I'm glad to hear it!"
Greg Ward90c0b072002-08-22 18:11:10 +000050
Greg Wardee413842002-08-22 18:55:38 +000051 self.check_wrap(text, 12,
52 ["Hello there,",
53 "how are you",
54 "this fine",
55 "day? I'm",
56 "glad to hear",
57 "it!"])
58 self.check_wrap(text, 42,
59 ["Hello there, how are you this fine day?",
60 "I'm glad to hear it!"])
61 self.check_wrap(text, 80, [text])
Greg Ward90c0b072002-08-22 18:11:10 +000062
63
Greg Wardf6765782002-08-22 18:35:49 +000064 def test_whitespace(self):
Guido van Rossum327af772002-08-22 20:13:47 +000065 # Whitespace munging and end-of-sentence detection
Greg Ward90c0b072002-08-22 18:11:10 +000066
Greg Wardee413842002-08-22 18:55:38 +000067 text = """\
Greg Ward90c0b072002-08-22 18:11:10 +000068This 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 Wardee413842002-08-22 18:55:38 +000075 expect = ["This is a paragraph that already has line",
76 "breaks. But some of its lines are much",
77 "longer than the others, so it needs to be",
78 "wrapped. Some lines are tabbed too. What a",
79 "mess!"]
Greg Ward90c0b072002-08-22 18:11:10 +000080
Greg Wardee413842002-08-22 18:55:38 +000081 result = self.wrapper.wrap(text)
Greg Ward90c0b072002-08-22 18:11:10 +000082 self.check(result, expect)
83
Greg Wardee413842002-08-22 18:55:38 +000084 result = self.wrapper.fill(text)
Greg Ward90c0b072002-08-22 18:11:10 +000085 self.check(result, '\n'.join(expect))
86
87
Greg Wardf6765782002-08-22 18:35:49 +000088 def test_wrap_short(self):
Guido van Rossum327af772002-08-22 20:13:47 +000089 # Wrapping to make short lines longer
Greg Ward90c0b072002-08-22 18:11:10 +000090
Greg Wardee413842002-08-22 18:55:38 +000091 text = "This is a\nshort paragraph."
Greg Ward90c0b072002-08-22 18:11:10 +000092
Greg Wardee413842002-08-22 18:55:38 +000093 self.check_wrap(text, 20, ["This is a short",
94 "paragraph."])
95 self.check_wrap(text, 40, ["This is a short paragraph."])
Greg Ward90c0b072002-08-22 18:11:10 +000096
97
Greg Wardf6765782002-08-22 18:35:49 +000098 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +000099 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000100
Greg Wardee413842002-08-22 18:55:38 +0000101 text = ("this-is-a-useful-feature-for-"
102 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000103
Greg Wardee413842002-08-22 18:55:38 +0000104 self.check_wrap(text, 40,
105 ["this-is-a-useful-feature-for-",
106 "reformatting-posts-from-tim-peters'ly"])
107 self.check_wrap(text, 41,
108 ["this-is-a-useful-feature-for-",
109 "reformatting-posts-from-tim-peters'ly"])
110 self.check_wrap(text, 42,
111 ["this-is-a-useful-feature-for-reformatting-",
112 "posts-from-tim-peters'ly"])
Greg Ward90c0b072002-08-22 18:11:10 +0000113
Greg Ward9ad15a32002-08-22 19:47:27 +0000114 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000115 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000116 text = "Em-dashes should be written -- thus."
117 self.check_wrap(text, 25,
118 ["Em-dashes should be",
119 "written -- thus."])
120
121 # Probe the boundaries of the properly written em-dash,
122 # ie. " -- ".
123 self.check_wrap(text, 29,
124 ["Em-dashes should be written",
125 "-- thus."])
126 expect = ["Em-dashes should be written --",
127 "thus."]
128 self.check_wrap(text, 30, expect)
129 self.check_wrap(text, 35, expect)
130 self.check_wrap(text, 36,
131 ["Em-dashes should be written -- thus."])
Guido van Rossum327af772002-08-22 20:13:47 +0000132
Greg Ward9ad15a32002-08-22 19:47:27 +0000133 # The improperly written em-dash is handled too, because
134 # it's adjacent to non-whitespace on both sides.
135 text = "You can also do--this or even---this."
136 expect = ["You can also do",
137 "--this or even",
138 "---this."]
139 self.check_wrap(text, 15, expect)
140 self.check_wrap(text, 16, expect)
141 expect = ["You can also do--",
142 "this or even---",
143 "this."]
144 self.check_wrap(text, 17, expect)
145 self.check_wrap(text, 19, expect)
146 expect = ["You can also do--this or even",
147 "---this."]
148 self.check_wrap(text, 29, expect)
149 self.check_wrap(text, 31, expect)
150 expect = ["You can also do--this or even---",
151 "this."]
152 self.check_wrap(text, 32, expect)
153 self.check_wrap(text, 35, expect)
154
155 # All of the above behaviour could be deduced by probing the
156 # _split() method.
157 text = "Here's an -- em-dash and--here's another---and another!"
158 result = self.wrapper._split(text)
159 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
160 "and", "--", "here's", " ", "another", "---",
161 "and", " ", "another!"]
162 self.assertEquals(result, expect,
163 "\nexpected %r\n"
164 "but got %r" % (expect, result))
Greg Ward90c0b072002-08-22 18:11:10 +0000165
Greg Ward34f995b2002-08-22 21:10:07 +0000166 def test_unix_options (self):
167 # Test that Unix-style command-line options are wrapped correctly.
168 # Both Optik (OptionParser) and Docutils rely on this behaviour!
169
170 text = "You should use the -n option, or --dry-run in its long form."
171 self.check_wrap(text, 20,
172 ["You should use the",
173 "-n option, or --dry-",
174 "run in its long",
175 "form."])
176 self.check_wrap(text, 21,
177 ["You should use the -n",
178 "option, or --dry-run",
179 "in its long form."])
180 expect = ["You should use the -n option, or",
181 "--dry-run in its long form."]
182 self.check_wrap(text, 32, expect)
183 self.check_wrap(text, 34, expect)
184 self.check_wrap(text, 35, expect)
185 self.check_wrap(text, 38, expect)
186 expect = ["You should use the -n option, or --dry-",
187 "run in its long form."]
188 self.check_wrap(text, 39, expect)
189 self.check_wrap(text, 41, expect)
190 expect = ["You should use the -n option, or --dry-run",
191 "in its long form."]
192 self.check_wrap(text, 42, expect)
193
Greg Ward90c0b072002-08-22 18:11:10 +0000194 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000195 # Ensure that the standard _split() method works as advertised
196 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000197
Greg Wardee413842002-08-22 18:55:38 +0000198 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000199
Greg Wardee413842002-08-22 18:55:38 +0000200 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000201 self.check(result,
202 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
203 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
204
205
Greg Wardfd030e42002-08-22 19:02:37 +0000206class LongWordTestCase (BaseTestCase):
207 def setUp(self):
208 self.wrapper = TextWrapper()
209 self.text = '''
Greg Ward90c0b072002-08-22 18:11:10 +0000210Did you say "supercalifragilisticexpialidocious?"
211How *do* you spell that odd word, anyways?
212'''
Greg Wardfd030e42002-08-22 19:02:37 +0000213
214 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000215 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000216
217 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000218 ['Did you say "supercalifragilis',
219 'ticexpialidocious?" How *do*',
220 'you spell that odd word,',
221 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000222 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000223 ['Did you say "supercalifragilisticexpialidocious?"',
224 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000225
226
Guido van Rossum327af772002-08-22 20:13:47 +0000227 def test_nobreak_long(self):
228 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000229 self.wrapper.break_long_words = 0
230 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000231 expect = ['Did you say',
232 '"supercalifragilisticexpialidocious?"',
233 'How *do* you spell that odd',
234 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000235 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000236 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000237 self.check(result, expect)
238
239 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000240 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000241 self.check(result, expect)
242
243
244
Greg Ward13c53c62002-08-22 18:57:26 +0000245class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000246
247 # called before each test method
248 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000249 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000250This paragraph will be filled, first without any indentation,
251and then with some (including a hanging indent).'''
252
253
Greg Wardf6765782002-08-22 18:35:49 +0000254 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000255 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000256
257 expect = '''\
258This paragraph will be filled, first
259without any indentation, and then with
260some (including a hanging indent).'''
261
Greg Wardf69d3c92002-08-22 19:06:45 +0000262 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000263 self.check(result, expect)
264
265
Greg Wardf6765782002-08-22 18:35:49 +0000266 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000267 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000268
Greg Wardf69d3c92002-08-22 19:06:45 +0000269 expect = [" This paragraph will be filled,",
270 "first without any indentation, and then",
271 "with some (including a hanging indent)."]
272 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000273 self.check(result, expect)
274
Greg Wardf69d3c92002-08-22 19:06:45 +0000275 expect = "\n".join(expect)
276 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000277 self.check(result, expect)
278
279
Greg Wardf6765782002-08-22 18:35:49 +0000280 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000281 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000282
283 expect = '''\
284 * This paragraph will be filled, first
285 without any indentation, and then
286 with some (including a hanging
287 indent).'''
288
Greg Wardf69d3c92002-08-22 19:06:45 +0000289 result = fill(self.text, 40,
290 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000291 self.check(result, expect)
292
293
Greg Wardf6765782002-08-22 18:35:49 +0000294def test_main():
295 suite = unittest.TestSuite()
296 suite.addTest(unittest.makeSuite(WrapTestCase))
Greg Wardfd030e42002-08-22 19:02:37 +0000297 suite.addTest(unittest.makeSuite(LongWordTestCase))
Greg Wardf6765782002-08-22 18:35:49 +0000298 suite.addTest(unittest.makeSuite(IndentTestCases))
299 test_support.run_suite(suite)
300
Greg Ward90c0b072002-08-22 18:11:10 +0000301if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000302 test_main()