blob: c91e242dfdb8655e26386e406e7b706efb46d52f [file] [log] [blame]
Greg Ward3dc94e12002-08-22 18:37:50 +00001#
Greg Ward61864102004-06-03 01:59:41 +00002# Test suite for the textwrap module.
Greg Ward3dc94e12002-08-22 18:37:50 +00003#
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 Ward9e082f42003-05-08 01:58:26 +000014from textwrap import TextWrapper, wrap, fill, dedent
Greg Ward90c0b072002-08-22 18:11:10 +000015
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)
Raymond Hettinger7a70ea42003-09-17 05:50:59 +000026 elif isinstance(textin, basestring):
Greg Ward90c0b072002-08-22 18:11:10 +000027 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):
Greg Wardf0ba7642004-05-13 01:53:10 +000050 self.wrapper = TextWrapper(width=45)
Greg Ward90c0b072002-08-22 18:11:10 +000051
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 Wardf0ba7642004-05-13 01:53:10 +000087 wrapper = TextWrapper(45, fix_sentence_endings=True)
88 result = wrapper.wrap(text)
Greg Ward90c0b072002-08-22 18:11:10 +000089 self.check(result, expect)
90
Greg Wardf0ba7642004-05-13 01:53:10 +000091 result = wrapper.fill(text)
Greg Ward90c0b072002-08-22 18:11:10 +000092 self.check(result, '\n'.join(expect))
93
Greg Wardf0ba7642004-05-13 01:53:10 +000094 def test_fix_sentence_endings(self):
95 wrapper = TextWrapper(60, fix_sentence_endings=True)
Greg Ward90c0b072002-08-22 18:11:10 +000096
Greg Wardf0ba7642004-05-13 01:53:10 +000097 # SF #847346: ensure that fix_sentence_endings=True does the
98 # right thing even on input short enough that it doesn't need to
99 # be wrapped.
100 text = "A short line. Note the single space."
101 expect = ["A short line. Note the single space."]
102 self.check(wrapper.wrap(text), expect)
103
104 # Test some of the hairy end cases that _fix_sentence_endings()
105 # is supposed to handle (the easy stuff is tested in
106 # test_whitespace() above).
107 text = "Well, Doctor? What do you think?"
108 expect = ["Well, Doctor? What do you think?"]
109 self.check(wrapper.wrap(text), expect)
110
111 text = "Well, Doctor?\nWhat do you think?"
112 self.check(wrapper.wrap(text), expect)
113
114 text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
115 expect = ['I say, chaps! Anyone for "tennis?" Hmmph!']
116 self.check(wrapper.wrap(text), expect)
117
118 wrapper.width = 20
119 expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
120 self.check(wrapper.wrap(text), expect)
121
122 text = 'And she said, "Go to hell!"\nCan you believe that?'
123 expect = ['And she said, "Go to',
124 'hell!" Can you',
125 'believe that?']
126 self.check(wrapper.wrap(text), expect)
127
128 wrapper.width = 60
129 expect = ['And she said, "Go to hell!" Can you believe that?']
130 self.check(wrapper.wrap(text), expect)
Tim Peters27f88362004-07-08 04:22:35 +0000131
Mark Dickinsonfe536f52008-04-25 16:59:09 +0000132 text = 'File stdio.h is nice.'
133 expect = ['File stdio.h is nice.']
134 self.check(wrapper.wrap(text), expect)
135
Greg Wardf6765782002-08-22 18:35:49 +0000136 def test_wrap_short(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000137 # Wrapping to make short lines longer
Greg Ward90c0b072002-08-22 18:11:10 +0000138
Greg Wardee413842002-08-22 18:55:38 +0000139 text = "This is a\nshort paragraph."
Greg Ward90c0b072002-08-22 18:11:10 +0000140
Greg Wardee413842002-08-22 18:55:38 +0000141 self.check_wrap(text, 20, ["This is a short",
142 "paragraph."])
143 self.check_wrap(text, 40, ["This is a short paragraph."])
Greg Ward90c0b072002-08-22 18:11:10 +0000144
145
Guido van Rossumeb287a22002-10-02 15:47:32 +0000146 def test_wrap_short_1line(self):
147 # Test endcases
148
149 text = "This is a short line."
150
151 self.check_wrap(text, 30, ["This is a short line."])
152 self.check_wrap(text, 30, ["(1) This is a short line."],
153 initial_indent="(1) ")
154
155
Greg Wardf6765782002-08-22 18:35:49 +0000156 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000157 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000158
Greg Wardee413842002-08-22 18:55:38 +0000159 text = ("this-is-a-useful-feature-for-"
160 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000161
Greg Wardee413842002-08-22 18:55:38 +0000162 self.check_wrap(text, 40,
163 ["this-is-a-useful-feature-for-",
164 "reformatting-posts-from-tim-peters'ly"])
165 self.check_wrap(text, 41,
166 ["this-is-a-useful-feature-for-",
167 "reformatting-posts-from-tim-peters'ly"])
168 self.check_wrap(text, 42,
169 ["this-is-a-useful-feature-for-reformatting-",
170 "posts-from-tim-peters'ly"])
Greg Ward90c0b072002-08-22 18:11:10 +0000171
Greg Ward40407942005-03-05 02:53:17 +0000172 def test_hyphenated_numbers(self):
173 # Test that hyphenated numbers (eg. dates) are not broken like words.
174 text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n"
175 "released on 1994-02-15.")
176
Antoine Pitrou74af3bb2008-12-13 23:12:30 +0000177 self.check_wrap(text, 35, ['Python 1.0.0 was released on',
Greg Ward40407942005-03-05 02:53:17 +0000178 '1994-01-26. Python 1.0.1 was',
179 'released on 1994-02-15.'])
180 self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
181 'Python 1.0.1 was released on 1994-02-15.'])
182
183 text = "I do all my shopping at 7-11."
184 self.check_wrap(text, 25, ["I do all my shopping at",
185 "7-11."])
186 self.check_wrap(text, 27, ["I do all my shopping at",
187 "7-11."])
188 self.check_wrap(text, 29, ["I do all my shopping at 7-11."])
189
Greg Ward9ad15a32002-08-22 19:47:27 +0000190 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000191 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000192 text = "Em-dashes should be written -- thus."
193 self.check_wrap(text, 25,
194 ["Em-dashes should be",
195 "written -- thus."])
196
197 # Probe the boundaries of the properly written em-dash,
198 # ie. " -- ".
199 self.check_wrap(text, 29,
200 ["Em-dashes should be written",
201 "-- thus."])
202 expect = ["Em-dashes should be written --",
203 "thus."]
204 self.check_wrap(text, 30, expect)
205 self.check_wrap(text, 35, expect)
206 self.check_wrap(text, 36,
207 ["Em-dashes should be written -- thus."])
Guido van Rossum327af772002-08-22 20:13:47 +0000208
Greg Ward9ad15a32002-08-22 19:47:27 +0000209 # The improperly written em-dash is handled too, because
210 # it's adjacent to non-whitespace on both sides.
211 text = "You can also do--this or even---this."
212 expect = ["You can also do",
213 "--this or even",
214 "---this."]
215 self.check_wrap(text, 15, expect)
216 self.check_wrap(text, 16, expect)
217 expect = ["You can also do--",
218 "this or even---",
219 "this."]
220 self.check_wrap(text, 17, expect)
221 self.check_wrap(text, 19, expect)
222 expect = ["You can also do--this or even",
223 "---this."]
224 self.check_wrap(text, 29, expect)
225 self.check_wrap(text, 31, expect)
226 expect = ["You can also do--this or even---",
227 "this."]
228 self.check_wrap(text, 32, expect)
229 self.check_wrap(text, 35, expect)
230
231 # All of the above behaviour could be deduced by probing the
232 # _split() method.
233 text = "Here's an -- em-dash and--here's another---and another!"
Greg Ward9ad15a32002-08-22 19:47:27 +0000234 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
235 "and", "--", "here's", " ", "another", "---",
236 "and", " ", "another!"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000237 self.check_split(text, expect)
Greg Ward90c0b072002-08-22 18:11:10 +0000238
Greg Wardc6edb372002-08-22 21:27:05 +0000239 text = "and then--bam!--he was gone"
240 expect = ["and", " ", "then", "--", "bam!", "--",
241 "he", " ", "was", " ", "gone"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000242 self.check_split(text, expect)
Greg Wardc6edb372002-08-22 21:27:05 +0000243
244
Greg Ward34f995b2002-08-22 21:10:07 +0000245 def test_unix_options (self):
246 # Test that Unix-style command-line options are wrapped correctly.
247 # Both Optik (OptionParser) and Docutils rely on this behaviour!
248
249 text = "You should use the -n option, or --dry-run in its long form."
250 self.check_wrap(text, 20,
251 ["You should use the",
252 "-n option, or --dry-",
253 "run in its long",
254 "form."])
255 self.check_wrap(text, 21,
256 ["You should use the -n",
257 "option, or --dry-run",
258 "in its long form."])
259 expect = ["You should use the -n option, or",
260 "--dry-run in its long form."]
261 self.check_wrap(text, 32, expect)
262 self.check_wrap(text, 34, expect)
263 self.check_wrap(text, 35, expect)
264 self.check_wrap(text, 38, expect)
265 expect = ["You should use the -n option, or --dry-",
266 "run in its long form."]
267 self.check_wrap(text, 39, expect)
268 self.check_wrap(text, 41, expect)
269 expect = ["You should use the -n option, or --dry-run",
270 "in its long form."]
271 self.check_wrap(text, 42, expect)
272
Greg Ward24a1c9c2002-08-22 21:12:54 +0000273 # Again, all of the above can be deduced from _split().
274 text = "the -n option, or --dry-run or --dryrun"
Greg Ward24a1c9c2002-08-22 21:12:54 +0000275 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
276 "--dry-", "run", " ", "or", " ", "--dryrun"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000277 self.check_split(text, expect)
278
279 def test_funky_hyphens (self):
280 # Screwy edge cases cooked up by David Goodger. All reported
281 # in SF bug #596434.
282 self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
283 self.check_split("what the--", ["what", " ", "the--"])
284 self.check_split("what the--.", ["what", " ", "the--."])
285 self.check_split("--text--.", ["--text--."])
286
Greg Wardcc55cb92003-05-07 01:19:22 +0000287 # When I first read bug #596434, this is what I thought David
288 # was talking about. I was wrong; these have always worked
289 # fine. The real problem is tested in test_funky_parens()
290 # below...
Greg Wardd1a72a02002-10-31 16:11:18 +0000291 self.check_split("--option", ["--option"])
292 self.check_split("--option-opt", ["--option-", "opt"])
Greg Wardcc55cb92003-05-07 01:19:22 +0000293 self.check_split("foo --option-opt bar",
294 ["foo", " ", "--option-", "opt", " ", "bar"])
295
Greg Ward61864102004-06-03 01:59:41 +0000296 def test_punct_hyphens(self):
297 # Oh bother, SF #965425 found another problem with hyphens --
298 # hyphenated words in single quotes weren't handled correctly.
299 # In fact, the bug is that *any* punctuation around a hyphenated
300 # word was handled incorrectly, except for a leading "--", which
301 # was special-cased for Optik and Docutils. So test a variety
302 # of styles of punctuation around a hyphenated word.
303 # (Actually this is based on an Optik bug report, #813077).
304 self.check_split("the 'wibble-wobble' widget",
305 ['the', ' ', "'wibble-", "wobble'", ' ', 'widget'])
306 self.check_split('the "wibble-wobble" widget',
307 ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget'])
308 self.check_split("the (wibble-wobble) widget",
309 ['the', ' ', "(wibble-", "wobble)", ' ', 'widget'])
310 self.check_split("the ['wibble-wobble'] widget",
311 ['the', ' ', "['wibble-", "wobble']", ' ', 'widget'])
312
Greg Wardcc55cb92003-05-07 01:19:22 +0000313 def test_funky_parens (self):
314 # Second part of SF bug #596434: long option strings inside
315 # parentheses.
316 self.check_split("foo (--option) bar",
317 ["foo", " ", "(--option)", " ", "bar"])
318
319 # Related stuff -- make sure parens work in simpler contexts.
320 self.check_split("foo (bar) baz",
321 ["foo", " ", "(bar)", " ", "baz"])
322 self.check_split("blah (ding dong), wubba",
323 ["blah", " ", "(ding", " ", "dong),",
324 " ", "wubba"])
Greg Ward24a1c9c2002-08-22 21:12:54 +0000325
Greg Ward24cbbcb2002-12-09 16:27:15 +0000326 def test_initial_whitespace(self):
327 # SF bug #622849 reported inconsistent handling of leading
328 # whitespace; let's test that a bit, shall we?
329 text = " This is a sentence with leading whitespace."
330 self.check_wrap(text, 50,
331 [" This is a sentence with leading whitespace."])
332 self.check_wrap(text, 30,
333 [" This is a sentence with", "leading whitespace."])
334
Georg Brandl9e6b4702007-03-13 18:15:41 +0000335 def test_no_drop_whitespace(self):
336 # SF patch #1581073
337 text = " This is a sentence with much whitespace."
338 self.check_wrap(text, 10,
339 [" This is a", " ", "sentence ",
340 "with ", "much white", "space."],
341 drop_whitespace=False)
342
Walter Dörwald4e41a4b2005-08-03 17:09:04 +0000343 if test_support.have_unicode:
344 def test_unicode(self):
345 # *Very* simple test of wrapping Unicode strings. I'm sure
346 # there's more to it than this, but let's at least make
347 # sure textwrap doesn't crash on Unicode input!
348 text = u"Hello there, how are you today?"
349 self.check_wrap(text, 50, [u"Hello there, how are you today?"])
350 self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
351 olines = self.wrapper.wrap(text)
352 assert isinstance(olines, list) and isinstance(olines[0], unicode)
353 otext = self.wrapper.fill(text)
354 assert isinstance(otext, unicode)
Greg Wardc7e3c5e2002-12-09 16:32:41 +0000355
Antoine Pitrou74af3bb2008-12-13 23:12:30 +0000356 def test_no_split_at_umlaut(self):
357 text = u"Die Empf\xe4nger-Auswahl"
358 self.check_wrap(text, 13, [u"Die", u"Empf\xe4nger-", u"Auswahl"])
359
360 def test_umlaut_followed_by_dash(self):
361 text = u"aa \xe4\xe4-\xe4\xe4"
362 self.check_wrap(text, 7, [u"aa \xe4\xe4-", u"\xe4\xe4"])
363
Greg Ward90c0b072002-08-22 18:11:10 +0000364 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000365 # Ensure that the standard _split() method works as advertised
366 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000367
Greg Wardee413842002-08-22 18:55:38 +0000368 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000369
Greg Wardee413842002-08-22 18:55:38 +0000370 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000371 self.check(result,
372 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
373 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
374
Georg Brandl6f95ae52008-05-11 10:42:28 +0000375 def test_break_on_hyphens(self):
376 # Ensure that the break_on_hyphens attributes work
377 text = "yaba daba-doo"
378 self.check_wrap(text, 10, ["yaba daba-", "doo"],
379 break_on_hyphens=True)
380 self.check_wrap(text, 10, ["yaba", "daba-doo"],
381 break_on_hyphens=False)
382
Greg Ward49128572003-05-07 00:54:42 +0000383 def test_bad_width(self):
384 # Ensure that width <= 0 is caught.
385 text = "Whatever, it doesn't matter."
386 self.assertRaises(ValueError, wrap, text, 0)
387 self.assertRaises(ValueError, wrap, text, -1)
388
Greg Ward90c0b072002-08-22 18:11:10 +0000389
Greg Wardfd030e42002-08-22 19:02:37 +0000390class LongWordTestCase (BaseTestCase):
391 def setUp(self):
392 self.wrapper = TextWrapper()
Greg Ward24cbbcb2002-12-09 16:27:15 +0000393 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000394Did you say "supercalifragilisticexpialidocious?"
395How *do* you spell that odd word, anyways?
396'''
Greg Wardfd030e42002-08-22 19:02:37 +0000397
398 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000399 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000400
401 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000402 ['Did you say "supercalifragilis',
403 'ticexpialidocious?" How *do*',
404 'you spell that odd word,',
405 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000406 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000407 ['Did you say "supercalifragilisticexpialidocious?"',
408 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000409
Raymond Hettingerc11dbcd2003-08-30 14:43:55 +0000410 # SF bug 797650. Prevent an infinite loop by making sure that at
411 # least one character gets split off on every pass.
412 self.check_wrap('-'*10+'hello', 10,
413 ['----------',
414 ' h',
415 ' e',
416 ' l',
417 ' l',
418 ' o'],
419 subsequent_indent = ' '*15)
Greg Ward90c0b072002-08-22 18:11:10 +0000420
Georg Brandlc6fde722008-01-19 19:48:19 +0000421 # bug 1146. Prevent a long word to be wrongly wrapped when the
422 # preceding word is exactly one character shorter than the width
423 self.check_wrap(self.text, 12,
424 ['Did you say ',
425 '"supercalifr',
426 'agilisticexp',
427 'ialidocious?',
428 '" How *do*',
429 'you spell',
430 'that odd',
431 'word,',
432 'anyways?'])
433
Guido van Rossum327af772002-08-22 20:13:47 +0000434 def test_nobreak_long(self):
435 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000436 self.wrapper.break_long_words = 0
437 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000438 expect = ['Did you say',
439 '"supercalifragilisticexpialidocious?"',
440 'How *do* you spell that odd',
441 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000442 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000443 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000444 self.check(result, expect)
445
446 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000447 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000448 self.check(result, expect)
449
450
Greg Ward13c53c62002-08-22 18:57:26 +0000451class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000452
453 # called before each test method
454 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000455 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000456This paragraph will be filled, first without any indentation,
457and then with some (including a hanging indent).'''
458
459
Greg Wardf6765782002-08-22 18:35:49 +0000460 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000461 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000462
463 expect = '''\
464This paragraph will be filled, first
465without any indentation, and then with
466some (including a hanging indent).'''
467
Greg Wardf69d3c92002-08-22 19:06:45 +0000468 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000469 self.check(result, expect)
470
471
Greg Wardf6765782002-08-22 18:35:49 +0000472 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000473 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000474
Greg Wardf69d3c92002-08-22 19:06:45 +0000475 expect = [" This paragraph will be filled,",
476 "first without any indentation, and then",
477 "with some (including a hanging indent)."]
478 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000479 self.check(result, expect)
480
Greg Wardf69d3c92002-08-22 19:06:45 +0000481 expect = "\n".join(expect)
482 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000483 self.check(result, expect)
484
485
Greg Wardf6765782002-08-22 18:35:49 +0000486 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000487 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000488
489 expect = '''\
490 * This paragraph will be filled, first
491 without any indentation, and then
492 with some (including a hanging
493 indent).'''
494
Greg Wardf69d3c92002-08-22 19:06:45 +0000495 result = fill(self.text, 40,
496 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000497 self.check(result, expect)
498
499
Greg Ward9e082f42003-05-08 01:58:26 +0000500# Despite the similar names, DedentTestCase is *not* the inverse
501# of IndentTestCase!
502class DedentTestCase(unittest.TestCase):
503
Greg Ward7f547402006-06-11 00:40:49 +0000504 def assertUnchanged(self, text):
505 """assert that dedent() has no effect on 'text'"""
506 self.assertEquals(text, dedent(text))
507
Greg Ward9e082f42003-05-08 01:58:26 +0000508 def test_dedent_nomargin(self):
509 # No lines indented.
510 text = "Hello there.\nHow are you?\nOh good, I'm glad."
Greg Ward7f547402006-06-11 00:40:49 +0000511 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000512
513 # Similar, with a blank line.
514 text = "Hello there.\n\nBoo!"
Greg Ward7f547402006-06-11 00:40:49 +0000515 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000516
517 # Some lines indented, but overall margin is still zero.
518 text = "Hello there.\n This is indented."
Greg Ward7f547402006-06-11 00:40:49 +0000519 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000520
521 # Again, add a blank line.
522 text = "Hello there.\n\n Boo!\n"
Greg Ward7f547402006-06-11 00:40:49 +0000523 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000524
525 def test_dedent_even(self):
526 # All lines indented by two spaces.
527 text = " Hello there.\n How are ya?\n Oh good."
528 expect = "Hello there.\nHow are ya?\nOh good."
Greg Ward7f547402006-06-11 00:40:49 +0000529 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000530
531 # Same, with blank lines.
532 text = " Hello there.\n\n How are ya?\n Oh good.\n"
533 expect = "Hello there.\n\nHow are ya?\nOh good.\n"
Greg Ward7f547402006-06-11 00:40:49 +0000534 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000535
536 # Now indent one of the blank lines.
537 text = " Hello there.\n \n How are ya?\n Oh good.\n"
538 expect = "Hello there.\n\nHow are ya?\nOh good.\n"
Greg Ward7f547402006-06-11 00:40:49 +0000539 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000540
541 def test_dedent_uneven(self):
542 # Lines indented unevenly.
543 text = '''\
544 def foo():
545 while 1:
546 return foo
547 '''
548 expect = '''\
549def foo():
550 while 1:
551 return foo
552'''
Greg Ward7f547402006-06-11 00:40:49 +0000553 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000554
555 # Uneven indentation with a blank line.
556 text = " Foo\n Bar\n\n Baz\n"
557 expect = "Foo\n Bar\n\n Baz\n"
Greg Ward7f547402006-06-11 00:40:49 +0000558 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000559
560 # Uneven indentation with a whitespace-only line.
561 text = " Foo\n Bar\n \n Baz\n"
562 expect = "Foo\n Bar\n\n Baz\n"
Greg Ward7f547402006-06-11 00:40:49 +0000563 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000564
Greg Ward7f547402006-06-11 00:40:49 +0000565 # dedent() should not mangle internal tabs
566 def test_dedent_preserve_internal_tabs(self):
567 text = " hello\tthere\n how are\tyou?"
568 expect = "hello\tthere\nhow are\tyou?"
569 self.assertEquals(expect, dedent(text))
570
571 # make sure that it preserves tabs when it's not making any
572 # changes at all
573 self.assertEquals(expect, dedent(expect))
574
575 # dedent() should not mangle tabs in the margin (i.e.
576 # tabs and spaces both count as margin, but are *not*
577 # considered equivalent)
578 def test_dedent_preserve_margin_tabs(self):
579 text = " hello there\n\thow are you?"
580 self.assertUnchanged(text)
581
582 # same effect even if we have 8 spaces
583 text = " hello there\n\thow are you?"
584 self.assertUnchanged(text)
585
586 # dedent() only removes whitespace that can be uniformly removed!
587 text = "\thello there\n\thow are you?"
588 expect = "hello there\nhow are you?"
589 self.assertEquals(expect, dedent(text))
590
591 text = " \thello there\n \thow are you?"
592 self.assertEquals(expect, dedent(text))
593
594 text = " \t hello there\n \t how are you?"
595 self.assertEquals(expect, dedent(text))
Tim Peters4f96f1f2006-06-11 19:42:51 +0000596
Greg Ward7f547402006-06-11 00:40:49 +0000597 text = " \thello there\n \t how are you?"
598 expect = "hello there\n how are you?"
599 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000600
601
Greg Wardf6765782002-08-22 18:35:49 +0000602def test_main():
Greg Ward9e082f42003-05-08 01:58:26 +0000603 test_support.run_unittest(WrapTestCase,
604 LongWordTestCase,
605 IndentTestCases,
606 DedentTestCase)
Greg Wardf6765782002-08-22 18:35:49 +0000607
Greg Ward90c0b072002-08-22 18:11:10 +0000608if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000609 test_main()