blob: bbd0882bda66259f2548d7801a40a9ff5d2a72f1 [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +000012from test import 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)
Guido van Rossum3172c5d2007-10-16 18:12:55 +000026 elif isinstance(textin, str):
Greg Ward90c0b072002-08-22 18:11:10 +000027 result = " %s\n" % repr(textin)
28 return result
29
30
31 def check(self, result, expect):
Ezio Melottib3aedd42010-11-20 19:04:17 +000032 self.assertEqual(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)
Ezio Melottib3aedd42010-11-20 19:04:17 +000042 self.assertEqual(result, expect,
43 "\nexpected %r\n"
44 "but got %r" % (expect, result))
Greg Ward715debd2002-08-22 21:16:25 +000045
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
Hynek Schlawackd5272592012-05-19 13:33:11 +020094 text = "\tTest\tdefault\t\ttabsize."
95 expect = [" Test default tabsize."]
96 self.check_wrap(text, 80, expect)
97
98 text = "\tTest\tcustom\t\ttabsize."
99 expect = [" Test custom tabsize."]
100 self.check_wrap(text, 80, expect, tabsize=4)
101
Greg Wardf0ba7642004-05-13 01:53:10 +0000102 def test_fix_sentence_endings(self):
103 wrapper = TextWrapper(60, fix_sentence_endings=True)
Greg Ward90c0b072002-08-22 18:11:10 +0000104
Greg Wardf0ba7642004-05-13 01:53:10 +0000105 # SF #847346: ensure that fix_sentence_endings=True does the
106 # right thing even on input short enough that it doesn't need to
107 # be wrapped.
108 text = "A short line. Note the single space."
109 expect = ["A short line. Note the single space."]
110 self.check(wrapper.wrap(text), expect)
111
112 # Test some of the hairy end cases that _fix_sentence_endings()
113 # is supposed to handle (the easy stuff is tested in
114 # test_whitespace() above).
115 text = "Well, Doctor? What do you think?"
116 expect = ["Well, Doctor? What do you think?"]
117 self.check(wrapper.wrap(text), expect)
118
119 text = "Well, Doctor?\nWhat do you think?"
120 self.check(wrapper.wrap(text), expect)
121
122 text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
123 expect = ['I say, chaps! Anyone for "tennis?" Hmmph!']
124 self.check(wrapper.wrap(text), expect)
125
126 wrapper.width = 20
127 expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
128 self.check(wrapper.wrap(text), expect)
129
130 text = 'And she said, "Go to hell!"\nCan you believe that?'
131 expect = ['And she said, "Go to',
132 'hell!" Can you',
133 'believe that?']
134 self.check(wrapper.wrap(text), expect)
135
136 wrapper.width = 60
137 expect = ['And she said, "Go to hell!" Can you believe that?']
138 self.check(wrapper.wrap(text), expect)
Tim Peters27f88362004-07-08 04:22:35 +0000139
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000140 text = 'File stdio.h is nice.'
141 expect = ['File stdio.h is nice.']
142 self.check(wrapper.wrap(text), expect)
143
Greg Wardf6765782002-08-22 18:35:49 +0000144 def test_wrap_short(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000145 # Wrapping to make short lines longer
Greg Ward90c0b072002-08-22 18:11:10 +0000146
Greg Wardee413842002-08-22 18:55:38 +0000147 text = "This is a\nshort paragraph."
Greg Ward90c0b072002-08-22 18:11:10 +0000148
Greg Wardee413842002-08-22 18:55:38 +0000149 self.check_wrap(text, 20, ["This is a short",
150 "paragraph."])
151 self.check_wrap(text, 40, ["This is a short paragraph."])
Greg Ward90c0b072002-08-22 18:11:10 +0000152
153
Guido van Rossumeb287a22002-10-02 15:47:32 +0000154 def test_wrap_short_1line(self):
155 # Test endcases
156
157 text = "This is a short line."
158
159 self.check_wrap(text, 30, ["This is a short line."])
160 self.check_wrap(text, 30, ["(1) This is a short line."],
161 initial_indent="(1) ")
162
163
Greg Wardf6765782002-08-22 18:35:49 +0000164 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000165 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000166
Greg Wardee413842002-08-22 18:55:38 +0000167 text = ("this-is-a-useful-feature-for-"
168 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000169
Greg Wardee413842002-08-22 18:55:38 +0000170 self.check_wrap(text, 40,
171 ["this-is-a-useful-feature-for-",
172 "reformatting-posts-from-tim-peters'ly"])
173 self.check_wrap(text, 41,
174 ["this-is-a-useful-feature-for-",
175 "reformatting-posts-from-tim-peters'ly"])
176 self.check_wrap(text, 42,
177 ["this-is-a-useful-feature-for-reformatting-",
178 "posts-from-tim-peters'ly"])
Greg Ward90c0b072002-08-22 18:11:10 +0000179
Greg Ward40407942005-03-05 02:53:17 +0000180 def test_hyphenated_numbers(self):
181 # Test that hyphenated numbers (eg. dates) are not broken like words.
182 text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n"
183 "released on 1994-02-15.")
184
185 self.check_wrap(text, 30, ['Python 1.0.0 was released on',
186 '1994-01-26. Python 1.0.1 was',
187 'released on 1994-02-15.'])
188 self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
189 'Python 1.0.1 was released on 1994-02-15.'])
190
191 text = "I do all my shopping at 7-11."
192 self.check_wrap(text, 25, ["I do all my shopping at",
193 "7-11."])
194 self.check_wrap(text, 27, ["I do all my shopping at",
195 "7-11."])
196 self.check_wrap(text, 29, ["I do all my shopping at 7-11."])
197
Greg Ward9ad15a32002-08-22 19:47:27 +0000198 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000199 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000200 text = "Em-dashes should be written -- thus."
201 self.check_wrap(text, 25,
202 ["Em-dashes should be",
203 "written -- thus."])
204
205 # Probe the boundaries of the properly written em-dash,
206 # ie. " -- ".
207 self.check_wrap(text, 29,
208 ["Em-dashes should be written",
209 "-- thus."])
210 expect = ["Em-dashes should be written --",
211 "thus."]
212 self.check_wrap(text, 30, expect)
213 self.check_wrap(text, 35, expect)
214 self.check_wrap(text, 36,
215 ["Em-dashes should be written -- thus."])
Guido van Rossum327af772002-08-22 20:13:47 +0000216
Greg Ward9ad15a32002-08-22 19:47:27 +0000217 # The improperly written em-dash is handled too, because
218 # it's adjacent to non-whitespace on both sides.
219 text = "You can also do--this or even---this."
220 expect = ["You can also do",
221 "--this or even",
222 "---this."]
223 self.check_wrap(text, 15, expect)
224 self.check_wrap(text, 16, expect)
225 expect = ["You can also do--",
226 "this or even---",
227 "this."]
228 self.check_wrap(text, 17, expect)
229 self.check_wrap(text, 19, expect)
230 expect = ["You can also do--this or even",
231 "---this."]
232 self.check_wrap(text, 29, expect)
233 self.check_wrap(text, 31, expect)
234 expect = ["You can also do--this or even---",
235 "this."]
236 self.check_wrap(text, 32, expect)
237 self.check_wrap(text, 35, expect)
238
239 # All of the above behaviour could be deduced by probing the
240 # _split() method.
241 text = "Here's an -- em-dash and--here's another---and another!"
Greg Ward9ad15a32002-08-22 19:47:27 +0000242 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
243 "and", "--", "here's", " ", "another", "---",
244 "and", " ", "another!"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000245 self.check_split(text, expect)
Greg Ward90c0b072002-08-22 18:11:10 +0000246
Greg Wardc6edb372002-08-22 21:27:05 +0000247 text = "and then--bam!--he was gone"
248 expect = ["and", " ", "then", "--", "bam!", "--",
249 "he", " ", "was", " ", "gone"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000250 self.check_split(text, expect)
Greg Wardc6edb372002-08-22 21:27:05 +0000251
252
Greg Ward34f995b2002-08-22 21:10:07 +0000253 def test_unix_options (self):
254 # Test that Unix-style command-line options are wrapped correctly.
255 # Both Optik (OptionParser) and Docutils rely on this behaviour!
256
257 text = "You should use the -n option, or --dry-run in its long form."
258 self.check_wrap(text, 20,
259 ["You should use the",
260 "-n option, or --dry-",
261 "run in its long",
262 "form."])
263 self.check_wrap(text, 21,
264 ["You should use the -n",
265 "option, or --dry-run",
266 "in its long form."])
267 expect = ["You should use the -n option, or",
268 "--dry-run in its long form."]
269 self.check_wrap(text, 32, expect)
270 self.check_wrap(text, 34, expect)
271 self.check_wrap(text, 35, expect)
272 self.check_wrap(text, 38, expect)
273 expect = ["You should use the -n option, or --dry-",
274 "run in its long form."]
275 self.check_wrap(text, 39, expect)
276 self.check_wrap(text, 41, expect)
277 expect = ["You should use the -n option, or --dry-run",
278 "in its long form."]
279 self.check_wrap(text, 42, expect)
280
Greg Ward24a1c9c2002-08-22 21:12:54 +0000281 # Again, all of the above can be deduced from _split().
282 text = "the -n option, or --dry-run or --dryrun"
Greg Ward24a1c9c2002-08-22 21:12:54 +0000283 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
284 "--dry-", "run", " ", "or", " ", "--dryrun"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000285 self.check_split(text, expect)
286
287 def test_funky_hyphens (self):
288 # Screwy edge cases cooked up by David Goodger. All reported
289 # in SF bug #596434.
290 self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
291 self.check_split("what the--", ["what", " ", "the--"])
292 self.check_split("what the--.", ["what", " ", "the--."])
293 self.check_split("--text--.", ["--text--."])
294
Greg Wardcc55cb92003-05-07 01:19:22 +0000295 # When I first read bug #596434, this is what I thought David
296 # was talking about. I was wrong; these have always worked
297 # fine. The real problem is tested in test_funky_parens()
298 # below...
Greg Wardd1a72a02002-10-31 16:11:18 +0000299 self.check_split("--option", ["--option"])
300 self.check_split("--option-opt", ["--option-", "opt"])
Greg Wardcc55cb92003-05-07 01:19:22 +0000301 self.check_split("foo --option-opt bar",
302 ["foo", " ", "--option-", "opt", " ", "bar"])
303
Greg Ward61864102004-06-03 01:59:41 +0000304 def test_punct_hyphens(self):
305 # Oh bother, SF #965425 found another problem with hyphens --
306 # hyphenated words in single quotes weren't handled correctly.
307 # In fact, the bug is that *any* punctuation around a hyphenated
308 # word was handled incorrectly, except for a leading "--", which
309 # was special-cased for Optik and Docutils. So test a variety
310 # of styles of punctuation around a hyphenated word.
311 # (Actually this is based on an Optik bug report, #813077).
312 self.check_split("the 'wibble-wobble' widget",
313 ['the', ' ', "'wibble-", "wobble'", ' ', 'widget'])
314 self.check_split('the "wibble-wobble" widget',
315 ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget'])
316 self.check_split("the (wibble-wobble) widget",
317 ['the', ' ', "(wibble-", "wobble)", ' ', 'widget'])
318 self.check_split("the ['wibble-wobble'] widget",
319 ['the', ' ', "['wibble-", "wobble']", ' ', 'widget'])
320
Greg Wardcc55cb92003-05-07 01:19:22 +0000321 def test_funky_parens (self):
322 # Second part of SF bug #596434: long option strings inside
323 # parentheses.
324 self.check_split("foo (--option) bar",
325 ["foo", " ", "(--option)", " ", "bar"])
326
327 # Related stuff -- make sure parens work in simpler contexts.
328 self.check_split("foo (bar) baz",
329 ["foo", " ", "(bar)", " ", "baz"])
330 self.check_split("blah (ding dong), wubba",
331 ["blah", " ", "(ding", " ", "dong),",
332 " ", "wubba"])
Greg Ward24a1c9c2002-08-22 21:12:54 +0000333
Greg Ward24cbbcb2002-12-09 16:27:15 +0000334 def test_initial_whitespace(self):
335 # SF bug #622849 reported inconsistent handling of leading
336 # whitespace; let's test that a bit, shall we?
337 text = " This is a sentence with leading whitespace."
338 self.check_wrap(text, 50,
339 [" This is a sentence with leading whitespace."])
340 self.check_wrap(text, 30,
341 [" This is a sentence with", "leading whitespace."])
342
Guido van Rossumd8faa362007-04-27 19:54:29 +0000343 def test_no_drop_whitespace(self):
344 # SF patch #1581073
345 text = " This is a sentence with much whitespace."
346 self.check_wrap(text, 10,
347 [" This is a", " ", "sentence ",
348 "with ", "much white", "space."],
349 drop_whitespace=False)
350
Greg Ward90c0b072002-08-22 18:11:10 +0000351 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000352 # Ensure that the standard _split() method works as advertised
353 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000354
Greg Wardee413842002-08-22 18:55:38 +0000355 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000356
Greg Wardee413842002-08-22 18:55:38 +0000357 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000358 self.check(result,
359 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
360 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
361
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000362 def test_break_on_hyphens(self):
363 # Ensure that the break_on_hyphens attributes work
364 text = "yaba daba-doo"
365 self.check_wrap(text, 10, ["yaba daba-", "doo"],
366 break_on_hyphens=True)
367 self.check_wrap(text, 10, ["yaba", "daba-doo"],
368 break_on_hyphens=False)
369
Greg Ward49128572003-05-07 00:54:42 +0000370 def test_bad_width(self):
371 # Ensure that width <= 0 is caught.
372 text = "Whatever, it doesn't matter."
373 self.assertRaises(ValueError, wrap, text, 0)
374 self.assertRaises(ValueError, wrap, text, -1)
375
Antoine Pitrou7c59bc62008-12-13 23:20:54 +0000376 def test_no_split_at_umlaut(self):
377 text = "Die Empf\xe4nger-Auswahl"
378 self.check_wrap(text, 13, ["Die", "Empf\xe4nger-", "Auswahl"])
379
380 def test_umlaut_followed_by_dash(self):
381 text = "aa \xe4\xe4-\xe4\xe4"
382 self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"])
383
Greg Ward90c0b072002-08-22 18:11:10 +0000384
Greg Wardfd030e42002-08-22 19:02:37 +0000385class LongWordTestCase (BaseTestCase):
386 def setUp(self):
387 self.wrapper = TextWrapper()
Greg Ward24cbbcb2002-12-09 16:27:15 +0000388 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000389Did you say "supercalifragilisticexpialidocious?"
390How *do* you spell that odd word, anyways?
391'''
Greg Wardfd030e42002-08-22 19:02:37 +0000392
393 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000394 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000395
396 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000397 ['Did you say "supercalifragilis',
398 'ticexpialidocious?" How *do*',
399 'you spell that odd word,',
400 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000401 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000402 ['Did you say "supercalifragilisticexpialidocious?"',
403 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000404
Raymond Hettingerc11dbcd2003-08-30 14:43:55 +0000405 # SF bug 797650. Prevent an infinite loop by making sure that at
406 # least one character gets split off on every pass.
407 self.check_wrap('-'*10+'hello', 10,
408 ['----------',
409 ' h',
410 ' e',
411 ' l',
412 ' l',
413 ' o'],
414 subsequent_indent = ' '*15)
Greg Ward90c0b072002-08-22 18:11:10 +0000415
Georg Brandlfceab5a2008-01-19 20:08:23 +0000416 # bug 1146. Prevent a long word to be wrongly wrapped when the
417 # preceding word is exactly one character shorter than the width
418 self.check_wrap(self.text, 12,
419 ['Did you say ',
420 '"supercalifr',
421 'agilisticexp',
422 'ialidocious?',
423 '" How *do*',
424 'you spell',
425 'that odd',
426 'word,',
427 'anyways?'])
428
Guido van Rossum327af772002-08-22 20:13:47 +0000429 def test_nobreak_long(self):
430 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000431 self.wrapper.break_long_words = 0
432 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000433 expect = ['Did you say',
434 '"supercalifragilisticexpialidocious?"',
435 'How *do* you spell that odd',
436 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000437 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000438 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000439 self.check(result, expect)
440
441 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000442 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000443 self.check(result, expect)
444
445
Greg Ward13c53c62002-08-22 18:57:26 +0000446class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000447
448 # called before each test method
449 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000450 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000451This paragraph will be filled, first without any indentation,
452and then with some (including a hanging indent).'''
453
454
Greg Wardf6765782002-08-22 18:35:49 +0000455 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000456 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000457
458 expect = '''\
459This paragraph will be filled, first
460without any indentation, and then with
461some (including a hanging indent).'''
462
Greg Wardf69d3c92002-08-22 19:06:45 +0000463 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000464 self.check(result, expect)
465
466
Greg Wardf6765782002-08-22 18:35:49 +0000467 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000468 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000469
Greg Wardf69d3c92002-08-22 19:06:45 +0000470 expect = [" This paragraph will be filled,",
471 "first without any indentation, and then",
472 "with some (including a hanging indent)."]
473 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000474 self.check(result, expect)
475
Greg Wardf69d3c92002-08-22 19:06:45 +0000476 expect = "\n".join(expect)
477 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000478 self.check(result, expect)
479
480
Greg Wardf6765782002-08-22 18:35:49 +0000481 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000482 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000483
484 expect = '''\
485 * This paragraph will be filled, first
486 without any indentation, and then
487 with some (including a hanging
488 indent).'''
489
Greg Wardf69d3c92002-08-22 19:06:45 +0000490 result = fill(self.text, 40,
491 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000492 self.check(result, expect)
493
494
Greg Ward9e082f42003-05-08 01:58:26 +0000495# Despite the similar names, DedentTestCase is *not* the inverse
496# of IndentTestCase!
497class DedentTestCase(unittest.TestCase):
498
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000499 def assertUnchanged(self, text):
500 """assert that dedent() has no effect on 'text'"""
Ezio Melottib3aedd42010-11-20 19:04:17 +0000501 self.assertEqual(text, dedent(text))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000502
Greg Ward9e082f42003-05-08 01:58:26 +0000503 def test_dedent_nomargin(self):
504 # No lines indented.
505 text = "Hello there.\nHow are you?\nOh good, I'm glad."
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000506 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000507
508 # Similar, with a blank line.
509 text = "Hello there.\n\nBoo!"
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000511
512 # Some lines indented, but overall margin is still zero.
513 text = "Hello there.\n This is indented."
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000514 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000515
516 # Again, add a blank line.
517 text = "Hello there.\n\n Boo!\n"
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000518 self.assertUnchanged(text)
Greg Ward9e082f42003-05-08 01:58:26 +0000519
520 def test_dedent_even(self):
521 # All lines indented by two spaces.
522 text = " Hello there.\n How are ya?\n Oh good."
523 expect = "Hello there.\nHow are ya?\nOh good."
Ezio Melottib3aedd42010-11-20 19:04:17 +0000524 self.assertEqual(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000525
526 # Same, with blank lines.
527 text = " Hello there.\n\n How are ya?\n Oh good.\n"
528 expect = "Hello there.\n\nHow are ya?\nOh good.\n"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000529 self.assertEqual(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000530
531 # Now indent one of the 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"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000534 self.assertEqual(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000535
536 def test_dedent_uneven(self):
537 # Lines indented unevenly.
538 text = '''\
539 def foo():
540 while 1:
541 return foo
542 '''
543 expect = '''\
544def foo():
545 while 1:
546 return foo
547'''
Ezio Melottib3aedd42010-11-20 19:04:17 +0000548 self.assertEqual(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000549
550 # Uneven indentation with a blank line.
551 text = " Foo\n Bar\n\n Baz\n"
552 expect = "Foo\n Bar\n\n Baz\n"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000553 self.assertEqual(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000554
555 # Uneven indentation with a whitespace-only line.
556 text = " Foo\n Bar\n \n Baz\n"
557 expect = "Foo\n Bar\n\n Baz\n"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000558 self.assertEqual(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000559
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000560 # dedent() should not mangle internal tabs
561 def test_dedent_preserve_internal_tabs(self):
562 text = " hello\tthere\n how are\tyou?"
563 expect = "hello\tthere\nhow are\tyou?"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000564 self.assertEqual(expect, dedent(text))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000565
566 # make sure that it preserves tabs when it's not making any
567 # changes at all
Ezio Melottib3aedd42010-11-20 19:04:17 +0000568 self.assertEqual(expect, dedent(expect))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000569
570 # dedent() should not mangle tabs in the margin (i.e.
571 # tabs and spaces both count as margin, but are *not*
572 # considered equivalent)
573 def test_dedent_preserve_margin_tabs(self):
574 text = " hello there\n\thow are you?"
575 self.assertUnchanged(text)
576
577 # same effect even if we have 8 spaces
578 text = " hello there\n\thow are you?"
579 self.assertUnchanged(text)
580
581 # dedent() only removes whitespace that can be uniformly removed!
582 text = "\thello there\n\thow are you?"
583 expect = "hello there\nhow are you?"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000584 self.assertEqual(expect, dedent(text))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585
586 text = " \thello there\n \thow are you?"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000587 self.assertEqual(expect, dedent(text))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000588
589 text = " \t hello there\n \t how are you?"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000590 self.assertEqual(expect, dedent(text))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000591
592 text = " \thello there\n \t how are you?"
593 expect = "hello there\n how are you?"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000594 self.assertEqual(expect, dedent(text))
Greg Ward9e082f42003-05-08 01:58:26 +0000595
596
Greg Wardf6765782002-08-22 18:35:49 +0000597def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000598 support.run_unittest(WrapTestCase,
Greg Ward9e082f42003-05-08 01:58:26 +0000599 LongWordTestCase,
600 IndentTestCases,
601 DedentTestCase)
Greg Wardf6765782002-08-22 18:35:49 +0000602
Greg Ward90c0b072002-08-22 18:11:10 +0000603if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000604 test_main()