test_textwrap.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #
  2. # Test suite 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. import unittest
  11. from test import test_support
  12. from textwrap import TextWrapper, wrap, fill, dedent
  13. class BaseTestCase(unittest.TestCase):
  14. '''Parent class with utility methods for textwrap tests.'''
  15. def show(self, textin):
  16. if isinstance(textin, list):
  17. result = []
  18. for i in range(len(textin)):
  19. result.append(" %d: %r" % (i, textin[i]))
  20. result = '\n'.join(result)
  21. elif isinstance(textin, basestring):
  22. result = " %s\n" % repr(textin)
  23. return result
  24. def check(self, result, expect):
  25. self.assertEqual(result, expect,
  26. 'expected:\n%s\nbut got:\n%s' % (
  27. self.show(expect), self.show(result)))
  28. def check_wrap(self, text, width, expect, **kwargs):
  29. result = wrap(text, width, **kwargs)
  30. self.check(result, expect)
  31. def check_split(self, text, expect):
  32. result = self.wrapper._split(text)
  33. self.assertEqual(result, expect,
  34. "\nexpected %r\n"
  35. "but got %r" % (expect, result))
  36. class WrapTestCase(BaseTestCase):
  37. def setUp(self):
  38. self.wrapper = TextWrapper(width=45)
  39. def test_simple(self):
  40. # Simple case: just words, spaces, and a bit of punctuation
  41. text = "Hello there, how are you this fine day? I'm glad to hear it!"
  42. self.check_wrap(text, 12,
  43. ["Hello there,",
  44. "how are you",
  45. "this fine",
  46. "day? I'm",
  47. "glad to hear",
  48. "it!"])
  49. self.check_wrap(text, 42,
  50. ["Hello there, how are you this fine day?",
  51. "I'm glad to hear it!"])
  52. self.check_wrap(text, 80, [text])
  53. def test_empty_string(self):
  54. # Check that wrapping the empty string returns an empty list.
  55. self.check_wrap("", 6, [])
  56. self.check_wrap("", 6, [], drop_whitespace=False)
  57. def test_empty_string_with_initial_indent(self):
  58. # Check that the empty string is not indented.
  59. self.check_wrap("", 6, [], initial_indent="++")
  60. self.check_wrap("", 6, [], initial_indent="++", drop_whitespace=False)
  61. def test_whitespace(self):
  62. # Whitespace munging and end-of-sentence detection
  63. text = """\
  64. This is a paragraph that already has
  65. line breaks. But some of its lines are much longer than the others,
  66. so it needs to be wrapped.
  67. Some lines are \ttabbed too.
  68. What a mess!
  69. """
  70. expect = ["This is a paragraph that already has line",
  71. "breaks. But some of its lines are much",
  72. "longer than the others, so it needs to be",
  73. "wrapped. Some lines are tabbed too. What a",
  74. "mess!"]
  75. wrapper = TextWrapper(45, fix_sentence_endings=True)
  76. result = wrapper.wrap(text)
  77. self.check(result, expect)
  78. result = wrapper.fill(text)
  79. self.check(result, '\n'.join(expect))
  80. def test_fix_sentence_endings(self):
  81. wrapper = TextWrapper(60, fix_sentence_endings=True)
  82. # SF #847346: ensure that fix_sentence_endings=True does the
  83. # right thing even on input short enough that it doesn't need to
  84. # be wrapped.
  85. text = "A short line. Note the single space."
  86. expect = ["A short line. Note the single space."]
  87. self.check(wrapper.wrap(text), expect)
  88. # Test some of the hairy end cases that _fix_sentence_endings()
  89. # is supposed to handle (the easy stuff is tested in
  90. # test_whitespace() above).
  91. text = "Well, Doctor? What do you think?"
  92. expect = ["Well, Doctor? What do you think?"]
  93. self.check(wrapper.wrap(text), expect)
  94. text = "Well, Doctor?\nWhat do you think?"
  95. self.check(wrapper.wrap(text), expect)
  96. text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
  97. expect = ['I say, chaps! Anyone for "tennis?" Hmmph!']
  98. self.check(wrapper.wrap(text), expect)
  99. wrapper.width = 20
  100. expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
  101. self.check(wrapper.wrap(text), expect)
  102. text = 'And she said, "Go to hell!"\nCan you believe that?'
  103. expect = ['And she said, "Go to',
  104. 'hell!" Can you',
  105. 'believe that?']
  106. self.check(wrapper.wrap(text), expect)
  107. wrapper.width = 60
  108. expect = ['And she said, "Go to hell!" Can you believe that?']
  109. self.check(wrapper.wrap(text), expect)
  110. text = 'File stdio.h is nice.'
  111. expect = ['File stdio.h is nice.']
  112. self.check(wrapper.wrap(text), expect)
  113. def test_wrap_short(self):
  114. # Wrapping to make short lines longer
  115. text = "This is a\nshort paragraph."
  116. self.check_wrap(text, 20, ["This is a short",
  117. "paragraph."])
  118. self.check_wrap(text, 40, ["This is a short paragraph."])
  119. def test_wrap_short_1line(self):
  120. # Test endcases
  121. text = "This is a short line."
  122. self.check_wrap(text, 30, ["This is a short line."])
  123. self.check_wrap(text, 30, ["(1) This is a short line."],
  124. initial_indent="(1) ")
  125. def test_hyphenated(self):
  126. # Test breaking hyphenated words
  127. text = ("this-is-a-useful-feature-for-"
  128. "reformatting-posts-from-tim-peters'ly")
  129. self.check_wrap(text, 40,
  130. ["this-is-a-useful-feature-for-",
  131. "reformatting-posts-from-tim-peters'ly"])
  132. self.check_wrap(text, 41,
  133. ["this-is-a-useful-feature-for-",
  134. "reformatting-posts-from-tim-peters'ly"])
  135. self.check_wrap(text, 42,
  136. ["this-is-a-useful-feature-for-reformatting-",
  137. "posts-from-tim-peters'ly"])
  138. def test_hyphenated_numbers(self):
  139. # Test that hyphenated numbers (eg. dates) are not broken like words.
  140. text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n"
  141. "released on 1994-02-15.")
  142. self.check_wrap(text, 35, ['Python 1.0.0 was released on',
  143. '1994-01-26. Python 1.0.1 was',
  144. 'released on 1994-02-15.'])
  145. self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
  146. 'Python 1.0.1 was released on 1994-02-15.'])
  147. text = "I do all my shopping at 7-11."
  148. self.check_wrap(text, 25, ["I do all my shopping at",
  149. "7-11."])
  150. self.check_wrap(text, 27, ["I do all my shopping at",
  151. "7-11."])
  152. self.check_wrap(text, 29, ["I do all my shopping at 7-11."])
  153. def test_em_dash(self):
  154. # Test text with em-dashes
  155. text = "Em-dashes should be written -- thus."
  156. self.check_wrap(text, 25,
  157. ["Em-dashes should be",
  158. "written -- thus."])
  159. # Probe the boundaries of the properly written em-dash,
  160. # ie. " -- ".
  161. self.check_wrap(text, 29,
  162. ["Em-dashes should be written",
  163. "-- thus."])
  164. expect = ["Em-dashes should be written --",
  165. "thus."]
  166. self.check_wrap(text, 30, expect)
  167. self.check_wrap(text, 35, expect)
  168. self.check_wrap(text, 36,
  169. ["Em-dashes should be written -- thus."])
  170. # The improperly written em-dash is handled too, because
  171. # it's adjacent to non-whitespace on both sides.
  172. text = "You can also do--this or even---this."
  173. expect = ["You can also do",
  174. "--this or even",
  175. "---this."]
  176. self.check_wrap(text, 15, expect)
  177. self.check_wrap(text, 16, expect)
  178. expect = ["You can also do--",
  179. "this or even---",
  180. "this."]
  181. self.check_wrap(text, 17, expect)
  182. self.check_wrap(text, 19, expect)
  183. expect = ["You can also do--this or even",
  184. "---this."]
  185. self.check_wrap(text, 29, expect)
  186. self.check_wrap(text, 31, expect)
  187. expect = ["You can also do--this or even---",
  188. "this."]
  189. self.check_wrap(text, 32, expect)
  190. self.check_wrap(text, 35, expect)
  191. # All of the above behaviour could be deduced by probing the
  192. # _split() method.
  193. text = "Here's an -- em-dash and--here's another---and another!"
  194. expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
  195. "and", "--", "here's", " ", "another", "---",
  196. "and", " ", "another!"]
  197. self.check_split(text, expect)
  198. text = "and then--bam!--he was gone"
  199. expect = ["and", " ", "then", "--", "bam!", "--",
  200. "he", " ", "was", " ", "gone"]
  201. self.check_split(text, expect)
  202. def test_unix_options (self):
  203. # Test that Unix-style command-line options are wrapped correctly.
  204. # Both Optik (OptionParser) and Docutils rely on this behaviour!
  205. text = "You should use the -n option, or --dry-run in its long form."
  206. self.check_wrap(text, 20,
  207. ["You should use the",
  208. "-n option, or --dry-",
  209. "run in its long",
  210. "form."])
  211. self.check_wrap(text, 21,
  212. ["You should use the -n",
  213. "option, or --dry-run",
  214. "in its long form."])
  215. expect = ["You should use the -n option, or",
  216. "--dry-run in its long form."]
  217. self.check_wrap(text, 32, expect)
  218. self.check_wrap(text, 34, expect)
  219. self.check_wrap(text, 35, expect)
  220. self.check_wrap(text, 38, expect)
  221. expect = ["You should use the -n option, or --dry-",
  222. "run in its long form."]
  223. self.check_wrap(text, 39, expect)
  224. self.check_wrap(text, 41, expect)
  225. expect = ["You should use the -n option, or --dry-run",
  226. "in its long form."]
  227. self.check_wrap(text, 42, expect)
  228. # Again, all of the above can be deduced from _split().
  229. text = "the -n option, or --dry-run or --dryrun"
  230. expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
  231. "--dry-", "run", " ", "or", " ", "--dryrun"]
  232. self.check_split(text, expect)
  233. def test_funky_hyphens (self):
  234. # Screwy edge cases cooked up by David Goodger. All reported
  235. # in SF bug #596434.
  236. self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
  237. self.check_split("what the--", ["what", " ", "the--"])
  238. self.check_split("what the--.", ["what", " ", "the--."])
  239. self.check_split("--text--.", ["--text--."])
  240. # When I first read bug #596434, this is what I thought David
  241. # was talking about. I was wrong; these have always worked
  242. # fine. The real problem is tested in test_funky_parens()
  243. # below...
  244. self.check_split("--option", ["--option"])
  245. self.check_split("--option-opt", ["--option-", "opt"])
  246. self.check_split("foo --option-opt bar",
  247. ["foo", " ", "--option-", "opt", " ", "bar"])
  248. def test_punct_hyphens(self):
  249. # Oh bother, SF #965425 found another problem with hyphens --
  250. # hyphenated words in single quotes weren't handled correctly.
  251. # In fact, the bug is that *any* punctuation around a hyphenated
  252. # word was handled incorrectly, except for a leading "--", which
  253. # was special-cased for Optik and Docutils. So test a variety
  254. # of styles of punctuation around a hyphenated word.
  255. # (Actually this is based on an Optik bug report, #813077).
  256. self.check_split("the 'wibble-wobble' widget",
  257. ['the', ' ', "'wibble-", "wobble'", ' ', 'widget'])
  258. self.check_split('the "wibble-wobble" widget',
  259. ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget'])
  260. self.check_split("the (wibble-wobble) widget",
  261. ['the', ' ', "(wibble-", "wobble)", ' ', 'widget'])
  262. self.check_split("the ['wibble-wobble'] widget",
  263. ['the', ' ', "['wibble-", "wobble']", ' ', 'widget'])
  264. def test_funky_parens (self):
  265. # Second part of SF bug #596434: long option strings inside
  266. # parentheses.
  267. self.check_split("foo (--option) bar",
  268. ["foo", " ", "(--option)", " ", "bar"])
  269. # Related stuff -- make sure parens work in simpler contexts.
  270. self.check_split("foo (bar) baz",
  271. ["foo", " ", "(bar)", " ", "baz"])
  272. self.check_split("blah (ding dong), wubba",
  273. ["blah", " ", "(ding", " ", "dong),",
  274. " ", "wubba"])
  275. def test_drop_whitespace_false(self):
  276. # Check that drop_whitespace=False preserves whitespace.
  277. # SF patch #1581073
  278. text = " This is a sentence with much whitespace."
  279. self.check_wrap(text, 10,
  280. [" This is a", " ", "sentence ",
  281. "with ", "much white", "space."],
  282. drop_whitespace=False)
  283. def test_drop_whitespace_false_whitespace_only(self):
  284. # Check that drop_whitespace=False preserves a whitespace-only string.
  285. self.check_wrap(" ", 6, [" "], drop_whitespace=False)
  286. def test_drop_whitespace_false_whitespace_only_with_indent(self):
  287. # Check that a whitespace-only string gets indented (when
  288. # drop_whitespace is False).
  289. self.check_wrap(" ", 6, [" "], drop_whitespace=False,
  290. initial_indent=" ")
  291. def test_drop_whitespace_whitespace_only(self):
  292. # Check drop_whitespace on a whitespace-only string.
  293. self.check_wrap(" ", 6, [])
  294. def test_drop_whitespace_leading_whitespace(self):
  295. # Check that drop_whitespace does not drop leading whitespace (if
  296. # followed by non-whitespace).
  297. # SF bug #622849 reported inconsistent handling of leading
  298. # whitespace; let's test that a bit, shall we?
  299. text = " This is a sentence with leading whitespace."
  300. self.check_wrap(text, 50,
  301. [" This is a sentence with leading whitespace."])
  302. self.check_wrap(text, 30,
  303. [" This is a sentence with", "leading whitespace."])
  304. def test_drop_whitespace_whitespace_line(self):
  305. # Check that drop_whitespace skips the whole line if a non-leading
  306. # line consists only of whitespace.
  307. text = "abcd efgh"
  308. # Include the result for drop_whitespace=False for comparison.
  309. self.check_wrap(text, 6, ["abcd", " ", "efgh"],
  310. drop_whitespace=False)
  311. self.check_wrap(text, 6, ["abcd", "efgh"])
  312. def test_drop_whitespace_whitespace_only_with_indent(self):
  313. # Check that initial_indent is not applied to a whitespace-only
  314. # string. This checks a special case of the fact that dropping
  315. # whitespace occurs before indenting.
  316. self.check_wrap(" ", 6, [], initial_indent="++")
  317. def test_drop_whitespace_whitespace_indent(self):
  318. # Check that drop_whitespace does not drop whitespace indents.
  319. # This checks a special case of the fact that dropping whitespace
  320. # occurs before indenting.
  321. self.check_wrap("abcd efgh", 6, [" abcd", " efgh"],
  322. initial_indent=" ", subsequent_indent=" ")
  323. if test_support.have_unicode:
  324. def test_unicode(self):
  325. # *Very* simple test of wrapping Unicode strings. I'm sure
  326. # there's more to it than this, but let's at least make
  327. # sure textwrap doesn't crash on Unicode input!
  328. text = u"Hello there, how are you today?"
  329. self.check_wrap(text, 50, [u"Hello there, how are you today?"])
  330. self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
  331. olines = self.wrapper.wrap(text)
  332. self.assertIsInstance(olines, list)
  333. self.assertIsInstance(olines[0], unicode)
  334. otext = self.wrapper.fill(text)
  335. self.assertIsInstance(otext, unicode)
  336. def test_no_split_at_umlaut(self):
  337. text = u"Die Empf\xe4nger-Auswahl"
  338. self.check_wrap(text, 13, [u"Die", u"Empf\xe4nger-", u"Auswahl"])
  339. def test_umlaut_followed_by_dash(self):
  340. text = u"aa \xe4\xe4-\xe4\xe4"
  341. self.check_wrap(text, 7, [u"aa \xe4\xe4-", u"\xe4\xe4"])
  342. def test_split(self):
  343. # Ensure that the standard _split() method works as advertised
  344. # in the comments
  345. text = "Hello there -- you goof-ball, use the -b option!"
  346. result = self.wrapper._split(text)
  347. self.check(result,
  348. ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
  349. "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
  350. def test_break_on_hyphens(self):
  351. # Ensure that the break_on_hyphens attributes work
  352. text = "yaba daba-doo"
  353. self.check_wrap(text, 10, ["yaba daba-", "doo"],
  354. break_on_hyphens=True)
  355. self.check_wrap(text, 10, ["yaba", "daba-doo"],
  356. break_on_hyphens=False)
  357. def test_bad_width(self):
  358. # Ensure that width <= 0 is caught.
  359. text = "Whatever, it doesn't matter."
  360. self.assertRaises(ValueError, wrap, text, 0)
  361. self.assertRaises(ValueError, wrap, text, -1)
  362. class LongWordTestCase (BaseTestCase):
  363. def setUp(self):
  364. self.wrapper = TextWrapper()
  365. self.text = '''\
  366. Did you say "supercalifragilisticexpialidocious?"
  367. How *do* you spell that odd word, anyways?
  368. '''
  369. def test_break_long(self):
  370. # Wrap text with long words and lots of punctuation
  371. self.check_wrap(self.text, 30,
  372. ['Did you say "supercalifragilis',
  373. 'ticexpialidocious?" How *do*',
  374. 'you spell that odd word,',
  375. 'anyways?'])
  376. self.check_wrap(self.text, 50,
  377. ['Did you say "supercalifragilisticexpialidocious?"',
  378. 'How *do* you spell that odd word, anyways?'])
  379. # SF bug 797650. Prevent an infinite loop by making sure that at
  380. # least one character gets split off on every pass.
  381. self.check_wrap('-'*10+'hello', 10,
  382. ['----------',
  383. ' h',
  384. ' e',
  385. ' l',
  386. ' l',
  387. ' o'],
  388. subsequent_indent = ' '*15)
  389. # bug 1146. Prevent a long word to be wrongly wrapped when the
  390. # preceding word is exactly one character shorter than the width
  391. self.check_wrap(self.text, 12,
  392. ['Did you say ',
  393. '"supercalifr',
  394. 'agilisticexp',
  395. 'ialidocious?',
  396. '" How *do*',
  397. 'you spell',
  398. 'that odd',
  399. 'word,',
  400. 'anyways?'])
  401. def test_nobreak_long(self):
  402. # Test with break_long_words disabled
  403. self.wrapper.break_long_words = 0
  404. self.wrapper.width = 30
  405. expect = ['Did you say',
  406. '"supercalifragilisticexpialidocious?"',
  407. 'How *do* you spell that odd',
  408. 'word, anyways?'
  409. ]
  410. result = self.wrapper.wrap(self.text)
  411. self.check(result, expect)
  412. # Same thing with kwargs passed to standalone wrap() function.
  413. result = wrap(self.text, width=30, break_long_words=0)
  414. self.check(result, expect)
  415. class IndentTestCases(BaseTestCase):
  416. # called before each test method
  417. def setUp(self):
  418. self.text = '''\
  419. This paragraph will be filled, first without any indentation,
  420. and then with some (including a hanging indent).'''
  421. def test_fill(self):
  422. # Test the fill() method
  423. expect = '''\
  424. This paragraph will be filled, first
  425. without any indentation, and then with
  426. some (including a hanging indent).'''
  427. result = fill(self.text, 40)
  428. self.check(result, expect)
  429. def test_initial_indent(self):
  430. # Test initial_indent parameter
  431. expect = [" This paragraph will be filled,",
  432. "first without any indentation, and then",
  433. "with some (including a hanging indent)."]
  434. result = wrap(self.text, 40, initial_indent=" ")
  435. self.check(result, expect)
  436. expect = "\n".join(expect)
  437. result = fill(self.text, 40, initial_indent=" ")
  438. self.check(result, expect)
  439. def test_subsequent_indent(self):
  440. # Test subsequent_indent parameter
  441. expect = '''\
  442. * This paragraph will be filled, first
  443. without any indentation, and then
  444. with some (including a hanging
  445. indent).'''
  446. result = fill(self.text, 40,
  447. initial_indent=" * ", subsequent_indent=" ")
  448. self.check(result, expect)
  449. # Despite the similar names, DedentTestCase is *not* the inverse
  450. # of IndentTestCase!
  451. class DedentTestCase(unittest.TestCase):
  452. def assertUnchanged(self, text):
  453. """assert that dedent() has no effect on 'text'"""
  454. self.assertEqual(text, dedent(text))
  455. def test_dedent_nomargin(self):
  456. # No lines indented.
  457. text = "Hello there.\nHow are you?\nOh good, I'm glad."
  458. self.assertUnchanged(text)
  459. # Similar, with a blank line.
  460. text = "Hello there.\n\nBoo!"
  461. self.assertUnchanged(text)
  462. # Some lines indented, but overall margin is still zero.
  463. text = "Hello there.\n This is indented."
  464. self.assertUnchanged(text)
  465. # Again, add a blank line.
  466. text = "Hello there.\n\n Boo!\n"
  467. self.assertUnchanged(text)
  468. def test_dedent_even(self):
  469. # All lines indented by two spaces.
  470. text = " Hello there.\n How are ya?\n Oh good."
  471. expect = "Hello there.\nHow are ya?\nOh good."
  472. self.assertEqual(expect, dedent(text))
  473. # Same, with blank lines.
  474. text = " Hello there.\n\n How are ya?\n Oh good.\n"
  475. expect = "Hello there.\n\nHow are ya?\nOh good.\n"
  476. self.assertEqual(expect, dedent(text))
  477. # Now indent one of the blank lines.
  478. text = " Hello there.\n \n How are ya?\n Oh good.\n"
  479. expect = "Hello there.\n\nHow are ya?\nOh good.\n"
  480. self.assertEqual(expect, dedent(text))
  481. def test_dedent_uneven(self):
  482. # Lines indented unevenly.
  483. text = '''\
  484. def foo():
  485. while 1:
  486. return foo
  487. '''
  488. expect = '''\
  489. def foo():
  490. while 1:
  491. return foo
  492. '''
  493. self.assertEqual(expect, dedent(text))
  494. # Uneven indentation with a blank line.
  495. text = " Foo\n Bar\n\n Baz\n"
  496. expect = "Foo\n Bar\n\n Baz\n"
  497. self.assertEqual(expect, dedent(text))
  498. # Uneven indentation with a whitespace-only line.
  499. text = " Foo\n Bar\n \n Baz\n"
  500. expect = "Foo\n Bar\n\n Baz\n"
  501. self.assertEqual(expect, dedent(text))
  502. # dedent() should not mangle internal tabs
  503. def test_dedent_preserve_internal_tabs(self):
  504. text = " hello\tthere\n how are\tyou?"
  505. expect = "hello\tthere\nhow are\tyou?"
  506. self.assertEqual(expect, dedent(text))
  507. # make sure that it preserves tabs when it's not making any
  508. # changes at all
  509. self.assertEqual(expect, dedent(expect))
  510. # dedent() should not mangle tabs in the margin (i.e.
  511. # tabs and spaces both count as margin, but are *not*
  512. # considered equivalent)
  513. def test_dedent_preserve_margin_tabs(self):
  514. text = " hello there\n\thow are you?"
  515. self.assertUnchanged(text)
  516. # same effect even if we have 8 spaces
  517. text = " hello there\n\thow are you?"
  518. self.assertUnchanged(text)
  519. # dedent() only removes whitespace that can be uniformly removed!
  520. text = "\thello there\n\thow are you?"
  521. expect = "hello there\nhow are you?"
  522. self.assertEqual(expect, dedent(text))
  523. text = " \thello there\n \thow are you?"
  524. self.assertEqual(expect, dedent(text))
  525. text = " \t hello there\n \t how are you?"
  526. self.assertEqual(expect, dedent(text))
  527. text = " \thello there\n \t how are you?"
  528. expect = "hello there\n how are you?"
  529. self.assertEqual(expect, dedent(text))
  530. # test margin is smaller than smallest indent
  531. text = " \thello there\n \thow are you?\n \tI'm fine, thanks"
  532. expect = " \thello there\n \thow are you?\n\tI'm fine, thanks"
  533. self.assertEqual(expect, dedent(text))
  534. def test_main():
  535. test_support.run_unittest(WrapTestCase,
  536. LongWordTestCase,
  537. IndentTestCases,
  538. DedentTestCase)
  539. if __name__ == '__main__':
  540. test_main()