blob: 0f7f567bd3c75e71875d283fd0e53a69c8d1d584 [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
Guido van Rossumeb287a22002-10-02 15:47:32 +000036 def check_wrap(self, text, width, expect, **kwargs):
37 result = wrap(text, width, **kwargs)
Greg Wardee413842002-08-22 18:55:38 +000038 self.check(result, expect)
39
Greg Wardd1a72a02002-10-31 16:11:18 +000040 def check_split(self, text, expect):
41 result = self.wrapper._split(text)
Greg Ward715debd2002-08-22 21:16:25 +000042 self.assertEquals(result, expect,
43 "\nexpected %r\n"
44 "but got %r" % (expect, result))
45
Greg Ward90c0b072002-08-22 18:11:10 +000046
Greg Ward13c53c62002-08-22 18:57:26 +000047class WrapTestCase(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +000048
Greg Ward90c0b072002-08-22 18:11:10 +000049 def setUp(self):
50 self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
51
Greg Wardf6765782002-08-22 18:35:49 +000052 def test_simple(self):
Guido van Rossum327af772002-08-22 20:13:47 +000053 # Simple case: just words, spaces, and a bit of punctuation
Greg Ward90c0b072002-08-22 18:11:10 +000054
Greg Wardee413842002-08-22 18:55:38 +000055 text = "Hello there, how are you this fine day? I'm glad to hear it!"
Greg Ward90c0b072002-08-22 18:11:10 +000056
Greg Wardee413842002-08-22 18:55:38 +000057 self.check_wrap(text, 12,
58 ["Hello there,",
59 "how are you",
60 "this fine",
61 "day? I'm",
62 "glad to hear",
63 "it!"])
64 self.check_wrap(text, 42,
65 ["Hello there, how are you this fine day?",
66 "I'm glad to hear it!"])
67 self.check_wrap(text, 80, [text])
Greg Ward90c0b072002-08-22 18:11:10 +000068
69
Greg Wardf6765782002-08-22 18:35:49 +000070 def test_whitespace(self):
Guido van Rossum327af772002-08-22 20:13:47 +000071 # Whitespace munging and end-of-sentence detection
Greg Ward90c0b072002-08-22 18:11:10 +000072
Greg Wardee413842002-08-22 18:55:38 +000073 text = """\
Greg Ward90c0b072002-08-22 18:11:10 +000074This 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 Wardee413842002-08-22 18:55:38 +000081 expect = ["This is a paragraph that already has line",
82 "breaks. But some of its lines are much",
83 "longer than the others, so it needs to be",
84 "wrapped. Some lines are tabbed too. What a",
85 "mess!"]
Greg Ward90c0b072002-08-22 18:11:10 +000086
Greg Wardee413842002-08-22 18:55:38 +000087 result = self.wrapper.wrap(text)
Greg Ward90c0b072002-08-22 18:11:10 +000088 self.check(result, expect)
89
Greg Wardee413842002-08-22 18:55:38 +000090 result = self.wrapper.fill(text)
Greg Ward90c0b072002-08-22 18:11:10 +000091 self.check(result, '\n'.join(expect))
92
93
Greg Wardf6765782002-08-22 18:35:49 +000094 def test_wrap_short(self):
Guido van Rossum327af772002-08-22 20:13:47 +000095 # Wrapping to make short lines longer
Greg Ward90c0b072002-08-22 18:11:10 +000096
Greg Wardee413842002-08-22 18:55:38 +000097 text = "This is a\nshort paragraph."
Greg Ward90c0b072002-08-22 18:11:10 +000098
Greg Wardee413842002-08-22 18:55:38 +000099 self.check_wrap(text, 20, ["This is a short",
100 "paragraph."])
101 self.check_wrap(text, 40, ["This is a short paragraph."])
Greg Ward90c0b072002-08-22 18:11:10 +0000102
103
Guido van Rossumeb287a22002-10-02 15:47:32 +0000104 def test_wrap_short_1line(self):
105 # Test endcases
106
107 text = "This is a short line."
108
109 self.check_wrap(text, 30, ["This is a short line."])
110 self.check_wrap(text, 30, ["(1) This is a short line."],
111 initial_indent="(1) ")
112
113
Greg Wardf6765782002-08-22 18:35:49 +0000114 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000115 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000116
Greg Wardee413842002-08-22 18:55:38 +0000117 text = ("this-is-a-useful-feature-for-"
118 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000119
Greg Wardee413842002-08-22 18:55:38 +0000120 self.check_wrap(text, 40,
121 ["this-is-a-useful-feature-for-",
122 "reformatting-posts-from-tim-peters'ly"])
123 self.check_wrap(text, 41,
124 ["this-is-a-useful-feature-for-",
125 "reformatting-posts-from-tim-peters'ly"])
126 self.check_wrap(text, 42,
127 ["this-is-a-useful-feature-for-reformatting-",
128 "posts-from-tim-peters'ly"])
Greg Ward90c0b072002-08-22 18:11:10 +0000129
Greg Ward9ad15a32002-08-22 19:47:27 +0000130 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000131 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000132 text = "Em-dashes should be written -- thus."
133 self.check_wrap(text, 25,
134 ["Em-dashes should be",
135 "written -- thus."])
136
137 # Probe the boundaries of the properly written em-dash,
138 # ie. " -- ".
139 self.check_wrap(text, 29,
140 ["Em-dashes should be written",
141 "-- thus."])
142 expect = ["Em-dashes should be written --",
143 "thus."]
144 self.check_wrap(text, 30, expect)
145 self.check_wrap(text, 35, expect)
146 self.check_wrap(text, 36,
147 ["Em-dashes should be written -- thus."])
Guido van Rossum327af772002-08-22 20:13:47 +0000148
Greg Ward9ad15a32002-08-22 19:47:27 +0000149 # The improperly written em-dash is handled too, because
150 # it's adjacent to non-whitespace on both sides.
151 text = "You can also do--this or even---this."
152 expect = ["You can also do",
153 "--this or even",
154 "---this."]
155 self.check_wrap(text, 15, expect)
156 self.check_wrap(text, 16, expect)
157 expect = ["You can also do--",
158 "this or even---",
159 "this."]
160 self.check_wrap(text, 17, expect)
161 self.check_wrap(text, 19, expect)
162 expect = ["You can also do--this or even",
163 "---this."]
164 self.check_wrap(text, 29, expect)
165 self.check_wrap(text, 31, expect)
166 expect = ["You can also do--this or even---",
167 "this."]
168 self.check_wrap(text, 32, expect)
169 self.check_wrap(text, 35, expect)
170
171 # All of the above behaviour could be deduced by probing the
172 # _split() method.
173 text = "Here's an -- em-dash and--here's another---and another!"
Greg Ward9ad15a32002-08-22 19:47:27 +0000174 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
175 "and", "--", "here's", " ", "another", "---",
176 "and", " ", "another!"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000177 self.check_split(text, expect)
Greg Ward90c0b072002-08-22 18:11:10 +0000178
Greg Wardc6edb372002-08-22 21:27:05 +0000179 text = "and then--bam!--he was gone"
180 expect = ["and", " ", "then", "--", "bam!", "--",
181 "he", " ", "was", " ", "gone"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000182 self.check_split(text, expect)
Greg Wardc6edb372002-08-22 21:27:05 +0000183
184
Greg Ward34f995b2002-08-22 21:10:07 +0000185 def test_unix_options (self):
186 # Test that Unix-style command-line options are wrapped correctly.
187 # Both Optik (OptionParser) and Docutils rely on this behaviour!
188
189 text = "You should use the -n option, or --dry-run in its long form."
190 self.check_wrap(text, 20,
191 ["You should use the",
192 "-n option, or --dry-",
193 "run in its long",
194 "form."])
195 self.check_wrap(text, 21,
196 ["You should use the -n",
197 "option, or --dry-run",
198 "in its long form."])
199 expect = ["You should use the -n option, or",
200 "--dry-run in its long form."]
201 self.check_wrap(text, 32, expect)
202 self.check_wrap(text, 34, expect)
203 self.check_wrap(text, 35, expect)
204 self.check_wrap(text, 38, expect)
205 expect = ["You should use the -n option, or --dry-",
206 "run in its long form."]
207 self.check_wrap(text, 39, expect)
208 self.check_wrap(text, 41, expect)
209 expect = ["You should use the -n option, or --dry-run",
210 "in its long form."]
211 self.check_wrap(text, 42, expect)
212
Greg Ward24a1c9c2002-08-22 21:12:54 +0000213 # Again, all of the above can be deduced from _split().
214 text = "the -n option, or --dry-run or --dryrun"
Greg Ward24a1c9c2002-08-22 21:12:54 +0000215 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
216 "--dry-", "run", " ", "or", " ", "--dryrun"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000217 self.check_split(text, expect)
218
219 def test_funky_hyphens (self):
220 # Screwy edge cases cooked up by David Goodger. All reported
221 # in SF bug #596434.
222 self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
223 self.check_split("what the--", ["what", " ", "the--"])
224 self.check_split("what the--.", ["what", " ", "the--."])
225 self.check_split("--text--.", ["--text--."])
226
Greg Ward24cbbcb2002-12-09 16:27:15 +0000227 # My initial mis-interpretation of part of the bug report --
228 # These were always handled correctly, but it can't hurt to make
229 # sure that they *stay* correct!
Greg Wardd1a72a02002-10-31 16:11:18 +0000230 self.check_split("--option", ["--option"])
231 self.check_split("--option-opt", ["--option-", "opt"])
Greg Ward24a1c9c2002-08-22 21:12:54 +0000232
Greg Ward24cbbcb2002-12-09 16:27:15 +0000233 def test_initial_whitespace(self):
234 # SF bug #622849 reported inconsistent handling of leading
235 # whitespace; let's test that a bit, shall we?
236 text = " This is a sentence with leading whitespace."
237 self.check_wrap(text, 50,
238 [" This is a sentence with leading whitespace."])
239 self.check_wrap(text, 30,
240 [" This is a sentence with", "leading whitespace."])
241
Greg Wardc7e3c5e2002-12-09 16:32:41 +0000242 def test_unicode(self):
243 # *Very* simple test of wrapping Unicode strings. I'm sure
244 # there's more to it than this, but let's at least make
245 # sure textwrap doesn't crash on Unicode input!
246 text = u"Hello there, how are you today?"
247 self.check_wrap(text, 50, [u"Hello there, how are you today?"])
248 self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
249 olines = self.wrapper.wrap(text)
250 assert isinstance(olines, list) and isinstance(olines[0], unicode)
251 otext = self.wrapper.fill(text)
252 assert isinstance(otext, unicode)
253
Greg Ward90c0b072002-08-22 18:11:10 +0000254 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000255 # Ensure that the standard _split() method works as advertised
256 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000257
Greg Wardee413842002-08-22 18:55:38 +0000258 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000259
Greg Wardee413842002-08-22 18:55:38 +0000260 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000261 self.check(result,
262 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
263 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
264
Greg Ward49128572003-05-07 00:54:42 +0000265 def test_bad_width(self):
266 # Ensure that width <= 0 is caught.
267 text = "Whatever, it doesn't matter."
268 self.assertRaises(ValueError, wrap, text, 0)
269 self.assertRaises(ValueError, wrap, text, -1)
270
Greg Ward90c0b072002-08-22 18:11:10 +0000271
Greg Wardfd030e42002-08-22 19:02:37 +0000272class LongWordTestCase (BaseTestCase):
273 def setUp(self):
274 self.wrapper = TextWrapper()
Greg Ward24cbbcb2002-12-09 16:27:15 +0000275 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000276Did you say "supercalifragilisticexpialidocious?"
277How *do* you spell that odd word, anyways?
278'''
Greg Wardfd030e42002-08-22 19:02:37 +0000279
280 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000281 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000282
283 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000284 ['Did you say "supercalifragilis',
285 'ticexpialidocious?" How *do*',
286 'you spell that odd word,',
287 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000288 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000289 ['Did you say "supercalifragilisticexpialidocious?"',
290 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000291
292
Guido van Rossum327af772002-08-22 20:13:47 +0000293 def test_nobreak_long(self):
294 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000295 self.wrapper.break_long_words = 0
296 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000297 expect = ['Did you say',
298 '"supercalifragilisticexpialidocious?"',
299 'How *do* you spell that odd',
300 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000301 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000302 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000303 self.check(result, expect)
304
305 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000306 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000307 self.check(result, expect)
308
309
310
Greg Ward13c53c62002-08-22 18:57:26 +0000311class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000312
313 # called before each test method
314 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000315 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000316This paragraph will be filled, first without any indentation,
317and then with some (including a hanging indent).'''
318
319
Greg Wardf6765782002-08-22 18:35:49 +0000320 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000321 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000322
323 expect = '''\
324This paragraph will be filled, first
325without any indentation, and then with
326some (including a hanging indent).'''
327
Greg Wardf69d3c92002-08-22 19:06:45 +0000328 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000329 self.check(result, expect)
330
331
Greg Wardf6765782002-08-22 18:35:49 +0000332 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000333 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000334
Greg Wardf69d3c92002-08-22 19:06:45 +0000335 expect = [" This paragraph will be filled,",
336 "first without any indentation, and then",
337 "with some (including a hanging indent)."]
338 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000339 self.check(result, expect)
340
Greg Wardf69d3c92002-08-22 19:06:45 +0000341 expect = "\n".join(expect)
342 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000343 self.check(result, expect)
344
345
Greg Wardf6765782002-08-22 18:35:49 +0000346 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000347 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000348
349 expect = '''\
350 * This paragraph will be filled, first
351 without any indentation, and then
352 with some (including a hanging
353 indent).'''
354
Greg Wardf69d3c92002-08-22 19:06:45 +0000355 result = fill(self.text, 40,
356 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000357 self.check(result, expect)
358
359
Greg Wardf6765782002-08-22 18:35:49 +0000360def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000361 test_support.run_unittest(WrapTestCase, LongWordTestCase, IndentTestCases)
Greg Wardf6765782002-08-22 18:35:49 +0000362
Greg Ward90c0b072002-08-22 18:11:10 +0000363if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000364 test_main()