Greg Ward | 3dc94e1 | 2002-08-22 18:37:50 +0000 | [diff] [blame] | 1 | # |
Greg Ward | 6186410 | 2004-06-03 01:59:41 +0000 | [diff] [blame] | 2 | # Test suite for the textwrap module. |
Greg Ward | 3dc94e1 | 2002-08-22 18:37:50 +0000 | [diff] [blame] | 3 | # |
| 4 | # Original tests written by Greg Ward <gward@python.net>. |
| 5 | # Converted to PyUnit by Peter Hansen <peter@engcorp.com>. |
| 6 | # Currently maintained by Greg Ward. |
| 7 | # |
| 8 | # $Id$ |
| 9 | # |
| 10 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 11 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 12 | from test import support |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 13 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 14 | from textwrap import TextWrapper, wrap, fill, dedent |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 15 | |
| 16 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 17 | class BaseTestCase(unittest.TestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 18 | '''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 Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 26 | elif isinstance(textin, str): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 27 | result = " %s\n" % repr(textin) |
| 28 | return result |
| 29 | |
| 30 | |
| 31 | def check(self, result, expect): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 32 | self.assertEqual(result, expect, |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 33 | 'expected:\n%s\nbut got:\n%s' % ( |
| 34 | self.show(expect), self.show(result))) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 36 | def check_wrap(self, text, width, expect, **kwargs): |
| 37 | result = wrap(text, width, **kwargs) |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 38 | self.check(result, expect) |
| 39 | |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 40 | def check_split(self, text, expect): |
| 41 | result = self.wrapper._split(text) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 42 | self.assertEqual(result, expect, |
| 43 | "\nexpected %r\n" |
| 44 | "but got %r" % (expect, result)) |
Greg Ward | 715debd | 2002-08-22 21:16:25 +0000 | [diff] [blame] | 45 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 46 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 47 | class WrapTestCase(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 48 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 49 | def setUp(self): |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 50 | self.wrapper = TextWrapper(width=45) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 51 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 52 | def test_simple(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 53 | # Simple case: just words, spaces, and a bit of punctuation |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 54 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 55 | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 56 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 57 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 68 | |
| 69 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 70 | def test_whitespace(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 71 | # Whitespace munging and end-of-sentence detection |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 72 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 73 | text = """\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 74 | This is a paragraph that already has |
| 75 | line breaks. But some of its lines are much longer than the others, |
| 76 | so it needs to be wrapped. |
| 77 | Some lines are \ttabbed too. |
| 78 | What a mess! |
| 79 | """ |
| 80 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 81 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 86 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 87 | wrapper = TextWrapper(45, fix_sentence_endings=True) |
| 88 | result = wrapper.wrap(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 89 | self.check(result, expect) |
| 90 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 91 | result = wrapper.fill(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 92 | self.check(result, '\n'.join(expect)) |
| 93 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 94 | def test_fix_sentence_endings(self): |
| 95 | wrapper = TextWrapper(60, fix_sentence_endings=True) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 96 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 97 | # 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 Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 131 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 132 | text = 'File stdio.h is nice.' |
| 133 | expect = ['File stdio.h is nice.'] |
| 134 | self.check(wrapper.wrap(text), expect) |
| 135 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 136 | def test_wrap_short(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 137 | # Wrapping to make short lines longer |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 138 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 139 | text = "This is a\nshort paragraph." |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 140 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 141 | self.check_wrap(text, 20, ["This is a short", |
| 142 | "paragraph."]) |
| 143 | self.check_wrap(text, 40, ["This is a short paragraph."]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 144 | |
| 145 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 146 | 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 Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 156 | def test_hyphenated(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 157 | # Test breaking hyphenated words |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 158 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 159 | text = ("this-is-a-useful-feature-for-" |
| 160 | "reformatting-posts-from-tim-peters'ly") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 161 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 162 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 171 | |
Greg Ward | 4040794 | 2005-03-05 02:53:17 +0000 | [diff] [blame] | 172 | 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 | |
| 177 | self.check_wrap(text, 30, ['Python 1.0.0 was released on', |
| 178 | '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 Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 190 | def test_em_dash(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 191 | # Test text with em-dashes |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 192 | 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 Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 208 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 209 | # 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 Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 234 | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
| 235 | "and", "--", "here's", " ", "another", "---", |
| 236 | "and", " ", "another!"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 237 | self.check_split(text, expect) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 238 | |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 239 | text = "and then--bam!--he was gone" |
| 240 | expect = ["and", " ", "then", "--", "bam!", "--", |
| 241 | "he", " ", "was", " ", "gone"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 242 | self.check_split(text, expect) |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 243 | |
| 244 | |
Greg Ward | 34f995b | 2002-08-22 21:10:07 +0000 | [diff] [blame] | 245 | 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 Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 273 | # Again, all of the above can be deduced from _split(). |
| 274 | text = "the -n option, or --dry-run or --dryrun" |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 275 | expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", |
| 276 | "--dry-", "run", " ", "or", " ", "--dryrun"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 277 | 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 Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 287 | # 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 Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 291 | self.check_split("--option", ["--option"]) |
| 292 | self.check_split("--option-opt", ["--option-", "opt"]) |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 293 | self.check_split("foo --option-opt bar", |
| 294 | ["foo", " ", "--option-", "opt", " ", "bar"]) |
| 295 | |
Greg Ward | 6186410 | 2004-06-03 01:59:41 +0000 | [diff] [blame] | 296 | 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 Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 313 | 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 Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 325 | |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 326 | 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 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 335 | 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 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 343 | def test_split(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 344 | # Ensure that the standard _split() method works as advertised |
| 345 | # in the comments |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 346 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 347 | text = "Hello there -- you goof-ball, use the -b option!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 348 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 349 | result = self.wrapper._split(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 350 | self.check(result, |
| 351 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 352 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 353 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 354 | def test_break_on_hyphens(self): |
| 355 | # Ensure that the break_on_hyphens attributes work |
| 356 | text = "yaba daba-doo" |
| 357 | self.check_wrap(text, 10, ["yaba daba-", "doo"], |
| 358 | break_on_hyphens=True) |
| 359 | self.check_wrap(text, 10, ["yaba", "daba-doo"], |
| 360 | break_on_hyphens=False) |
| 361 | |
Greg Ward | 4912857 | 2003-05-07 00:54:42 +0000 | [diff] [blame] | 362 | def test_bad_width(self): |
| 363 | # Ensure that width <= 0 is caught. |
| 364 | text = "Whatever, it doesn't matter." |
| 365 | self.assertRaises(ValueError, wrap, text, 0) |
| 366 | self.assertRaises(ValueError, wrap, text, -1) |
| 367 | |
Antoine Pitrou | 7c59bc6 | 2008-12-13 23:20:54 +0000 | [diff] [blame] | 368 | def test_no_split_at_umlaut(self): |
| 369 | text = "Die Empf\xe4nger-Auswahl" |
| 370 | self.check_wrap(text, 13, ["Die", "Empf\xe4nger-", "Auswahl"]) |
| 371 | |
| 372 | def test_umlaut_followed_by_dash(self): |
| 373 | text = "aa \xe4\xe4-\xe4\xe4" |
| 374 | self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"]) |
| 375 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 376 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 377 | class LongWordTestCase (BaseTestCase): |
| 378 | def setUp(self): |
| 379 | self.wrapper = TextWrapper() |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 380 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 381 | Did you say "supercalifragilisticexpialidocious?" |
| 382 | How *do* you spell that odd word, anyways? |
| 383 | ''' |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 384 | |
| 385 | def test_break_long(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 386 | # Wrap text with long words and lots of punctuation |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 387 | |
| 388 | self.check_wrap(self.text, 30, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 389 | ['Did you say "supercalifragilis', |
| 390 | 'ticexpialidocious?" How *do*', |
| 391 | 'you spell that odd word,', |
| 392 | 'anyways?']) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 393 | self.check_wrap(self.text, 50, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 394 | ['Did you say "supercalifragilisticexpialidocious?"', |
| 395 | 'How *do* you spell that odd word, anyways?']) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 396 | |
Raymond Hettinger | c11dbcd | 2003-08-30 14:43:55 +0000 | [diff] [blame] | 397 | # SF bug 797650. Prevent an infinite loop by making sure that at |
| 398 | # least one character gets split off on every pass. |
| 399 | self.check_wrap('-'*10+'hello', 10, |
| 400 | ['----------', |
| 401 | ' h', |
| 402 | ' e', |
| 403 | ' l', |
| 404 | ' l', |
| 405 | ' o'], |
| 406 | subsequent_indent = ' '*15) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 407 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 408 | # bug 1146. Prevent a long word to be wrongly wrapped when the |
| 409 | # preceding word is exactly one character shorter than the width |
| 410 | self.check_wrap(self.text, 12, |
| 411 | ['Did you say ', |
| 412 | '"supercalifr', |
| 413 | 'agilisticexp', |
| 414 | 'ialidocious?', |
| 415 | '" How *do*', |
| 416 | 'you spell', |
| 417 | 'that odd', |
| 418 | 'word,', |
| 419 | 'anyways?']) |
| 420 | |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 421 | def test_nobreak_long(self): |
| 422 | # Test with break_long_words disabled |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 423 | self.wrapper.break_long_words = 0 |
| 424 | self.wrapper.width = 30 |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 425 | expect = ['Did you say', |
| 426 | '"supercalifragilisticexpialidocious?"', |
| 427 | 'How *do* you spell that odd', |
| 428 | 'word, anyways?' |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 429 | ] |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 430 | result = self.wrapper.wrap(self.text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 431 | self.check(result, expect) |
| 432 | |
| 433 | # Same thing with kwargs passed to standalone wrap() function. |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 434 | result = wrap(self.text, width=30, break_long_words=0) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 435 | self.check(result, expect) |
| 436 | |
| 437 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 438 | class IndentTestCases(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 439 | |
| 440 | # called before each test method |
| 441 | def setUp(self): |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 442 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 443 | This paragraph will be filled, first without any indentation, |
| 444 | and then with some (including a hanging indent).''' |
| 445 | |
| 446 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 447 | def test_fill(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 448 | # Test the fill() method |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 449 | |
| 450 | expect = '''\ |
| 451 | This paragraph will be filled, first |
| 452 | without any indentation, and then with |
| 453 | some (including a hanging indent).''' |
| 454 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 455 | result = fill(self.text, 40) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 456 | self.check(result, expect) |
| 457 | |
| 458 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 459 | def test_initial_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 460 | # Test initial_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 461 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 462 | expect = [" This paragraph will be filled,", |
| 463 | "first without any indentation, and then", |
| 464 | "with some (including a hanging indent)."] |
| 465 | result = wrap(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 466 | self.check(result, expect) |
| 467 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 468 | expect = "\n".join(expect) |
| 469 | result = fill(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 470 | self.check(result, expect) |
| 471 | |
| 472 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 473 | def test_subsequent_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 474 | # Test subsequent_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 475 | |
| 476 | expect = '''\ |
| 477 | * This paragraph will be filled, first |
| 478 | without any indentation, and then |
| 479 | with some (including a hanging |
| 480 | indent).''' |
| 481 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 482 | result = fill(self.text, 40, |
| 483 | initial_indent=" * ", subsequent_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 484 | self.check(result, expect) |
| 485 | |
| 486 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 487 | # Despite the similar names, DedentTestCase is *not* the inverse |
| 488 | # of IndentTestCase! |
| 489 | class DedentTestCase(unittest.TestCase): |
| 490 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 491 | def assertUnchanged(self, text): |
| 492 | """assert that dedent() has no effect on 'text'""" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 493 | self.assertEqual(text, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 494 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 495 | def test_dedent_nomargin(self): |
| 496 | # No lines indented. |
| 497 | text = "Hello there.\nHow are you?\nOh good, I'm glad." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 498 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 499 | |
| 500 | # Similar, with a blank line. |
| 501 | text = "Hello there.\n\nBoo!" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 502 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 503 | |
| 504 | # Some lines indented, but overall margin is still zero. |
| 505 | text = "Hello there.\n This is indented." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 506 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 507 | |
| 508 | # Again, add a blank line. |
| 509 | text = "Hello there.\n\n Boo!\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 510 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 511 | |
| 512 | def test_dedent_even(self): |
| 513 | # All lines indented by two spaces. |
| 514 | text = " Hello there.\n How are ya?\n Oh good." |
| 515 | expect = "Hello there.\nHow are ya?\nOh good." |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 516 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 517 | |
| 518 | # Same, with blank lines. |
| 519 | text = " Hello there.\n\n How are ya?\n Oh good.\n" |
| 520 | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 521 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 522 | |
| 523 | # Now indent one of the blank lines. |
| 524 | text = " Hello there.\n \n How are ya?\n Oh good.\n" |
| 525 | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 526 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 527 | |
| 528 | def test_dedent_uneven(self): |
| 529 | # Lines indented unevenly. |
| 530 | text = '''\ |
| 531 | def foo(): |
| 532 | while 1: |
| 533 | return foo |
| 534 | ''' |
| 535 | expect = '''\ |
| 536 | def foo(): |
| 537 | while 1: |
| 538 | return foo |
| 539 | ''' |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 540 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 541 | |
| 542 | # Uneven indentation with a blank line. |
| 543 | text = " Foo\n Bar\n\n Baz\n" |
| 544 | expect = "Foo\n Bar\n\n Baz\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 545 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 546 | |
| 547 | # Uneven indentation with a whitespace-only line. |
| 548 | text = " Foo\n Bar\n \n Baz\n" |
| 549 | expect = "Foo\n Bar\n\n Baz\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 550 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 551 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 552 | # dedent() should not mangle internal tabs |
| 553 | def test_dedent_preserve_internal_tabs(self): |
| 554 | text = " hello\tthere\n how are\tyou?" |
| 555 | expect = "hello\tthere\nhow are\tyou?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 556 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 557 | |
| 558 | # make sure that it preserves tabs when it's not making any |
| 559 | # changes at all |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 560 | self.assertEqual(expect, dedent(expect)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 561 | |
| 562 | # dedent() should not mangle tabs in the margin (i.e. |
| 563 | # tabs and spaces both count as margin, but are *not* |
| 564 | # considered equivalent) |
| 565 | def test_dedent_preserve_margin_tabs(self): |
| 566 | text = " hello there\n\thow are you?" |
| 567 | self.assertUnchanged(text) |
| 568 | |
| 569 | # same effect even if we have 8 spaces |
| 570 | text = " hello there\n\thow are you?" |
| 571 | self.assertUnchanged(text) |
| 572 | |
| 573 | # dedent() only removes whitespace that can be uniformly removed! |
| 574 | text = "\thello there\n\thow are you?" |
| 575 | expect = "hello there\nhow are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 576 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 577 | |
| 578 | text = " \thello there\n \thow are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 579 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 580 | |
| 581 | text = " \t hello there\n \t how are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 582 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 583 | |
| 584 | text = " \thello there\n \t how are you?" |
| 585 | expect = "hello there\n how are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 586 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 587 | |
| 588 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 589 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 590 | support.run_unittest(WrapTestCase, |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 591 | LongWordTestCase, |
| 592 | IndentTestCases, |
| 593 | DedentTestCase) |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 594 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 595 | if __name__ == '__main__': |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 596 | test_main() |