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 |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 12 | from test import test_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) |
Raymond Hettinger | 7a70ea4 | 2003-09-17 05:50:59 +0000 | [diff] [blame] | 26 | elif isinstance(textin, basestring): |
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): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 32 | self.assertEquals(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) |
Greg Ward | 715debd | 2002-08-22 21:16:25 +0000 | [diff] [blame] | 42 | self.assertEquals(result, expect, |
| 43 | "\nexpected %r\n" |
| 44 | "but got %r" % (expect, result)) |
| 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) |
| 131 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 132 | def test_wrap_short(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 133 | # Wrapping to make short lines longer |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 134 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 135 | text = "This is a\nshort paragraph." |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 136 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 137 | self.check_wrap(text, 20, ["This is a short", |
| 138 | "paragraph."]) |
| 139 | self.check_wrap(text, 40, ["This is a short paragraph."]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 140 | |
| 141 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 142 | 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 Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 152 | def test_hyphenated(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 153 | # Test breaking hyphenated words |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 154 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 155 | text = ("this-is-a-useful-feature-for-" |
| 156 | "reformatting-posts-from-tim-peters'ly") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 157 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 158 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 167 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 168 | def test_em_dash(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 169 | # Test text with em-dashes |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 170 | text = "Em-dashes should be written -- thus." |
| 171 | self.check_wrap(text, 25, |
| 172 | ["Em-dashes should be", |
| 173 | "written -- thus."]) |
| 174 | |
| 175 | # Probe the boundaries of the properly written em-dash, |
| 176 | # ie. " -- ". |
| 177 | self.check_wrap(text, 29, |
| 178 | ["Em-dashes should be written", |
| 179 | "-- thus."]) |
| 180 | expect = ["Em-dashes should be written --", |
| 181 | "thus."] |
| 182 | self.check_wrap(text, 30, expect) |
| 183 | self.check_wrap(text, 35, expect) |
| 184 | self.check_wrap(text, 36, |
| 185 | ["Em-dashes should be written -- thus."]) |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 186 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 187 | # The improperly written em-dash is handled too, because |
| 188 | # it's adjacent to non-whitespace on both sides. |
| 189 | text = "You can also do--this or even---this." |
| 190 | expect = ["You can also do", |
| 191 | "--this or even", |
| 192 | "---this."] |
| 193 | self.check_wrap(text, 15, expect) |
| 194 | self.check_wrap(text, 16, expect) |
| 195 | expect = ["You can also do--", |
| 196 | "this or even---", |
| 197 | "this."] |
| 198 | self.check_wrap(text, 17, expect) |
| 199 | self.check_wrap(text, 19, expect) |
| 200 | expect = ["You can also do--this or even", |
| 201 | "---this."] |
| 202 | self.check_wrap(text, 29, expect) |
| 203 | self.check_wrap(text, 31, expect) |
| 204 | expect = ["You can also do--this or even---", |
| 205 | "this."] |
| 206 | self.check_wrap(text, 32, expect) |
| 207 | self.check_wrap(text, 35, expect) |
| 208 | |
| 209 | # All of the above behaviour could be deduced by probing the |
| 210 | # _split() method. |
| 211 | text = "Here's an -- em-dash and--here's another---and another!" |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 212 | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
| 213 | "and", "--", "here's", " ", "another", "---", |
| 214 | "and", " ", "another!"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 215 | self.check_split(text, expect) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 216 | |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 217 | text = "and then--bam!--he was gone" |
| 218 | expect = ["and", " ", "then", "--", "bam!", "--", |
| 219 | "he", " ", "was", " ", "gone"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 220 | self.check_split(text, expect) |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 221 | |
| 222 | |
Greg Ward | 34f995b | 2002-08-22 21:10:07 +0000 | [diff] [blame] | 223 | def test_unix_options (self): |
| 224 | # Test that Unix-style command-line options are wrapped correctly. |
| 225 | # Both Optik (OptionParser) and Docutils rely on this behaviour! |
| 226 | |
| 227 | text = "You should use the -n option, or --dry-run in its long form." |
| 228 | self.check_wrap(text, 20, |
| 229 | ["You should use the", |
| 230 | "-n option, or --dry-", |
| 231 | "run in its long", |
| 232 | "form."]) |
| 233 | self.check_wrap(text, 21, |
| 234 | ["You should use the -n", |
| 235 | "option, or --dry-run", |
| 236 | "in its long form."]) |
| 237 | expect = ["You should use the -n option, or", |
| 238 | "--dry-run in its long form."] |
| 239 | self.check_wrap(text, 32, expect) |
| 240 | self.check_wrap(text, 34, expect) |
| 241 | self.check_wrap(text, 35, expect) |
| 242 | self.check_wrap(text, 38, expect) |
| 243 | expect = ["You should use the -n option, or --dry-", |
| 244 | "run in its long form."] |
| 245 | self.check_wrap(text, 39, expect) |
| 246 | self.check_wrap(text, 41, expect) |
| 247 | expect = ["You should use the -n option, or --dry-run", |
| 248 | "in its long form."] |
| 249 | self.check_wrap(text, 42, expect) |
| 250 | |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 251 | # Again, all of the above can be deduced from _split(). |
| 252 | text = "the -n option, or --dry-run or --dryrun" |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 253 | expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", |
| 254 | "--dry-", "run", " ", "or", " ", "--dryrun"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 255 | self.check_split(text, expect) |
| 256 | |
| 257 | def test_funky_hyphens (self): |
| 258 | # Screwy edge cases cooked up by David Goodger. All reported |
| 259 | # in SF bug #596434. |
| 260 | self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"]) |
| 261 | self.check_split("what the--", ["what", " ", "the--"]) |
| 262 | self.check_split("what the--.", ["what", " ", "the--."]) |
| 263 | self.check_split("--text--.", ["--text--."]) |
| 264 | |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 265 | # When I first read bug #596434, this is what I thought David |
| 266 | # was talking about. I was wrong; these have always worked |
| 267 | # fine. The real problem is tested in test_funky_parens() |
| 268 | # below... |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 269 | self.check_split("--option", ["--option"]) |
| 270 | self.check_split("--option-opt", ["--option-", "opt"]) |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 271 | self.check_split("foo --option-opt bar", |
| 272 | ["foo", " ", "--option-", "opt", " ", "bar"]) |
| 273 | |
Greg Ward | 6186410 | 2004-06-03 01:59:41 +0000 | [diff] [blame] | 274 | def test_punct_hyphens(self): |
| 275 | # Oh bother, SF #965425 found another problem with hyphens -- |
| 276 | # hyphenated words in single quotes weren't handled correctly. |
| 277 | # In fact, the bug is that *any* punctuation around a hyphenated |
| 278 | # word was handled incorrectly, except for a leading "--", which |
| 279 | # was special-cased for Optik and Docutils. So test a variety |
| 280 | # of styles of punctuation around a hyphenated word. |
| 281 | # (Actually this is based on an Optik bug report, #813077). |
| 282 | self.check_split("the 'wibble-wobble' widget", |
| 283 | ['the', ' ', "'wibble-", "wobble'", ' ', 'widget']) |
| 284 | self.check_split('the "wibble-wobble" widget', |
| 285 | ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget']) |
| 286 | self.check_split("the (wibble-wobble) widget", |
| 287 | ['the', ' ', "(wibble-", "wobble)", ' ', 'widget']) |
| 288 | self.check_split("the ['wibble-wobble'] widget", |
| 289 | ['the', ' ', "['wibble-", "wobble']", ' ', 'widget']) |
| 290 | |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 291 | def test_funky_parens (self): |
| 292 | # Second part of SF bug #596434: long option strings inside |
| 293 | # parentheses. |
| 294 | self.check_split("foo (--option) bar", |
| 295 | ["foo", " ", "(--option)", " ", "bar"]) |
| 296 | |
| 297 | # Related stuff -- make sure parens work in simpler contexts. |
| 298 | self.check_split("foo (bar) baz", |
| 299 | ["foo", " ", "(bar)", " ", "baz"]) |
| 300 | self.check_split("blah (ding dong), wubba", |
| 301 | ["blah", " ", "(ding", " ", "dong),", |
| 302 | " ", "wubba"]) |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 303 | |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 304 | def test_initial_whitespace(self): |
| 305 | # SF bug #622849 reported inconsistent handling of leading |
| 306 | # whitespace; let's test that a bit, shall we? |
| 307 | text = " This is a sentence with leading whitespace." |
| 308 | self.check_wrap(text, 50, |
| 309 | [" This is a sentence with leading whitespace."]) |
| 310 | self.check_wrap(text, 30, |
| 311 | [" This is a sentence with", "leading whitespace."]) |
| 312 | |
Greg Ward | c7e3c5e | 2002-12-09 16:32:41 +0000 | [diff] [blame] | 313 | def test_unicode(self): |
| 314 | # *Very* simple test of wrapping Unicode strings. I'm sure |
| 315 | # there's more to it than this, but let's at least make |
| 316 | # sure textwrap doesn't crash on Unicode input! |
| 317 | text = u"Hello there, how are you today?" |
| 318 | self.check_wrap(text, 50, [u"Hello there, how are you today?"]) |
| 319 | self.check_wrap(text, 20, [u"Hello there, how are", "you today?"]) |
| 320 | olines = self.wrapper.wrap(text) |
| 321 | assert isinstance(olines, list) and isinstance(olines[0], unicode) |
| 322 | otext = self.wrapper.fill(text) |
| 323 | assert isinstance(otext, unicode) |
| 324 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 325 | def test_split(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 326 | # Ensure that the standard _split() method works as advertised |
| 327 | # in the comments |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 328 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 329 | text = "Hello there -- you goof-ball, use the -b option!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 330 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 331 | result = self.wrapper._split(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 332 | self.check(result, |
| 333 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 334 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 335 | |
Greg Ward | 4912857 | 2003-05-07 00:54:42 +0000 | [diff] [blame] | 336 | def test_bad_width(self): |
| 337 | # Ensure that width <= 0 is caught. |
| 338 | text = "Whatever, it doesn't matter." |
| 339 | self.assertRaises(ValueError, wrap, text, 0) |
| 340 | self.assertRaises(ValueError, wrap, text, -1) |
| 341 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 342 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 343 | class LongWordTestCase (BaseTestCase): |
| 344 | def setUp(self): |
| 345 | self.wrapper = TextWrapper() |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 346 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 347 | Did you say "supercalifragilisticexpialidocious?" |
| 348 | How *do* you spell that odd word, anyways? |
| 349 | ''' |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 350 | |
| 351 | def test_break_long(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 352 | # Wrap text with long words and lots of punctuation |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 353 | |
| 354 | self.check_wrap(self.text, 30, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 355 | ['Did you say "supercalifragilis', |
| 356 | 'ticexpialidocious?" How *do*', |
| 357 | 'you spell that odd word,', |
| 358 | 'anyways?']) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 359 | self.check_wrap(self.text, 50, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 360 | ['Did you say "supercalifragilisticexpialidocious?"', |
| 361 | 'How *do* you spell that odd word, anyways?']) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 362 | |
Raymond Hettinger | c11dbcd | 2003-08-30 14:43:55 +0000 | [diff] [blame] | 363 | # SF bug 797650. Prevent an infinite loop by making sure that at |
| 364 | # least one character gets split off on every pass. |
| 365 | self.check_wrap('-'*10+'hello', 10, |
| 366 | ['----------', |
| 367 | ' h', |
| 368 | ' e', |
| 369 | ' l', |
| 370 | ' l', |
| 371 | ' o'], |
| 372 | subsequent_indent = ' '*15) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 374 | def test_nobreak_long(self): |
| 375 | # Test with break_long_words disabled |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 376 | self.wrapper.break_long_words = 0 |
| 377 | self.wrapper.width = 30 |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 378 | expect = ['Did you say', |
| 379 | '"supercalifragilisticexpialidocious?"', |
| 380 | 'How *do* you spell that odd', |
| 381 | 'word, anyways?' |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 382 | ] |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 383 | result = self.wrapper.wrap(self.text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 384 | self.check(result, expect) |
| 385 | |
| 386 | # Same thing with kwargs passed to standalone wrap() function. |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 387 | result = wrap(self.text, width=30, break_long_words=0) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 388 | self.check(result, expect) |
| 389 | |
| 390 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 391 | class IndentTestCases(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 392 | |
| 393 | # called before each test method |
| 394 | def setUp(self): |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 395 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 396 | This paragraph will be filled, first without any indentation, |
| 397 | and then with some (including a hanging indent).''' |
| 398 | |
| 399 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 400 | def test_fill(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 401 | # Test the fill() method |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 402 | |
| 403 | expect = '''\ |
| 404 | This paragraph will be filled, first |
| 405 | without any indentation, and then with |
| 406 | some (including a hanging indent).''' |
| 407 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 408 | result = fill(self.text, 40) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 409 | self.check(result, expect) |
| 410 | |
| 411 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 412 | def test_initial_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 413 | # Test initial_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 414 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 415 | expect = [" This paragraph will be filled,", |
| 416 | "first without any indentation, and then", |
| 417 | "with some (including a hanging indent)."] |
| 418 | result = wrap(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 419 | self.check(result, expect) |
| 420 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 421 | expect = "\n".join(expect) |
| 422 | result = fill(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 423 | self.check(result, expect) |
| 424 | |
| 425 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 426 | def test_subsequent_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 427 | # Test subsequent_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 428 | |
| 429 | expect = '''\ |
| 430 | * This paragraph will be filled, first |
| 431 | without any indentation, and then |
| 432 | with some (including a hanging |
| 433 | indent).''' |
| 434 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 435 | result = fill(self.text, 40, |
| 436 | initial_indent=" * ", subsequent_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 437 | self.check(result, expect) |
| 438 | |
| 439 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 440 | # Despite the similar names, DedentTestCase is *not* the inverse |
| 441 | # of IndentTestCase! |
| 442 | class DedentTestCase(unittest.TestCase): |
| 443 | |
| 444 | def test_dedent_nomargin(self): |
| 445 | # No lines indented. |
| 446 | text = "Hello there.\nHow are you?\nOh good, I'm glad." |
| 447 | self.assertEquals(dedent(text), text) |
| 448 | |
| 449 | # Similar, with a blank line. |
| 450 | text = "Hello there.\n\nBoo!" |
| 451 | self.assertEquals(dedent(text), text) |
| 452 | |
| 453 | # Some lines indented, but overall margin is still zero. |
| 454 | text = "Hello there.\n This is indented." |
| 455 | self.assertEquals(dedent(text), text) |
| 456 | |
| 457 | # Again, add a blank line. |
| 458 | text = "Hello there.\n\n Boo!\n" |
| 459 | self.assertEquals(dedent(text), text) |
| 460 | |
| 461 | def test_dedent_even(self): |
| 462 | # All lines indented by two spaces. |
| 463 | text = " Hello there.\n How are ya?\n Oh good." |
| 464 | expect = "Hello there.\nHow are ya?\nOh good." |
| 465 | self.assertEquals(dedent(text), expect) |
| 466 | |
| 467 | # Same, with blank lines. |
| 468 | text = " Hello there.\n\n How are ya?\n Oh good.\n" |
| 469 | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
| 470 | self.assertEquals(dedent(text), expect) |
| 471 | |
| 472 | # Now indent one of the blank lines. |
| 473 | text = " Hello there.\n \n How are ya?\n Oh good.\n" |
| 474 | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
| 475 | self.assertEquals(dedent(text), expect) |
| 476 | |
| 477 | def test_dedent_uneven(self): |
| 478 | # Lines indented unevenly. |
| 479 | text = '''\ |
| 480 | def foo(): |
| 481 | while 1: |
| 482 | return foo |
| 483 | ''' |
| 484 | expect = '''\ |
| 485 | def foo(): |
| 486 | while 1: |
| 487 | return foo |
| 488 | ''' |
| 489 | self.assertEquals(dedent(text), expect) |
| 490 | |
| 491 | # Uneven indentation with a blank line. |
| 492 | text = " Foo\n Bar\n\n Baz\n" |
| 493 | expect = "Foo\n Bar\n\n Baz\n" |
| 494 | self.assertEquals(dedent(text), expect) |
| 495 | |
| 496 | # Uneven indentation with a whitespace-only line. |
| 497 | text = " Foo\n Bar\n \n Baz\n" |
| 498 | expect = "Foo\n Bar\n\n Baz\n" |
| 499 | self.assertEquals(dedent(text), expect) |
| 500 | |
| 501 | |
| 502 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 503 | def test_main(): |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 504 | test_support.run_unittest(WrapTestCase, |
| 505 | LongWordTestCase, |
| 506 | IndentTestCases, |
| 507 | DedentTestCase) |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 508 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 509 | if __name__ == '__main__': |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 510 | test_main() |