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 | |
Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 14 | from textwrap import TextWrapper, wrap, fill, dedent, indent |
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 | |
Hynek Schlawack | d527259 | 2012-05-19 13:33:11 +0200 | [diff] [blame] | 94 | 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 Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 102 | def test_fix_sentence_endings(self): |
| 103 | wrapper = TextWrapper(60, fix_sentence_endings=True) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 104 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 105 | # 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 Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 139 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 140 | text = 'File stdio.h is nice.' |
| 141 | expect = ['File stdio.h is nice.'] |
| 142 | self.check(wrapper.wrap(text), expect) |
| 143 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 144 | def test_wrap_short(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 145 | # Wrapping to make short lines longer |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 146 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 147 | text = "This is a\nshort paragraph." |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 148 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 149 | self.check_wrap(text, 20, ["This is a short", |
| 150 | "paragraph."]) |
| 151 | self.check_wrap(text, 40, ["This is a short paragraph."]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 152 | |
| 153 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 154 | 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 Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 164 | def test_hyphenated(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 165 | # Test breaking hyphenated words |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 166 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 167 | text = ("this-is-a-useful-feature-for-" |
| 168 | "reformatting-posts-from-tim-peters'ly") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 169 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 170 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 179 | |
Greg Ward | 4040794 | 2005-03-05 02:53:17 +0000 | [diff] [blame] | 180 | 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 Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 198 | def test_em_dash(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 199 | # Test text with em-dashes |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 200 | 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 Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 216 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 217 | # 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 Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 242 | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
| 243 | "and", "--", "here's", " ", "another", "---", |
| 244 | "and", " ", "another!"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 245 | self.check_split(text, expect) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 246 | |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 247 | text = "and then--bam!--he was gone" |
| 248 | expect = ["and", " ", "then", "--", "bam!", "--", |
| 249 | "he", " ", "was", " ", "gone"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 250 | self.check_split(text, expect) |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 251 | |
| 252 | |
Greg Ward | 34f995b | 2002-08-22 21:10:07 +0000 | [diff] [blame] | 253 | 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 Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 281 | # Again, all of the above can be deduced from _split(). |
| 282 | text = "the -n option, or --dry-run or --dryrun" |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 283 | expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", |
| 284 | "--dry-", "run", " ", "or", " ", "--dryrun"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 285 | 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 Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 295 | # 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 Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 299 | self.check_split("--option", ["--option"]) |
| 300 | self.check_split("--option-opt", ["--option-", "opt"]) |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 301 | self.check_split("foo --option-opt bar", |
| 302 | ["foo", " ", "--option-", "opt", " ", "bar"]) |
| 303 | |
Greg Ward | 6186410 | 2004-06-03 01:59:41 +0000 | [diff] [blame] | 304 | 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 Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 321 | 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 Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 333 | |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 334 | 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 Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 343 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 351 | def test_split(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 352 | # Ensure that the standard _split() method works as advertised |
| 353 | # in the comments |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 354 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 355 | text = "Hello there -- you goof-ball, use the -b option!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 356 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 357 | result = self.wrapper._split(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 358 | self.check(result, |
| 359 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 360 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 361 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 362 | 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 Ward | 4912857 | 2003-05-07 00:54:42 +0000 | [diff] [blame] | 370 | 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 Pitrou | 7c59bc6 | 2008-12-13 23:20:54 +0000 | [diff] [blame] | 376 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 384 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 385 | class LongWordTestCase (BaseTestCase): |
| 386 | def setUp(self): |
| 387 | self.wrapper = TextWrapper() |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 388 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 389 | Did you say "supercalifragilisticexpialidocious?" |
| 390 | How *do* you spell that odd word, anyways? |
| 391 | ''' |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 392 | |
| 393 | def test_break_long(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 394 | # Wrap text with long words and lots of punctuation |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 395 | |
| 396 | self.check_wrap(self.text, 30, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 397 | ['Did you say "supercalifragilis', |
| 398 | 'ticexpialidocious?" How *do*', |
| 399 | 'you spell that odd word,', |
| 400 | 'anyways?']) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 401 | self.check_wrap(self.text, 50, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 402 | ['Did you say "supercalifragilisticexpialidocious?"', |
| 403 | 'How *do* you spell that odd word, anyways?']) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 404 | |
Raymond Hettinger | c11dbcd | 2003-08-30 14:43:55 +0000 | [diff] [blame] | 405 | # 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 415 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 416 | # 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 Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 429 | def test_nobreak_long(self): |
| 430 | # Test with break_long_words disabled |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 431 | self.wrapper.break_long_words = 0 |
| 432 | self.wrapper.width = 30 |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 433 | expect = ['Did you say', |
| 434 | '"supercalifragilisticexpialidocious?"', |
| 435 | 'How *do* you spell that odd', |
| 436 | 'word, anyways?' |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 437 | ] |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 438 | result = self.wrapper.wrap(self.text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 439 | self.check(result, expect) |
| 440 | |
| 441 | # Same thing with kwargs passed to standalone wrap() function. |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 442 | result = wrap(self.text, width=30, break_long_words=0) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 443 | self.check(result, expect) |
| 444 | |
| 445 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 446 | class IndentTestCases(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 447 | |
| 448 | # called before each test method |
| 449 | def setUp(self): |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 450 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 451 | This paragraph will be filled, first without any indentation, |
| 452 | and then with some (including a hanging indent).''' |
| 453 | |
| 454 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 455 | def test_fill(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 456 | # Test the fill() method |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 457 | |
| 458 | expect = '''\ |
| 459 | This paragraph will be filled, first |
| 460 | without any indentation, and then with |
| 461 | some (including a hanging indent).''' |
| 462 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 463 | result = fill(self.text, 40) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 464 | self.check(result, expect) |
| 465 | |
| 466 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 467 | def test_initial_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 468 | # Test initial_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 469 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 470 | 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 Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 474 | self.check(result, expect) |
| 475 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 476 | expect = "\n".join(expect) |
| 477 | result = fill(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 478 | self.check(result, expect) |
| 479 | |
| 480 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 481 | def test_subsequent_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 482 | # Test subsequent_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 483 | |
| 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 Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 490 | result = fill(self.text, 40, |
| 491 | initial_indent=" * ", subsequent_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 492 | self.check(result, expect) |
| 493 | |
| 494 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 495 | # Despite the similar names, DedentTestCase is *not* the inverse |
| 496 | # of IndentTestCase! |
| 497 | class DedentTestCase(unittest.TestCase): |
| 498 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 499 | def assertUnchanged(self, text): |
| 500 | """assert that dedent() has no effect on 'text'""" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 501 | self.assertEqual(text, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 502 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 503 | def test_dedent_nomargin(self): |
| 504 | # No lines indented. |
| 505 | text = "Hello there.\nHow are you?\nOh good, I'm glad." |
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 | # Similar, with a blank line. |
| 509 | text = "Hello there.\n\nBoo!" |
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 | # Some lines indented, but overall margin is still zero. |
| 513 | text = "Hello there.\n This is indented." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 514 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 515 | |
| 516 | # Again, add a blank line. |
| 517 | text = "Hello there.\n\n Boo!\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 518 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 519 | |
| 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 524 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 525 | |
| 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 529 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 530 | |
| 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 534 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 535 | |
| 536 | def test_dedent_uneven(self): |
| 537 | # Lines indented unevenly. |
| 538 | text = '''\ |
| 539 | def foo(): |
| 540 | while 1: |
| 541 | return foo |
| 542 | ''' |
| 543 | expect = '''\ |
| 544 | def foo(): |
| 545 | while 1: |
| 546 | return foo |
| 547 | ''' |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 548 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 549 | |
| 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 553 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 554 | |
| 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 558 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 559 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 560 | # 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 564 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 565 | |
| 566 | # make sure that it preserves tabs when it's not making any |
| 567 | # changes at all |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 568 | self.assertEqual(expect, dedent(expect)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 569 | |
| 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 584 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 585 | |
| 586 | text = " \thello there\n \thow are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 587 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 588 | |
| 589 | text = " \t hello there\n \t how are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 590 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 591 | |
| 592 | text = " \thello there\n \t how are you?" |
| 593 | expect = "hello there\n how are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 594 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 595 | |
| 596 | |
Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 597 | # Test textwrap.indent |
| 598 | class IndentTestCase(unittest.TestCase): |
| 599 | # The examples used for tests. If any of these change, the expected |
| 600 | # results in the various test cases must also be updated. |
| 601 | # The roundtrip cases are separate, because textwrap.dedent doesn't |
| 602 | # handle Windows line endings |
| 603 | ROUNDTRIP_CASES = ( |
| 604 | # Basic test case |
| 605 | "Hi.\nThis is a test.\nTesting.", |
| 606 | # Include a blank line |
| 607 | "Hi.\nThis is a test.\n\nTesting.", |
| 608 | # Include leading and trailing blank lines |
| 609 | "\nHi.\nThis is a test.\nTesting.\n", |
| 610 | ) |
| 611 | CASES = ROUNDTRIP_CASES + ( |
| 612 | # Use Windows line endings |
| 613 | "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
| 614 | # Pathological case |
| 615 | "\nHi.\r\nThis is a test.\n\r\nTesting.\r\n\n", |
| 616 | ) |
| 617 | |
| 618 | def test_indent_nomargin_default(self): |
| 619 | # indent should do nothing if 'prefix' is empty. |
| 620 | for text in self.CASES: |
| 621 | self.assertEqual(indent(text, ''), text) |
| 622 | |
| 623 | def test_indent_nomargin_explicit_default(self): |
| 624 | # The same as test_indent_nomargin, but explicitly requesting |
| 625 | # the default behaviour by passing None as the predicate |
| 626 | for text in self.CASES: |
| 627 | self.assertEqual(indent(text, '', None), text) |
| 628 | |
| 629 | def test_indent_nomargin_all_lines(self): |
| 630 | # The same as test_indent_nomargin, but using the optional |
| 631 | # predicate argument |
| 632 | predicate = lambda line: True |
| 633 | for text in self.CASES: |
| 634 | self.assertEqual(indent(text, '', predicate), text) |
| 635 | |
| 636 | def test_indent_no_lines(self): |
| 637 | # Explicitly skip indenting any lines |
| 638 | predicate = lambda line: False |
| 639 | for text in self.CASES: |
| 640 | self.assertEqual(indent(text, ' ', predicate), text) |
| 641 | |
| 642 | def test_roundtrip_spaces(self): |
| 643 | # A whitespace prefix should roundtrip with dedent |
| 644 | for text in self.ROUNDTRIP_CASES: |
| 645 | self.assertEqual(dedent(indent(text, ' ')), text) |
| 646 | |
| 647 | def test_roundtrip_tabs(self): |
| 648 | # A whitespace prefix should roundtrip with dedent |
| 649 | for text in self.ROUNDTRIP_CASES: |
| 650 | self.assertEqual(dedent(indent(text, '\t\t')), text) |
| 651 | |
| 652 | def test_roundtrip_mixed(self): |
| 653 | # A whitespace prefix should roundtrip with dedent |
| 654 | for text in self.ROUNDTRIP_CASES: |
| 655 | self.assertEqual(dedent(indent(text, ' \t \t ')), text) |
| 656 | |
| 657 | def test_indent_default(self): |
| 658 | # Test default indenting of lines that are not whitespace only |
| 659 | prefix = ' ' |
| 660 | expected = ( |
| 661 | # Basic test case |
| 662 | " Hi.\n This is a test.\n Testing.", |
| 663 | # Include a blank line |
| 664 | " Hi.\n This is a test.\n\n Testing.", |
| 665 | # Include leading and trailing blank lines |
| 666 | "\n Hi.\n This is a test.\n Testing.\n", |
| 667 | # Use Windows line endings |
| 668 | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 669 | # Pathological case |
| 670 | "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
| 671 | ) |
| 672 | for text, expect in zip(self.CASES, expected): |
| 673 | self.assertEqual(indent(text, prefix), expect) |
| 674 | |
| 675 | def test_indent_explicit_default(self): |
| 676 | # Test default indenting of lines that are not whitespace only |
| 677 | prefix = ' ' |
| 678 | expected = ( |
| 679 | # Basic test case |
| 680 | " Hi.\n This is a test.\n Testing.", |
| 681 | # Include a blank line |
| 682 | " Hi.\n This is a test.\n\n Testing.", |
| 683 | # Include leading and trailing blank lines |
| 684 | "\n Hi.\n This is a test.\n Testing.\n", |
| 685 | # Use Windows line endings |
| 686 | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 687 | # Pathological case |
| 688 | "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
| 689 | ) |
| 690 | for text, expect in zip(self.CASES, expected): |
| 691 | self.assertEqual(indent(text, prefix, None), expect) |
| 692 | |
| 693 | def test_indent_all_lines(self): |
| 694 | # Add 'prefix' to all lines, including whitespace-only ones. |
| 695 | prefix = ' ' |
| 696 | expected = ( |
| 697 | # Basic test case |
| 698 | " Hi.\n This is a test.\n Testing.", |
| 699 | # Include a blank line |
| 700 | " Hi.\n This is a test.\n \n Testing.", |
| 701 | # Include leading and trailing blank lines |
| 702 | " \n Hi.\n This is a test.\n Testing.\n", |
| 703 | # Use Windows line endings |
| 704 | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 705 | # Pathological case |
| 706 | " \n Hi.\r\n This is a test.\n \r\n Testing.\r\n \n", |
| 707 | ) |
| 708 | predicate = lambda line: True |
| 709 | for text, expect in zip(self.CASES, expected): |
| 710 | self.assertEqual(indent(text, prefix, predicate), expect) |
| 711 | |
| 712 | def test_indent_empty_lines(self): |
| 713 | # Add 'prefix' solely to whitespace-only lines. |
| 714 | prefix = ' ' |
| 715 | expected = ( |
| 716 | # Basic test case |
| 717 | "Hi.\nThis is a test.\nTesting.", |
| 718 | # Include a blank line |
| 719 | "Hi.\nThis is a test.\n \nTesting.", |
| 720 | # Include leading and trailing blank lines |
| 721 | " \nHi.\nThis is a test.\nTesting.\n", |
| 722 | # Use Windows line endings |
| 723 | "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
| 724 | # Pathological case |
| 725 | " \nHi.\r\nThis is a test.\n \r\nTesting.\r\n \n", |
| 726 | ) |
| 727 | predicate = lambda line: not line.strip() |
| 728 | for text, expect in zip(self.CASES, expected): |
| 729 | self.assertEqual(indent(text, prefix, predicate), expect) |
| 730 | |
| 731 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 732 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 733 | support.run_unittest(WrapTestCase, |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 734 | LongWordTestCase, |
| 735 | IndentTestCases, |
Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 736 | DedentTestCase, |
| 737 | IndentTestCase) |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 738 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 739 | if __name__ == '__main__': |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 740 | test_main() |