blob: 787153e3cdfbc589703dce675d79d189c4afbd5b [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
Greg Wardf6765782002-08-22 18:35:49 +0000132 def test_wrap_short(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000133 # Wrapping to make short lines longer
Greg Ward90c0b072002-08-22 18:11:10 +0000134
Greg Wardee413842002-08-22 18:55:38 +0000135 text = "This is a\nshort paragraph."
Greg Ward90c0b072002-08-22 18:11:10 +0000136
Greg Wardee413842002-08-22 18:55:38 +0000137 self.check_wrap(text, 20, ["This is a short",
138 "paragraph."])
139 self.check_wrap(text, 40, ["This is a short paragraph."])
Greg Ward90c0b072002-08-22 18:11:10 +0000140
141
Guido van Rossumeb287a22002-10-02 15:47:32 +0000142 def test_wrap_short_1line(self):
143 # Test endcases
144
145 text = "This is a short line."
146
147 self.check_wrap(text, 30, ["This is a short line."])
148 self.check_wrap(text, 30, ["(1) This is a short line."],
149 initial_indent="(1) ")
150
151
Greg Wardf6765782002-08-22 18:35:49 +0000152 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000153 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000154
Greg Wardee413842002-08-22 18:55:38 +0000155 text = ("this-is-a-useful-feature-for-"
156 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000157
Greg Wardee413842002-08-22 18:55:38 +0000158 self.check_wrap(text, 40,
159 ["this-is-a-useful-feature-for-",
160 "reformatting-posts-from-tim-peters'ly"])
161 self.check_wrap(text, 41,
162 ["this-is-a-useful-feature-for-",
163 "reformatting-posts-from-tim-peters'ly"])
164 self.check_wrap(text, 42,
165 ["this-is-a-useful-feature-for-reformatting-",
166 "posts-from-tim-peters'ly"])
Greg Ward90c0b072002-08-22 18:11:10 +0000167
Greg Ward40407942005-03-05 02:53:17 +0000168 def test_hyphenated_numbers(self):
169 # Test that hyphenated numbers (eg. dates) are not broken like words.
170 text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n"
171 "released on 1994-02-15.")
172
173 self.check_wrap(text, 30, ['Python 1.0.0 was released on',
174 '1994-01-26. Python 1.0.1 was',
175 'released on 1994-02-15.'])
176 self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
177 'Python 1.0.1 was released on 1994-02-15.'])
178
179 text = "I do all my shopping at 7-11."
180 self.check_wrap(text, 25, ["I do all my shopping at",
181 "7-11."])
182 self.check_wrap(text, 27, ["I do all my shopping at",
183 "7-11."])
184 self.check_wrap(text, 29, ["I do all my shopping at 7-11."])
185
Greg Ward9ad15a32002-08-22 19:47:27 +0000186 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000187 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000188 text = "Em-dashes should be written -- thus."
189 self.check_wrap(text, 25,
190 ["Em-dashes should be",
191 "written -- thus."])
192
193 # Probe the boundaries of the properly written em-dash,
194 # ie. " -- ".
195 self.check_wrap(text, 29,
196 ["Em-dashes should be written",
197 "-- thus."])
198 expect = ["Em-dashes should be written --",
199 "thus."]
200 self.check_wrap(text, 30, expect)
201 self.check_wrap(text, 35, expect)
202 self.check_wrap(text, 36,
203 ["Em-dashes should be written -- thus."])
Guido van Rossum327af772002-08-22 20:13:47 +0000204
Greg Ward9ad15a32002-08-22 19:47:27 +0000205 # The improperly written em-dash is handled too, because
206 # it's adjacent to non-whitespace on both sides.
207 text = "You can also do--this or even---this."
208 expect = ["You can also do",
209 "--this or even",
210 "---this."]
211 self.check_wrap(text, 15, expect)
212 self.check_wrap(text, 16, expect)
213 expect = ["You can also do--",
214 "this or even---",
215 "this."]
216 self.check_wrap(text, 17, expect)
217 self.check_wrap(text, 19, expect)
218 expect = ["You can also do--this or even",
219 "---this."]
220 self.check_wrap(text, 29, expect)
221 self.check_wrap(text, 31, expect)
222 expect = ["You can also do--this or even---",
223 "this."]
224 self.check_wrap(text, 32, expect)
225 self.check_wrap(text, 35, expect)
226
227 # All of the above behaviour could be deduced by probing the
228 # _split() method.
229 text = "Here's an -- em-dash and--here's another---and another!"
Greg Ward9ad15a32002-08-22 19:47:27 +0000230 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
231 "and", "--", "here's", " ", "another", "---",
232 "and", " ", "another!"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000233 self.check_split(text, expect)
Greg Ward90c0b072002-08-22 18:11:10 +0000234
Greg Wardc6edb372002-08-22 21:27:05 +0000235 text = "and then--bam!--he was gone"
236 expect = ["and", " ", "then", "--", "bam!", "--",
237 "he", " ", "was", " ", "gone"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000238 self.check_split(text, expect)
Greg Wardc6edb372002-08-22 21:27:05 +0000239
240
Greg Ward34f995b2002-08-22 21:10:07 +0000241 def test_unix_options (self):
242 # Test that Unix-style command-line options are wrapped correctly.
243 # Both Optik (OptionParser) and Docutils rely on this behaviour!
244
245 text = "You should use the -n option, or --dry-run in its long form."
246 self.check_wrap(text, 20,
247 ["You should use the",
248 "-n option, or --dry-",
249 "run in its long",
250 "form."])
251 self.check_wrap(text, 21,
252 ["You should use the -n",
253 "option, or --dry-run",
254 "in its long form."])
255 expect = ["You should use the -n option, or",
256 "--dry-run in its long form."]
257 self.check_wrap(text, 32, expect)
258 self.check_wrap(text, 34, expect)
259 self.check_wrap(text, 35, expect)
260 self.check_wrap(text, 38, expect)
261 expect = ["You should use the -n option, or --dry-",
262 "run in its long form."]
263 self.check_wrap(text, 39, expect)
264 self.check_wrap(text, 41, expect)
265 expect = ["You should use the -n option, or --dry-run",
266 "in its long form."]
267 self.check_wrap(text, 42, expect)
268
Greg Ward24a1c9c2002-08-22 21:12:54 +0000269 # Again, all of the above can be deduced from _split().
270 text = "the -n option, or --dry-run or --dryrun"
Greg Ward24a1c9c2002-08-22 21:12:54 +0000271 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
272 "--dry-", "run", " ", "or", " ", "--dryrun"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000273 self.check_split(text, expect)
274
275 def test_funky_hyphens (self):
276 # Screwy edge cases cooked up by David Goodger. All reported
277 # in SF bug #596434.
278 self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
279 self.check_split("what the--", ["what", " ", "the--"])
280 self.check_split("what the--.", ["what", " ", "the--."])
281 self.check_split("--text--.", ["--text--."])
282
Greg Wardcc55cb92003-05-07 01:19:22 +0000283 # When I first read bug #596434, this is what I thought David
284 # was talking about. I was wrong; these have always worked
285 # fine. The real problem is tested in test_funky_parens()
286 # below...
Greg Wardd1a72a02002-10-31 16:11:18 +0000287 self.check_split("--option", ["--option"])
288 self.check_split("--option-opt", ["--option-", "opt"])
Greg Wardcc55cb92003-05-07 01:19:22 +0000289 self.check_split("foo --option-opt bar",
290 ["foo", " ", "--option-", "opt", " ", "bar"])
291
Greg Ward61864102004-06-03 01:59:41 +0000292 def test_punct_hyphens(self):
293 # Oh bother, SF #965425 found another problem with hyphens --
294 # hyphenated words in single quotes weren't handled correctly.
295 # In fact, the bug is that *any* punctuation around a hyphenated
296 # word was handled incorrectly, except for a leading "--", which
297 # was special-cased for Optik and Docutils. So test a variety
298 # of styles of punctuation around a hyphenated word.
299 # (Actually this is based on an Optik bug report, #813077).
300 self.check_split("the 'wibble-wobble' widget",
301 ['the', ' ', "'wibble-", "wobble'", ' ', 'widget'])
302 self.check_split('the "wibble-wobble" widget',
303 ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget'])
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
Greg Wardcc55cb92003-05-07 01:19:22 +0000309 def test_funky_parens (self):
310 # Second part of SF bug #596434: long option strings inside
311 # parentheses.
312 self.check_split("foo (--option) bar",
313 ["foo", " ", "(--option)", " ", "bar"])
314
315 # Related stuff -- make sure parens work in simpler contexts.
316 self.check_split("foo (bar) baz",
317 ["foo", " ", "(bar)", " ", "baz"])
318 self.check_split("blah (ding dong), wubba",
319 ["blah", " ", "(ding", " ", "dong),",
320 " ", "wubba"])
Greg Ward24a1c9c2002-08-22 21:12:54 +0000321
Greg Ward24cbbcb2002-12-09 16:27:15 +0000322 def test_initial_whitespace(self):
323 # SF bug #622849 reported inconsistent handling of leading
324 # whitespace; let's test that a bit, shall we?
325 text = " This is a sentence with leading whitespace."
326 self.check_wrap(text, 50,
327 [" This is a sentence with leading whitespace."])
328 self.check_wrap(text, 30,
329 [" This is a sentence with", "leading whitespace."])
330
Georg Brandl9e6b4702007-03-13 18:15:41 +0000331 def test_no_drop_whitespace(self):
332 # SF patch #1581073
333 text = " This is a sentence with much whitespace."
334 self.check_wrap(text, 10,
335 [" This is a", " ", "sentence ",
336 "with ", "much white", "space."],
337 drop_whitespace=False)
338
Walter Dörwald4e41a4b2005-08-03 17:09:04 +0000339 if test_support.have_unicode:
340 def test_unicode(self):
341 # *Very* simple test of wrapping Unicode strings. I'm sure
342 # there's more to it than this, but let's at least make
343 # sure textwrap doesn't crash on Unicode input!
344 text = u"Hello there, how are you today?"
345 self.check_wrap(text, 50, [u"Hello there, how are you today?"])
346 self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
347 olines = self.wrapper.wrap(text)
348 assert isinstance(olines, list) and isinstance(olines[0], unicode)
349 otext = self.wrapper.fill(text)
350 assert isinstance(otext, unicode)
Greg Wardc7e3c5e2002-12-09 16:32:41 +0000351
Greg Ward90c0b072002-08-22 18:11:10 +0000352 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000353 # Ensure that the standard _split() method works as advertised
354 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000355
Greg Wardee413842002-08-22 18:55:38 +0000356 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000357
Greg Wardee413842002-08-22 18:55:38 +0000358 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000359 self.check(result,
360 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
361 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
362
Greg Ward49128572003-05-07 00:54:42 +0000363 def test_bad_width(self):
364 # Ensure that width <= 0 is caught.
365 text = "Whatever, it doesn't matter."
366 self.assertRaises(ValueError, wrap, text, 0)
367 self.assertRaises(ValueError, wrap, text, -1)
368
Greg Ward90c0b072002-08-22 18:11:10 +0000369
Greg Wardfd030e42002-08-22 19:02:37 +0000370class LongWordTestCase (BaseTestCase):
371 def setUp(self):
372 self.wrapper = TextWrapper()
Greg Ward24cbbcb2002-12-09 16:27:15 +0000373 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000374Did you say "supercalifragilisticexpialidocious?"
375How *do* you spell that odd word, anyways?
376'''
Greg Wardfd030e42002-08-22 19:02:37 +0000377
378 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000379 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000380
381 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000382 ['Did you say "supercalifragilis',
383 'ticexpialidocious?" How *do*',
384 'you spell that odd word,',
385 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000386 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000387 ['Did you say "supercalifragilisticexpialidocious?"',
388 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000389
Raymond Hettingerc11dbcd2003-08-30 14:43:55 +0000390 # SF bug 797650. Prevent an infinite loop by making sure that at
391 # least one character gets split off on every pass.
392 self.check_wrap('-'*10+'hello', 10,
393 ['----------',
394 ' h',
395 ' e',
396 ' l',
397 ' l',
398 ' o'],
399 subsequent_indent = ' '*15)
Greg Ward90c0b072002-08-22 18:11:10 +0000400
Georg Brandlc6fde722008-01-19 19:48:19 +0000401 # bug 1146. Prevent a long word to be wrongly wrapped when the
402 # preceding word is exactly one character shorter than the width
403 self.check_wrap(self.text, 12,
404 ['Did you say ',
405 '"supercalifr',
406 'agilisticexp',
407 'ialidocious?',
408 '" How *do*',
409 'you spell',
410 'that odd',
411 'word,',
412 'anyways?'])
413
Guido van Rossum327af772002-08-22 20:13:47 +0000414 def test_nobreak_long(self):
415 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000416 self.wrapper.break_long_words = 0
417 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000418 expect = ['Did you say',
419 '"supercalifragilisticexpialidocious?"',
420 'How *do* you spell that odd',
421 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000422 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000423 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000424 self.check(result, expect)
425
426 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000427 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000428 self.check(result, expect)
429
430
Greg Ward13c53c62002-08-22 18:57:26 +0000431class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000432
433 # called before each test method
434 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000435 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000436This paragraph will be filled, first without any indentation,
437and then with some (including a hanging indent).'''
438
439
Greg Wardf6765782002-08-22 18:35:49 +0000440 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000441 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000442
443 expect = '''\
444This paragraph will be filled, first
445without any indentation, and then with
446some (including a hanging indent).'''
447
Greg Wardf69d3c92002-08-22 19:06:45 +0000448 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000449 self.check(result, expect)
450
451
Greg Wardf6765782002-08-22 18:35:49 +0000452 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000453 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000454
Greg Wardf69d3c92002-08-22 19:06:45 +0000455 expect = [" This paragraph will be filled,",
456 "first without any indentation, and then",
457 "with some (including a hanging indent)."]
458 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000459 self.check(result, expect)
460
Greg Wardf69d3c92002-08-22 19:06:45 +0000461 expect = "\n".join(expect)
462 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000463 self.check(result, expect)
464
465
Greg Wardf6765782002-08-22 18:35:49 +0000466 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000467 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000468
469 expect = '''\
470 * This paragraph will be filled, first
471 without any indentation, and then
472 with some (including a hanging
473 indent).'''
474
Greg Wardf69d3c92002-08-22 19:06:45 +0000475 result = fill(self.text, 40,
476 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000477 self.check(result, expect)
478
479
Greg Ward9e082f42003-05-08 01:58:26 +0000480# Despite the similar names, DedentTestCase is *not* the inverse
481# of IndentTestCase!
482class DedentTestCase(unittest.TestCase):
483
Greg Ward7f547402006-06-11 00:40:49 +0000484 def assertUnchanged(self, text):
485 """assert that dedent() has no effect on 'text'"""
486 self.assertEquals(text, dedent(text))
487
Greg Ward9e082f42003-05-08 01:58:26 +0000488 def test_dedent_nomargin(self):
489 # No lines indented.
490 text = "Hello there.\nHow are you?\nOh good, I'm glad."
Greg Ward7f547402006-06-11 00:40:49 +0000491 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000492
493 # Similar, with a blank line.
494 text = "Hello there.\n\nBoo!"
Greg Ward7f547402006-06-11 00:40:49 +0000495 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000496
497 # Some lines indented, but overall margin is still zero.
498 text = "Hello there.\n This is indented."
Greg Ward7f547402006-06-11 00:40:49 +0000499 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000500
501 # Again, add a blank line.
502 text = "Hello there.\n\n Boo!\n"
Greg Ward7f547402006-06-11 00:40:49 +0000503 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000504
505 def test_dedent_even(self):
506 # All lines indented by two spaces.
507 text = " Hello there.\n How are ya?\n Oh good."
508 expect = "Hello there.\nHow are ya?\nOh good."
Greg Ward7f547402006-06-11 00:40:49 +0000509 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000510
511 # Same, with blank lines.
512 text = " Hello there.\n\n How are ya?\n Oh good.\n"
513 expect = "Hello there.\n\nHow are ya?\nOh good.\n"
Greg Ward7f547402006-06-11 00:40:49 +0000514 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000515
516 # Now indent one of the blank lines.
517 text = " Hello there.\n \n How are ya?\n Oh good.\n"
518 expect = "Hello there.\n\nHow are ya?\nOh good.\n"
Greg Ward7f547402006-06-11 00:40:49 +0000519 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000520
521 def test_dedent_uneven(self):
522 # Lines indented unevenly.
523 text = '''\
524 def foo():
525 while 1:
526 return foo
527 '''
528 expect = '''\
529def foo():
530 while 1:
531 return foo
532'''
Greg Ward7f547402006-06-11 00:40:49 +0000533 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000534
535 # Uneven indentation with a blank line.
536 text = " Foo\n Bar\n\n Baz\n"
537 expect = "Foo\n Bar\n\n Baz\n"
Greg Ward7f547402006-06-11 00:40:49 +0000538 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000539
540 # Uneven indentation with a whitespace-only line.
541 text = " Foo\n Bar\n \n Baz\n"
542 expect = "Foo\n Bar\n\n Baz\n"
Greg Ward7f547402006-06-11 00:40:49 +0000543 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000544
Greg Ward7f547402006-06-11 00:40:49 +0000545 # dedent() should not mangle internal tabs
546 def test_dedent_preserve_internal_tabs(self):
547 text = " hello\tthere\n how are\tyou?"
548 expect = "hello\tthere\nhow are\tyou?"
549 self.assertEquals(expect, dedent(text))
550
551 # make sure that it preserves tabs when it's not making any
552 # changes at all
553 self.assertEquals(expect, dedent(expect))
554
555 # dedent() should not mangle tabs in the margin (i.e.
556 # tabs and spaces both count as margin, but are *not*
557 # considered equivalent)
558 def test_dedent_preserve_margin_tabs(self):
559 text = " hello there\n\thow are you?"
560 self.assertUnchanged(text)
561
562 # same effect even if we have 8 spaces
563 text = " hello there\n\thow are you?"
564 self.assertUnchanged(text)
565
566 # dedent() only removes whitespace that can be uniformly removed!
567 text = "\thello there\n\thow are you?"
568 expect = "hello there\nhow are you?"
569 self.assertEquals(expect, dedent(text))
570
571 text = " \thello there\n \thow are you?"
572 self.assertEquals(expect, dedent(text))
573
574 text = " \t hello there\n \t how are you?"
575 self.assertEquals(expect, dedent(text))
Tim Peters4f96f1f2006-06-11 19:42:51 +0000576
Greg Ward7f547402006-06-11 00:40:49 +0000577 text = " \thello there\n \t how are you?"
578 expect = "hello there\n how are you?"
579 self.assertEquals(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000580
581
Greg Wardf6765782002-08-22 18:35:49 +0000582def test_main():
Greg Ward9e082f42003-05-08 01:58:26 +0000583 test_support.run_unittest(WrapTestCase,
584 LongWordTestCase,
585 IndentTestCases,
586 DedentTestCase)
Greg Wardf6765782002-08-22 18:35:49 +0000587
Greg Ward90c0b072002-08-22 18:11:10 +0000588if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000589 test_main()