| 1 | n/a | # |
|---|
| 2 | n/a | # Test suite for the textwrap module. |
|---|
| 3 | n/a | # |
|---|
| 4 | n/a | # Original tests written by Greg Ward <gward@python.net>. |
|---|
| 5 | n/a | # Converted to PyUnit by Peter Hansen <peter@engcorp.com>. |
|---|
| 6 | n/a | # Currently maintained by Greg Ward. |
|---|
| 7 | n/a | # |
|---|
| 8 | n/a | # $Id$ |
|---|
| 9 | n/a | # |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | import unittest |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | from textwrap import TextWrapper, wrap, fill, dedent, indent, shorten |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | class BaseTestCase(unittest.TestCase): |
|---|
| 17 | n/a | '''Parent class with utility methods for textwrap tests.''' |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def show(self, textin): |
|---|
| 20 | n/a | if isinstance(textin, list): |
|---|
| 21 | n/a | result = [] |
|---|
| 22 | n/a | for i in range(len(textin)): |
|---|
| 23 | n/a | result.append(" %d: %r" % (i, textin[i])) |
|---|
| 24 | n/a | result = "\n".join(result) if result else " no lines" |
|---|
| 25 | n/a | elif isinstance(textin, str): |
|---|
| 26 | n/a | result = " %s\n" % repr(textin) |
|---|
| 27 | n/a | return result |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | def check(self, result, expect): |
|---|
| 31 | n/a | self.assertEqual(result, expect, |
|---|
| 32 | n/a | 'expected:\n%s\nbut got:\n%s' % ( |
|---|
| 33 | n/a | self.show(expect), self.show(result))) |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | def check_wrap(self, text, width, expect, **kwargs): |
|---|
| 36 | n/a | result = wrap(text, width, **kwargs) |
|---|
| 37 | n/a | self.check(result, expect) |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def check_split(self, text, expect): |
|---|
| 40 | n/a | result = self.wrapper._split(text) |
|---|
| 41 | n/a | self.assertEqual(result, expect, |
|---|
| 42 | n/a | "\nexpected %r\n" |
|---|
| 43 | n/a | "but got %r" % (expect, result)) |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | class WrapTestCase(BaseTestCase): |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | def setUp(self): |
|---|
| 49 | n/a | self.wrapper = TextWrapper(width=45) |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | def test_simple(self): |
|---|
| 52 | n/a | # Simple case: just words, spaces, and a bit of punctuation |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | self.check_wrap(text, 12, |
|---|
| 57 | n/a | ["Hello there,", |
|---|
| 58 | n/a | "how are you", |
|---|
| 59 | n/a | "this fine", |
|---|
| 60 | n/a | "day? I'm", |
|---|
| 61 | n/a | "glad to hear", |
|---|
| 62 | n/a | "it!"]) |
|---|
| 63 | n/a | self.check_wrap(text, 42, |
|---|
| 64 | n/a | ["Hello there, how are you this fine day?", |
|---|
| 65 | n/a | "I'm glad to hear it!"]) |
|---|
| 66 | n/a | self.check_wrap(text, 80, [text]) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def test_empty_string(self): |
|---|
| 69 | n/a | # Check that wrapping the empty string returns an empty list. |
|---|
| 70 | n/a | self.check_wrap("", 6, []) |
|---|
| 71 | n/a | self.check_wrap("", 6, [], drop_whitespace=False) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def test_empty_string_with_initial_indent(self): |
|---|
| 74 | n/a | # Check that the empty string is not indented. |
|---|
| 75 | n/a | self.check_wrap("", 6, [], initial_indent="++") |
|---|
| 76 | n/a | self.check_wrap("", 6, [], initial_indent="++", drop_whitespace=False) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def test_whitespace(self): |
|---|
| 79 | n/a | # Whitespace munging and end-of-sentence detection |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | text = """\ |
|---|
| 82 | n/a | This is a paragraph that already has |
|---|
| 83 | n/a | line breaks. But some of its lines are much longer than the others, |
|---|
| 84 | n/a | so it needs to be wrapped. |
|---|
| 85 | n/a | Some lines are \ttabbed too. |
|---|
| 86 | n/a | What a mess! |
|---|
| 87 | n/a | """ |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | expect = ["This is a paragraph that already has line", |
|---|
| 90 | n/a | "breaks. But some of its lines are much", |
|---|
| 91 | n/a | "longer than the others, so it needs to be", |
|---|
| 92 | n/a | "wrapped. Some lines are tabbed too. What a", |
|---|
| 93 | n/a | "mess!"] |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | wrapper = TextWrapper(45, fix_sentence_endings=True) |
|---|
| 96 | n/a | result = wrapper.wrap(text) |
|---|
| 97 | n/a | self.check(result, expect) |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | result = wrapper.fill(text) |
|---|
| 100 | n/a | self.check(result, '\n'.join(expect)) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | text = "\tTest\tdefault\t\ttabsize." |
|---|
| 103 | n/a | expect = [" Test default tabsize."] |
|---|
| 104 | n/a | self.check_wrap(text, 80, expect) |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | text = "\tTest\tcustom\t\ttabsize." |
|---|
| 107 | n/a | expect = [" Test custom tabsize."] |
|---|
| 108 | n/a | self.check_wrap(text, 80, expect, tabsize=4) |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def test_fix_sentence_endings(self): |
|---|
| 111 | n/a | wrapper = TextWrapper(60, fix_sentence_endings=True) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | # SF #847346: ensure that fix_sentence_endings=True does the |
|---|
| 114 | n/a | # right thing even on input short enough that it doesn't need to |
|---|
| 115 | n/a | # be wrapped. |
|---|
| 116 | n/a | text = "A short line. Note the single space." |
|---|
| 117 | n/a | expect = ["A short line. Note the single space."] |
|---|
| 118 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | # Test some of the hairy end cases that _fix_sentence_endings() |
|---|
| 121 | n/a | # is supposed to handle (the easy stuff is tested in |
|---|
| 122 | n/a | # test_whitespace() above). |
|---|
| 123 | n/a | text = "Well, Doctor? What do you think?" |
|---|
| 124 | n/a | expect = ["Well, Doctor? What do you think?"] |
|---|
| 125 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | text = "Well, Doctor?\nWhat do you think?" |
|---|
| 128 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | text = 'I say, chaps! Anyone for "tennis?"\nHmmph!' |
|---|
| 131 | n/a | expect = ['I say, chaps! Anyone for "tennis?" Hmmph!'] |
|---|
| 132 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | wrapper.width = 20 |
|---|
| 135 | n/a | expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!'] |
|---|
| 136 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | text = 'And she said, "Go to hell!"\nCan you believe that?' |
|---|
| 139 | n/a | expect = ['And she said, "Go to', |
|---|
| 140 | n/a | 'hell!" Can you', |
|---|
| 141 | n/a | 'believe that?'] |
|---|
| 142 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | wrapper.width = 60 |
|---|
| 145 | n/a | expect = ['And she said, "Go to hell!" Can you believe that?'] |
|---|
| 146 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | text = 'File stdio.h is nice.' |
|---|
| 149 | n/a | expect = ['File stdio.h is nice.'] |
|---|
| 150 | n/a | self.check(wrapper.wrap(text), expect) |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | def test_wrap_short(self): |
|---|
| 153 | n/a | # Wrapping to make short lines longer |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | text = "This is a\nshort paragraph." |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | self.check_wrap(text, 20, ["This is a short", |
|---|
| 158 | n/a | "paragraph."]) |
|---|
| 159 | n/a | self.check_wrap(text, 40, ["This is a short paragraph."]) |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | def test_wrap_short_1line(self): |
|---|
| 163 | n/a | # Test endcases |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | text = "This is a short line." |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | self.check_wrap(text, 30, ["This is a short line."]) |
|---|
| 168 | n/a | self.check_wrap(text, 30, ["(1) This is a short line."], |
|---|
| 169 | n/a | initial_indent="(1) ") |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def test_hyphenated(self): |
|---|
| 173 | n/a | # Test breaking hyphenated words |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | text = ("this-is-a-useful-feature-for-" |
|---|
| 176 | n/a | "reformatting-posts-from-tim-peters'ly") |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | self.check_wrap(text, 40, |
|---|
| 179 | n/a | ["this-is-a-useful-feature-for-", |
|---|
| 180 | n/a | "reformatting-posts-from-tim-peters'ly"]) |
|---|
| 181 | n/a | self.check_wrap(text, 41, |
|---|
| 182 | n/a | ["this-is-a-useful-feature-for-", |
|---|
| 183 | n/a | "reformatting-posts-from-tim-peters'ly"]) |
|---|
| 184 | n/a | self.check_wrap(text, 42, |
|---|
| 185 | n/a | ["this-is-a-useful-feature-for-reformatting-", |
|---|
| 186 | n/a | "posts-from-tim-peters'ly"]) |
|---|
| 187 | n/a | # The test tests current behavior but is not testing parts of the API. |
|---|
| 188 | n/a | expect = ("this-|is-|a-|useful-|feature-|for-|" |
|---|
| 189 | n/a | "reformatting-|posts-|from-|tim-|peters'ly").split('|') |
|---|
| 190 | n/a | self.check_wrap(text, 1, expect, break_long_words=False) |
|---|
| 191 | n/a | self.check_split(text, expect) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | self.check_split('e-mail', ['e-mail']) |
|---|
| 194 | n/a | self.check_split('Jelly-O', ['Jelly-O']) |
|---|
| 195 | n/a | # The test tests current behavior but is not testing parts of the API. |
|---|
| 196 | n/a | self.check_split('half-a-crown', 'half-|a-|crown'.split('|')) |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | def test_hyphenated_numbers(self): |
|---|
| 199 | n/a | # Test that hyphenated numbers (eg. dates) are not broken like words. |
|---|
| 200 | n/a | text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n" |
|---|
| 201 | n/a | "released on 1994-02-15.") |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | self.check_wrap(text, 30, ['Python 1.0.0 was released on', |
|---|
| 204 | n/a | '1994-01-26. Python 1.0.1 was', |
|---|
| 205 | n/a | 'released on 1994-02-15.']) |
|---|
| 206 | n/a | self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.', |
|---|
| 207 | n/a | 'Python 1.0.1 was released on 1994-02-15.']) |
|---|
| 208 | n/a | self.check_wrap(text, 1, text.split(), break_long_words=False) |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | text = "I do all my shopping at 7-11." |
|---|
| 211 | n/a | self.check_wrap(text, 25, ["I do all my shopping at", |
|---|
| 212 | n/a | "7-11."]) |
|---|
| 213 | n/a | self.check_wrap(text, 27, ["I do all my shopping at", |
|---|
| 214 | n/a | "7-11."]) |
|---|
| 215 | n/a | self.check_wrap(text, 29, ["I do all my shopping at 7-11."]) |
|---|
| 216 | n/a | self.check_wrap(text, 1, text.split(), break_long_words=False) |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | def test_em_dash(self): |
|---|
| 219 | n/a | # Test text with em-dashes |
|---|
| 220 | n/a | text = "Em-dashes should be written -- thus." |
|---|
| 221 | n/a | self.check_wrap(text, 25, |
|---|
| 222 | n/a | ["Em-dashes should be", |
|---|
| 223 | n/a | "written -- thus."]) |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | # Probe the boundaries of the properly written em-dash, |
|---|
| 226 | n/a | # ie. " -- ". |
|---|
| 227 | n/a | self.check_wrap(text, 29, |
|---|
| 228 | n/a | ["Em-dashes should be written", |
|---|
| 229 | n/a | "-- thus."]) |
|---|
| 230 | n/a | expect = ["Em-dashes should be written --", |
|---|
| 231 | n/a | "thus."] |
|---|
| 232 | n/a | self.check_wrap(text, 30, expect) |
|---|
| 233 | n/a | self.check_wrap(text, 35, expect) |
|---|
| 234 | n/a | self.check_wrap(text, 36, |
|---|
| 235 | n/a | ["Em-dashes should be written -- thus."]) |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | # The improperly written em-dash is handled too, because |
|---|
| 238 | n/a | # it's adjacent to non-whitespace on both sides. |
|---|
| 239 | n/a | text = "You can also do--this or even---this." |
|---|
| 240 | n/a | expect = ["You can also do", |
|---|
| 241 | n/a | "--this or even", |
|---|
| 242 | n/a | "---this."] |
|---|
| 243 | n/a | self.check_wrap(text, 15, expect) |
|---|
| 244 | n/a | self.check_wrap(text, 16, expect) |
|---|
| 245 | n/a | expect = ["You can also do--", |
|---|
| 246 | n/a | "this or even---", |
|---|
| 247 | n/a | "this."] |
|---|
| 248 | n/a | self.check_wrap(text, 17, expect) |
|---|
| 249 | n/a | self.check_wrap(text, 19, expect) |
|---|
| 250 | n/a | expect = ["You can also do--this or even", |
|---|
| 251 | n/a | "---this."] |
|---|
| 252 | n/a | self.check_wrap(text, 29, expect) |
|---|
| 253 | n/a | self.check_wrap(text, 31, expect) |
|---|
| 254 | n/a | expect = ["You can also do--this or even---", |
|---|
| 255 | n/a | "this."] |
|---|
| 256 | n/a | self.check_wrap(text, 32, expect) |
|---|
| 257 | n/a | self.check_wrap(text, 35, expect) |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | # All of the above behaviour could be deduced by probing the |
|---|
| 260 | n/a | # _split() method. |
|---|
| 261 | n/a | text = "Here's an -- em-dash and--here's another---and another!" |
|---|
| 262 | n/a | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
|---|
| 263 | n/a | "and", "--", "here's", " ", "another", "---", |
|---|
| 264 | n/a | "and", " ", "another!"] |
|---|
| 265 | n/a | self.check_split(text, expect) |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | text = "and then--bam!--he was gone" |
|---|
| 268 | n/a | expect = ["and", " ", "then", "--", "bam!", "--", |
|---|
| 269 | n/a | "he", " ", "was", " ", "gone"] |
|---|
| 270 | n/a | self.check_split(text, expect) |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | def test_unix_options (self): |
|---|
| 274 | n/a | # Test that Unix-style command-line options are wrapped correctly. |
|---|
| 275 | n/a | # Both Optik (OptionParser) and Docutils rely on this behaviour! |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | text = "You should use the -n option, or --dry-run in its long form." |
|---|
| 278 | n/a | self.check_wrap(text, 20, |
|---|
| 279 | n/a | ["You should use the", |
|---|
| 280 | n/a | "-n option, or --dry-", |
|---|
| 281 | n/a | "run in its long", |
|---|
| 282 | n/a | "form."]) |
|---|
| 283 | n/a | self.check_wrap(text, 21, |
|---|
| 284 | n/a | ["You should use the -n", |
|---|
| 285 | n/a | "option, or --dry-run", |
|---|
| 286 | n/a | "in its long form."]) |
|---|
| 287 | n/a | expect = ["You should use the -n option, or", |
|---|
| 288 | n/a | "--dry-run in its long form."] |
|---|
| 289 | n/a | self.check_wrap(text, 32, expect) |
|---|
| 290 | n/a | self.check_wrap(text, 34, expect) |
|---|
| 291 | n/a | self.check_wrap(text, 35, expect) |
|---|
| 292 | n/a | self.check_wrap(text, 38, expect) |
|---|
| 293 | n/a | expect = ["You should use the -n option, or --dry-", |
|---|
| 294 | n/a | "run in its long form."] |
|---|
| 295 | n/a | self.check_wrap(text, 39, expect) |
|---|
| 296 | n/a | self.check_wrap(text, 41, expect) |
|---|
| 297 | n/a | expect = ["You should use the -n option, or --dry-run", |
|---|
| 298 | n/a | "in its long form."] |
|---|
| 299 | n/a | self.check_wrap(text, 42, expect) |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | # Again, all of the above can be deduced from _split(). |
|---|
| 302 | n/a | text = "the -n option, or --dry-run or --dryrun" |
|---|
| 303 | n/a | expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", |
|---|
| 304 | n/a | "--dry-", "run", " ", "or", " ", "--dryrun"] |
|---|
| 305 | n/a | self.check_split(text, expect) |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | def test_funky_hyphens (self): |
|---|
| 308 | n/a | # Screwy edge cases cooked up by David Goodger. All reported |
|---|
| 309 | n/a | # in SF bug #596434. |
|---|
| 310 | n/a | self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"]) |
|---|
| 311 | n/a | self.check_split("what the--", ["what", " ", "the--"]) |
|---|
| 312 | n/a | self.check_split("what the--.", ["what", " ", "the--."]) |
|---|
| 313 | n/a | self.check_split("--text--.", ["--text--."]) |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | # When I first read bug #596434, this is what I thought David |
|---|
| 316 | n/a | # was talking about. I was wrong; these have always worked |
|---|
| 317 | n/a | # fine. The real problem is tested in test_funky_parens() |
|---|
| 318 | n/a | # below... |
|---|
| 319 | n/a | self.check_split("--option", ["--option"]) |
|---|
| 320 | n/a | self.check_split("--option-opt", ["--option-", "opt"]) |
|---|
| 321 | n/a | self.check_split("foo --option-opt bar", |
|---|
| 322 | n/a | ["foo", " ", "--option-", "opt", " ", "bar"]) |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | def test_punct_hyphens(self): |
|---|
| 325 | n/a | # Oh bother, SF #965425 found another problem with hyphens -- |
|---|
| 326 | n/a | # hyphenated words in single quotes weren't handled correctly. |
|---|
| 327 | n/a | # In fact, the bug is that *any* punctuation around a hyphenated |
|---|
| 328 | n/a | # word was handled incorrectly, except for a leading "--", which |
|---|
| 329 | n/a | # was special-cased for Optik and Docutils. So test a variety |
|---|
| 330 | n/a | # of styles of punctuation around a hyphenated word. |
|---|
| 331 | n/a | # (Actually this is based on an Optik bug report, #813077). |
|---|
| 332 | n/a | self.check_split("the 'wibble-wobble' widget", |
|---|
| 333 | n/a | ['the', ' ', "'wibble-", "wobble'", ' ', 'widget']) |
|---|
| 334 | n/a | self.check_split('the "wibble-wobble" widget', |
|---|
| 335 | n/a | ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget']) |
|---|
| 336 | n/a | self.check_split("the (wibble-wobble) widget", |
|---|
| 337 | n/a | ['the', ' ', "(wibble-", "wobble)", ' ', 'widget']) |
|---|
| 338 | n/a | self.check_split("the ['wibble-wobble'] widget", |
|---|
| 339 | n/a | ['the', ' ', "['wibble-", "wobble']", ' ', 'widget']) |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | # The test tests current behavior but is not testing parts of the API. |
|---|
| 342 | n/a | self.check_split("what-d'you-call-it.", |
|---|
| 343 | n/a | "what-d'you-|call-|it.".split('|')) |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | def test_funky_parens (self): |
|---|
| 346 | n/a | # Second part of SF bug #596434: long option strings inside |
|---|
| 347 | n/a | # parentheses. |
|---|
| 348 | n/a | self.check_split("foo (--option) bar", |
|---|
| 349 | n/a | ["foo", " ", "(--option)", " ", "bar"]) |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | # Related stuff -- make sure parens work in simpler contexts. |
|---|
| 352 | n/a | self.check_split("foo (bar) baz", |
|---|
| 353 | n/a | ["foo", " ", "(bar)", " ", "baz"]) |
|---|
| 354 | n/a | self.check_split("blah (ding dong), wubba", |
|---|
| 355 | n/a | ["blah", " ", "(ding", " ", "dong),", |
|---|
| 356 | n/a | " ", "wubba"]) |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | def test_drop_whitespace_false(self): |
|---|
| 359 | n/a | # Check that drop_whitespace=False preserves whitespace. |
|---|
| 360 | n/a | # SF patch #1581073 |
|---|
| 361 | n/a | text = " This is a sentence with much whitespace." |
|---|
| 362 | n/a | self.check_wrap(text, 10, |
|---|
| 363 | n/a | [" This is a", " ", "sentence ", |
|---|
| 364 | n/a | "with ", "much white", "space."], |
|---|
| 365 | n/a | drop_whitespace=False) |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | def test_drop_whitespace_false_whitespace_only(self): |
|---|
| 368 | n/a | # Check that drop_whitespace=False preserves a whitespace-only string. |
|---|
| 369 | n/a | self.check_wrap(" ", 6, [" "], drop_whitespace=False) |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | def test_drop_whitespace_false_whitespace_only_with_indent(self): |
|---|
| 372 | n/a | # Check that a whitespace-only string gets indented (when |
|---|
| 373 | n/a | # drop_whitespace is False). |
|---|
| 374 | n/a | self.check_wrap(" ", 6, [" "], drop_whitespace=False, |
|---|
| 375 | n/a | initial_indent=" ") |
|---|
| 376 | n/a | |
|---|
| 377 | n/a | def test_drop_whitespace_whitespace_only(self): |
|---|
| 378 | n/a | # Check drop_whitespace on a whitespace-only string. |
|---|
| 379 | n/a | self.check_wrap(" ", 6, []) |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | def test_drop_whitespace_leading_whitespace(self): |
|---|
| 382 | n/a | # Check that drop_whitespace does not drop leading whitespace (if |
|---|
| 383 | n/a | # followed by non-whitespace). |
|---|
| 384 | n/a | # SF bug #622849 reported inconsistent handling of leading |
|---|
| 385 | n/a | # whitespace; let's test that a bit, shall we? |
|---|
| 386 | n/a | text = " This is a sentence with leading whitespace." |
|---|
| 387 | n/a | self.check_wrap(text, 50, |
|---|
| 388 | n/a | [" This is a sentence with leading whitespace."]) |
|---|
| 389 | n/a | self.check_wrap(text, 30, |
|---|
| 390 | n/a | [" This is a sentence with", "leading whitespace."]) |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | def test_drop_whitespace_whitespace_line(self): |
|---|
| 393 | n/a | # Check that drop_whitespace skips the whole line if a non-leading |
|---|
| 394 | n/a | # line consists only of whitespace. |
|---|
| 395 | n/a | text = "abcd efgh" |
|---|
| 396 | n/a | # Include the result for drop_whitespace=False for comparison. |
|---|
| 397 | n/a | self.check_wrap(text, 6, ["abcd", " ", "efgh"], |
|---|
| 398 | n/a | drop_whitespace=False) |
|---|
| 399 | n/a | self.check_wrap(text, 6, ["abcd", "efgh"]) |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | def test_drop_whitespace_whitespace_only_with_indent(self): |
|---|
| 402 | n/a | # Check that initial_indent is not applied to a whitespace-only |
|---|
| 403 | n/a | # string. This checks a special case of the fact that dropping |
|---|
| 404 | n/a | # whitespace occurs before indenting. |
|---|
| 405 | n/a | self.check_wrap(" ", 6, [], initial_indent="++") |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | def test_drop_whitespace_whitespace_indent(self): |
|---|
| 408 | n/a | # Check that drop_whitespace does not drop whitespace indents. |
|---|
| 409 | n/a | # This checks a special case of the fact that dropping whitespace |
|---|
| 410 | n/a | # occurs before indenting. |
|---|
| 411 | n/a | self.check_wrap("abcd efgh", 6, [" abcd", " efgh"], |
|---|
| 412 | n/a | initial_indent=" ", subsequent_indent=" ") |
|---|
| 413 | n/a | |
|---|
| 414 | n/a | def test_split(self): |
|---|
| 415 | n/a | # Ensure that the standard _split() method works as advertised |
|---|
| 416 | n/a | # in the comments |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | text = "Hello there -- you goof-ball, use the -b option!" |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | result = self.wrapper._split(text) |
|---|
| 421 | n/a | self.check(result, |
|---|
| 422 | n/a | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
|---|
| 423 | n/a | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | def test_break_on_hyphens(self): |
|---|
| 426 | n/a | # Ensure that the break_on_hyphens attributes work |
|---|
| 427 | n/a | text = "yaba daba-doo" |
|---|
| 428 | n/a | self.check_wrap(text, 10, ["yaba daba-", "doo"], |
|---|
| 429 | n/a | break_on_hyphens=True) |
|---|
| 430 | n/a | self.check_wrap(text, 10, ["yaba", "daba-doo"], |
|---|
| 431 | n/a | break_on_hyphens=False) |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | def test_bad_width(self): |
|---|
| 434 | n/a | # Ensure that width <= 0 is caught. |
|---|
| 435 | n/a | text = "Whatever, it doesn't matter." |
|---|
| 436 | n/a | self.assertRaises(ValueError, wrap, text, 0) |
|---|
| 437 | n/a | self.assertRaises(ValueError, wrap, text, -1) |
|---|
| 438 | n/a | |
|---|
| 439 | n/a | def test_no_split_at_umlaut(self): |
|---|
| 440 | n/a | text = "Die Empf\xe4nger-Auswahl" |
|---|
| 441 | n/a | self.check_wrap(text, 13, ["Die", "Empf\xe4nger-", "Auswahl"]) |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | def test_umlaut_followed_by_dash(self): |
|---|
| 444 | n/a | text = "aa \xe4\xe4-\xe4\xe4" |
|---|
| 445 | n/a | self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"]) |
|---|
| 446 | n/a | |
|---|
| 447 | n/a | def test_non_breaking_space(self): |
|---|
| 448 | n/a | text = 'This is a sentence with non-breaking\N{NO-BREAK SPACE}space.' |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | self.check_wrap(text, 20, |
|---|
| 451 | n/a | ['This is a sentence', |
|---|
| 452 | n/a | 'with non-', |
|---|
| 453 | n/a | 'breaking\N{NO-BREAK SPACE}space.'], |
|---|
| 454 | n/a | break_on_hyphens=True) |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | self.check_wrap(text, 20, |
|---|
| 457 | n/a | ['This is a sentence', |
|---|
| 458 | n/a | 'with', |
|---|
| 459 | n/a | 'non-breaking\N{NO-BREAK SPACE}space.'], |
|---|
| 460 | n/a | break_on_hyphens=False) |
|---|
| 461 | n/a | |
|---|
| 462 | n/a | def test_narrow_non_breaking_space(self): |
|---|
| 463 | n/a | text = ('This is a sentence with non-breaking' |
|---|
| 464 | n/a | '\N{NARROW NO-BREAK SPACE}space.') |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | self.check_wrap(text, 20, |
|---|
| 467 | n/a | ['This is a sentence', |
|---|
| 468 | n/a | 'with non-', |
|---|
| 469 | n/a | 'breaking\N{NARROW NO-BREAK SPACE}space.'], |
|---|
| 470 | n/a | break_on_hyphens=True) |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | self.check_wrap(text, 20, |
|---|
| 473 | n/a | ['This is a sentence', |
|---|
| 474 | n/a | 'with', |
|---|
| 475 | n/a | 'non-breaking\N{NARROW NO-BREAK SPACE}space.'], |
|---|
| 476 | n/a | break_on_hyphens=False) |
|---|
| 477 | n/a | |
|---|
| 478 | n/a | |
|---|
| 479 | n/a | class MaxLinesTestCase(BaseTestCase): |
|---|
| 480 | n/a | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
|---|
| 481 | n/a | |
|---|
| 482 | n/a | def test_simple(self): |
|---|
| 483 | n/a | self.check_wrap(self.text, 12, |
|---|
| 484 | n/a | ["Hello [...]"], |
|---|
| 485 | n/a | max_lines=0) |
|---|
| 486 | n/a | self.check_wrap(self.text, 12, |
|---|
| 487 | n/a | ["Hello [...]"], |
|---|
| 488 | n/a | max_lines=1) |
|---|
| 489 | n/a | self.check_wrap(self.text, 12, |
|---|
| 490 | n/a | ["Hello there,", |
|---|
| 491 | n/a | "how [...]"], |
|---|
| 492 | n/a | max_lines=2) |
|---|
| 493 | n/a | self.check_wrap(self.text, 13, |
|---|
| 494 | n/a | ["Hello there,", |
|---|
| 495 | n/a | "how are [...]"], |
|---|
| 496 | n/a | max_lines=2) |
|---|
| 497 | n/a | self.check_wrap(self.text, 80, [self.text], max_lines=1) |
|---|
| 498 | n/a | self.check_wrap(self.text, 12, |
|---|
| 499 | n/a | ["Hello there,", |
|---|
| 500 | n/a | "how are you", |
|---|
| 501 | n/a | "this fine", |
|---|
| 502 | n/a | "day? I'm", |
|---|
| 503 | n/a | "glad to hear", |
|---|
| 504 | n/a | "it!"], |
|---|
| 505 | n/a | max_lines=6) |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | def test_spaces(self): |
|---|
| 508 | n/a | # strip spaces before placeholder |
|---|
| 509 | n/a | self.check_wrap(self.text, 12, |
|---|
| 510 | n/a | ["Hello there,", |
|---|
| 511 | n/a | "how are you", |
|---|
| 512 | n/a | "this fine", |
|---|
| 513 | n/a | "day? [...]"], |
|---|
| 514 | n/a | max_lines=4) |
|---|
| 515 | n/a | # placeholder at the start of line |
|---|
| 516 | n/a | self.check_wrap(self.text, 6, |
|---|
| 517 | n/a | ["Hello", |
|---|
| 518 | n/a | "[...]"], |
|---|
| 519 | n/a | max_lines=2) |
|---|
| 520 | n/a | # final spaces |
|---|
| 521 | n/a | self.check_wrap(self.text + ' ' * 10, 12, |
|---|
| 522 | n/a | ["Hello there,", |
|---|
| 523 | n/a | "how are you", |
|---|
| 524 | n/a | "this fine", |
|---|
| 525 | n/a | "day? I'm", |
|---|
| 526 | n/a | "glad to hear", |
|---|
| 527 | n/a | "it!"], |
|---|
| 528 | n/a | max_lines=6) |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | def test_placeholder(self): |
|---|
| 531 | n/a | self.check_wrap(self.text, 12, |
|---|
| 532 | n/a | ["Hello..."], |
|---|
| 533 | n/a | max_lines=1, |
|---|
| 534 | n/a | placeholder='...') |
|---|
| 535 | n/a | self.check_wrap(self.text, 12, |
|---|
| 536 | n/a | ["Hello there,", |
|---|
| 537 | n/a | "how are..."], |
|---|
| 538 | n/a | max_lines=2, |
|---|
| 539 | n/a | placeholder='...') |
|---|
| 540 | n/a | # long placeholder and indentation |
|---|
| 541 | n/a | with self.assertRaises(ValueError): |
|---|
| 542 | n/a | wrap(self.text, 16, initial_indent=' ', |
|---|
| 543 | n/a | max_lines=1, placeholder=' [truncated]...') |
|---|
| 544 | n/a | with self.assertRaises(ValueError): |
|---|
| 545 | n/a | wrap(self.text, 16, subsequent_indent=' ', |
|---|
| 546 | n/a | max_lines=2, placeholder=' [truncated]...') |
|---|
| 547 | n/a | self.check_wrap(self.text, 16, |
|---|
| 548 | n/a | [" Hello there,", |
|---|
| 549 | n/a | " [truncated]..."], |
|---|
| 550 | n/a | max_lines=2, |
|---|
| 551 | n/a | initial_indent=' ', |
|---|
| 552 | n/a | subsequent_indent=' ', |
|---|
| 553 | n/a | placeholder=' [truncated]...') |
|---|
| 554 | n/a | self.check_wrap(self.text, 16, |
|---|
| 555 | n/a | [" [truncated]..."], |
|---|
| 556 | n/a | max_lines=1, |
|---|
| 557 | n/a | initial_indent=' ', |
|---|
| 558 | n/a | subsequent_indent=' ', |
|---|
| 559 | n/a | placeholder=' [truncated]...') |
|---|
| 560 | n/a | self.check_wrap(self.text, 80, [self.text], placeholder='.' * 1000) |
|---|
| 561 | n/a | |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | class LongWordTestCase (BaseTestCase): |
|---|
| 564 | n/a | def setUp(self): |
|---|
| 565 | n/a | self.wrapper = TextWrapper() |
|---|
| 566 | n/a | self.text = '''\ |
|---|
| 567 | n/a | Did you say "supercalifragilisticexpialidocious?" |
|---|
| 568 | n/a | How *do* you spell that odd word, anyways? |
|---|
| 569 | n/a | ''' |
|---|
| 570 | n/a | |
|---|
| 571 | n/a | def test_break_long(self): |
|---|
| 572 | n/a | # Wrap text with long words and lots of punctuation |
|---|
| 573 | n/a | |
|---|
| 574 | n/a | self.check_wrap(self.text, 30, |
|---|
| 575 | n/a | ['Did you say "supercalifragilis', |
|---|
| 576 | n/a | 'ticexpialidocious?" How *do*', |
|---|
| 577 | n/a | 'you spell that odd word,', |
|---|
| 578 | n/a | 'anyways?']) |
|---|
| 579 | n/a | self.check_wrap(self.text, 50, |
|---|
| 580 | n/a | ['Did you say "supercalifragilisticexpialidocious?"', |
|---|
| 581 | n/a | 'How *do* you spell that odd word, anyways?']) |
|---|
| 582 | n/a | |
|---|
| 583 | n/a | # SF bug 797650. Prevent an infinite loop by making sure that at |
|---|
| 584 | n/a | # least one character gets split off on every pass. |
|---|
| 585 | n/a | self.check_wrap('-'*10+'hello', 10, |
|---|
| 586 | n/a | ['----------', |
|---|
| 587 | n/a | ' h', |
|---|
| 588 | n/a | ' e', |
|---|
| 589 | n/a | ' l', |
|---|
| 590 | n/a | ' l', |
|---|
| 591 | n/a | ' o'], |
|---|
| 592 | n/a | subsequent_indent = ' '*15) |
|---|
| 593 | n/a | |
|---|
| 594 | n/a | # bug 1146. Prevent a long word to be wrongly wrapped when the |
|---|
| 595 | n/a | # preceding word is exactly one character shorter than the width |
|---|
| 596 | n/a | self.check_wrap(self.text, 12, |
|---|
| 597 | n/a | ['Did you say ', |
|---|
| 598 | n/a | '"supercalifr', |
|---|
| 599 | n/a | 'agilisticexp', |
|---|
| 600 | n/a | 'ialidocious?', |
|---|
| 601 | n/a | '" How *do*', |
|---|
| 602 | n/a | 'you spell', |
|---|
| 603 | n/a | 'that odd', |
|---|
| 604 | n/a | 'word,', |
|---|
| 605 | n/a | 'anyways?']) |
|---|
| 606 | n/a | |
|---|
| 607 | n/a | def test_nobreak_long(self): |
|---|
| 608 | n/a | # Test with break_long_words disabled |
|---|
| 609 | n/a | self.wrapper.break_long_words = 0 |
|---|
| 610 | n/a | self.wrapper.width = 30 |
|---|
| 611 | n/a | expect = ['Did you say', |
|---|
| 612 | n/a | '"supercalifragilisticexpialidocious?"', |
|---|
| 613 | n/a | 'How *do* you spell that odd', |
|---|
| 614 | n/a | 'word, anyways?' |
|---|
| 615 | n/a | ] |
|---|
| 616 | n/a | result = self.wrapper.wrap(self.text) |
|---|
| 617 | n/a | self.check(result, expect) |
|---|
| 618 | n/a | |
|---|
| 619 | n/a | # Same thing with kwargs passed to standalone wrap() function. |
|---|
| 620 | n/a | result = wrap(self.text, width=30, break_long_words=0) |
|---|
| 621 | n/a | self.check(result, expect) |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | def test_max_lines_long(self): |
|---|
| 624 | n/a | self.check_wrap(self.text, 12, |
|---|
| 625 | n/a | ['Did you say ', |
|---|
| 626 | n/a | '"supercalifr', |
|---|
| 627 | n/a | 'agilisticexp', |
|---|
| 628 | n/a | '[...]'], |
|---|
| 629 | n/a | max_lines=4) |
|---|
| 630 | n/a | |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | class IndentTestCases(BaseTestCase): |
|---|
| 633 | n/a | |
|---|
| 634 | n/a | # called before each test method |
|---|
| 635 | n/a | def setUp(self): |
|---|
| 636 | n/a | self.text = '''\ |
|---|
| 637 | n/a | This paragraph will be filled, first without any indentation, |
|---|
| 638 | n/a | and then with some (including a hanging indent).''' |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | def test_fill(self): |
|---|
| 642 | n/a | # Test the fill() method |
|---|
| 643 | n/a | |
|---|
| 644 | n/a | expect = '''\ |
|---|
| 645 | n/a | This paragraph will be filled, first |
|---|
| 646 | n/a | without any indentation, and then with |
|---|
| 647 | n/a | some (including a hanging indent).''' |
|---|
| 648 | n/a | |
|---|
| 649 | n/a | result = fill(self.text, 40) |
|---|
| 650 | n/a | self.check(result, expect) |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | |
|---|
| 653 | n/a | def test_initial_indent(self): |
|---|
| 654 | n/a | # Test initial_indent parameter |
|---|
| 655 | n/a | |
|---|
| 656 | n/a | expect = [" This paragraph will be filled,", |
|---|
| 657 | n/a | "first without any indentation, and then", |
|---|
| 658 | n/a | "with some (including a hanging indent)."] |
|---|
| 659 | n/a | result = wrap(self.text, 40, initial_indent=" ") |
|---|
| 660 | n/a | self.check(result, expect) |
|---|
| 661 | n/a | |
|---|
| 662 | n/a | expect = "\n".join(expect) |
|---|
| 663 | n/a | result = fill(self.text, 40, initial_indent=" ") |
|---|
| 664 | n/a | self.check(result, expect) |
|---|
| 665 | n/a | |
|---|
| 666 | n/a | |
|---|
| 667 | n/a | def test_subsequent_indent(self): |
|---|
| 668 | n/a | # Test subsequent_indent parameter |
|---|
| 669 | n/a | |
|---|
| 670 | n/a | expect = '''\ |
|---|
| 671 | n/a | * This paragraph will be filled, first |
|---|
| 672 | n/a | without any indentation, and then |
|---|
| 673 | n/a | with some (including a hanging |
|---|
| 674 | n/a | indent).''' |
|---|
| 675 | n/a | |
|---|
| 676 | n/a | result = fill(self.text, 40, |
|---|
| 677 | n/a | initial_indent=" * ", subsequent_indent=" ") |
|---|
| 678 | n/a | self.check(result, expect) |
|---|
| 679 | n/a | |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | # Despite the similar names, DedentTestCase is *not* the inverse |
|---|
| 682 | n/a | # of IndentTestCase! |
|---|
| 683 | n/a | class DedentTestCase(unittest.TestCase): |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | def assertUnchanged(self, text): |
|---|
| 686 | n/a | """assert that dedent() has no effect on 'text'""" |
|---|
| 687 | n/a | self.assertEqual(text, dedent(text)) |
|---|
| 688 | n/a | |
|---|
| 689 | n/a | def test_dedent_nomargin(self): |
|---|
| 690 | n/a | # No lines indented. |
|---|
| 691 | n/a | text = "Hello there.\nHow are you?\nOh good, I'm glad." |
|---|
| 692 | n/a | self.assertUnchanged(text) |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | # Similar, with a blank line. |
|---|
| 695 | n/a | text = "Hello there.\n\nBoo!" |
|---|
| 696 | n/a | self.assertUnchanged(text) |
|---|
| 697 | n/a | |
|---|
| 698 | n/a | # Some lines indented, but overall margin is still zero. |
|---|
| 699 | n/a | text = "Hello there.\n This is indented." |
|---|
| 700 | n/a | self.assertUnchanged(text) |
|---|
| 701 | n/a | |
|---|
| 702 | n/a | # Again, add a blank line. |
|---|
| 703 | n/a | text = "Hello there.\n\n Boo!\n" |
|---|
| 704 | n/a | self.assertUnchanged(text) |
|---|
| 705 | n/a | |
|---|
| 706 | n/a | def test_dedent_even(self): |
|---|
| 707 | n/a | # All lines indented by two spaces. |
|---|
| 708 | n/a | text = " Hello there.\n How are ya?\n Oh good." |
|---|
| 709 | n/a | expect = "Hello there.\nHow are ya?\nOh good." |
|---|
| 710 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 711 | n/a | |
|---|
| 712 | n/a | # Same, with blank lines. |
|---|
| 713 | n/a | text = " Hello there.\n\n How are ya?\n Oh good.\n" |
|---|
| 714 | n/a | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
|---|
| 715 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 716 | n/a | |
|---|
| 717 | n/a | # Now indent one of the blank lines. |
|---|
| 718 | n/a | text = " Hello there.\n \n How are ya?\n Oh good.\n" |
|---|
| 719 | n/a | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
|---|
| 720 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 721 | n/a | |
|---|
| 722 | n/a | def test_dedent_uneven(self): |
|---|
| 723 | n/a | # Lines indented unevenly. |
|---|
| 724 | n/a | text = '''\ |
|---|
| 725 | n/a | def foo(): |
|---|
| 726 | n/a | while 1: |
|---|
| 727 | n/a | return foo |
|---|
| 728 | n/a | ''' |
|---|
| 729 | n/a | expect = '''\ |
|---|
| 730 | n/a | def foo(): |
|---|
| 731 | n/a | while 1: |
|---|
| 732 | n/a | return foo |
|---|
| 733 | n/a | ''' |
|---|
| 734 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 735 | n/a | |
|---|
| 736 | n/a | # Uneven indentation with a blank line. |
|---|
| 737 | n/a | text = " Foo\n Bar\n\n Baz\n" |
|---|
| 738 | n/a | expect = "Foo\n Bar\n\n Baz\n" |
|---|
| 739 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 740 | n/a | |
|---|
| 741 | n/a | # Uneven indentation with a whitespace-only line. |
|---|
| 742 | n/a | text = " Foo\n Bar\n \n Baz\n" |
|---|
| 743 | n/a | expect = "Foo\n Bar\n\n Baz\n" |
|---|
| 744 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | # dedent() should not mangle internal tabs |
|---|
| 747 | n/a | def test_dedent_preserve_internal_tabs(self): |
|---|
| 748 | n/a | text = " hello\tthere\n how are\tyou?" |
|---|
| 749 | n/a | expect = "hello\tthere\nhow are\tyou?" |
|---|
| 750 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 751 | n/a | |
|---|
| 752 | n/a | # make sure that it preserves tabs when it's not making any |
|---|
| 753 | n/a | # changes at all |
|---|
| 754 | n/a | self.assertEqual(expect, dedent(expect)) |
|---|
| 755 | n/a | |
|---|
| 756 | n/a | # dedent() should not mangle tabs in the margin (i.e. |
|---|
| 757 | n/a | # tabs and spaces both count as margin, but are *not* |
|---|
| 758 | n/a | # considered equivalent) |
|---|
| 759 | n/a | def test_dedent_preserve_margin_tabs(self): |
|---|
| 760 | n/a | text = " hello there\n\thow are you?" |
|---|
| 761 | n/a | self.assertUnchanged(text) |
|---|
| 762 | n/a | |
|---|
| 763 | n/a | # same effect even if we have 8 spaces |
|---|
| 764 | n/a | text = " hello there\n\thow are you?" |
|---|
| 765 | n/a | self.assertUnchanged(text) |
|---|
| 766 | n/a | |
|---|
| 767 | n/a | # dedent() only removes whitespace that can be uniformly removed! |
|---|
| 768 | n/a | text = "\thello there\n\thow are you?" |
|---|
| 769 | n/a | expect = "hello there\nhow are you?" |
|---|
| 770 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | text = " \thello there\n \thow are you?" |
|---|
| 773 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 774 | n/a | |
|---|
| 775 | n/a | text = " \t hello there\n \t how are you?" |
|---|
| 776 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 777 | n/a | |
|---|
| 778 | n/a | text = " \thello there\n \t how are you?" |
|---|
| 779 | n/a | expect = "hello there\n how are you?" |
|---|
| 780 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 781 | n/a | |
|---|
| 782 | n/a | # test margin is smaller than smallest indent |
|---|
| 783 | n/a | text = " \thello there\n \thow are you?\n \tI'm fine, thanks" |
|---|
| 784 | n/a | expect = " \thello there\n \thow are you?\n\tI'm fine, thanks" |
|---|
| 785 | n/a | self.assertEqual(expect, dedent(text)) |
|---|
| 786 | n/a | |
|---|
| 787 | n/a | |
|---|
| 788 | n/a | # Test textwrap.indent |
|---|
| 789 | n/a | class IndentTestCase(unittest.TestCase): |
|---|
| 790 | n/a | # The examples used for tests. If any of these change, the expected |
|---|
| 791 | n/a | # results in the various test cases must also be updated. |
|---|
| 792 | n/a | # The roundtrip cases are separate, because textwrap.dedent doesn't |
|---|
| 793 | n/a | # handle Windows line endings |
|---|
| 794 | n/a | ROUNDTRIP_CASES = ( |
|---|
| 795 | n/a | # Basic test case |
|---|
| 796 | n/a | "Hi.\nThis is a test.\nTesting.", |
|---|
| 797 | n/a | # Include a blank line |
|---|
| 798 | n/a | "Hi.\nThis is a test.\n\nTesting.", |
|---|
| 799 | n/a | # Include leading and trailing blank lines |
|---|
| 800 | n/a | "\nHi.\nThis is a test.\nTesting.\n", |
|---|
| 801 | n/a | ) |
|---|
| 802 | n/a | CASES = ROUNDTRIP_CASES + ( |
|---|
| 803 | n/a | # Use Windows line endings |
|---|
| 804 | n/a | "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
|---|
| 805 | n/a | # Pathological case |
|---|
| 806 | n/a | "\nHi.\r\nThis is a test.\n\r\nTesting.\r\n\n", |
|---|
| 807 | n/a | ) |
|---|
| 808 | n/a | |
|---|
| 809 | n/a | def test_indent_nomargin_default(self): |
|---|
| 810 | n/a | # indent should do nothing if 'prefix' is empty. |
|---|
| 811 | n/a | for text in self.CASES: |
|---|
| 812 | n/a | self.assertEqual(indent(text, ''), text) |
|---|
| 813 | n/a | |
|---|
| 814 | n/a | def test_indent_nomargin_explicit_default(self): |
|---|
| 815 | n/a | # The same as test_indent_nomargin, but explicitly requesting |
|---|
| 816 | n/a | # the default behaviour by passing None as the predicate |
|---|
| 817 | n/a | for text in self.CASES: |
|---|
| 818 | n/a | self.assertEqual(indent(text, '', None), text) |
|---|
| 819 | n/a | |
|---|
| 820 | n/a | def test_indent_nomargin_all_lines(self): |
|---|
| 821 | n/a | # The same as test_indent_nomargin, but using the optional |
|---|
| 822 | n/a | # predicate argument |
|---|
| 823 | n/a | predicate = lambda line: True |
|---|
| 824 | n/a | for text in self.CASES: |
|---|
| 825 | n/a | self.assertEqual(indent(text, '', predicate), text) |
|---|
| 826 | n/a | |
|---|
| 827 | n/a | def test_indent_no_lines(self): |
|---|
| 828 | n/a | # Explicitly skip indenting any lines |
|---|
| 829 | n/a | predicate = lambda line: False |
|---|
| 830 | n/a | for text in self.CASES: |
|---|
| 831 | n/a | self.assertEqual(indent(text, ' ', predicate), text) |
|---|
| 832 | n/a | |
|---|
| 833 | n/a | def test_roundtrip_spaces(self): |
|---|
| 834 | n/a | # A whitespace prefix should roundtrip with dedent |
|---|
| 835 | n/a | for text in self.ROUNDTRIP_CASES: |
|---|
| 836 | n/a | self.assertEqual(dedent(indent(text, ' ')), text) |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | def test_roundtrip_tabs(self): |
|---|
| 839 | n/a | # A whitespace prefix should roundtrip with dedent |
|---|
| 840 | n/a | for text in self.ROUNDTRIP_CASES: |
|---|
| 841 | n/a | self.assertEqual(dedent(indent(text, '\t\t')), text) |
|---|
| 842 | n/a | |
|---|
| 843 | n/a | def test_roundtrip_mixed(self): |
|---|
| 844 | n/a | # A whitespace prefix should roundtrip with dedent |
|---|
| 845 | n/a | for text in self.ROUNDTRIP_CASES: |
|---|
| 846 | n/a | self.assertEqual(dedent(indent(text, ' \t \t ')), text) |
|---|
| 847 | n/a | |
|---|
| 848 | n/a | def test_indent_default(self): |
|---|
| 849 | n/a | # Test default indenting of lines that are not whitespace only |
|---|
| 850 | n/a | prefix = ' ' |
|---|
| 851 | n/a | expected = ( |
|---|
| 852 | n/a | # Basic test case |
|---|
| 853 | n/a | " Hi.\n This is a test.\n Testing.", |
|---|
| 854 | n/a | # Include a blank line |
|---|
| 855 | n/a | " Hi.\n This is a test.\n\n Testing.", |
|---|
| 856 | n/a | # Include leading and trailing blank lines |
|---|
| 857 | n/a | "\n Hi.\n This is a test.\n Testing.\n", |
|---|
| 858 | n/a | # Use Windows line endings |
|---|
| 859 | n/a | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
|---|
| 860 | n/a | # Pathological case |
|---|
| 861 | n/a | "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
|---|
| 862 | n/a | ) |
|---|
| 863 | n/a | for text, expect in zip(self.CASES, expected): |
|---|
| 864 | n/a | self.assertEqual(indent(text, prefix), expect) |
|---|
| 865 | n/a | |
|---|
| 866 | n/a | def test_indent_explicit_default(self): |
|---|
| 867 | n/a | # Test default indenting of lines that are not whitespace only |
|---|
| 868 | n/a | prefix = ' ' |
|---|
| 869 | n/a | expected = ( |
|---|
| 870 | n/a | # Basic test case |
|---|
| 871 | n/a | " Hi.\n This is a test.\n Testing.", |
|---|
| 872 | n/a | # Include a blank line |
|---|
| 873 | n/a | " Hi.\n This is a test.\n\n Testing.", |
|---|
| 874 | n/a | # Include leading and trailing blank lines |
|---|
| 875 | n/a | "\n Hi.\n This is a test.\n Testing.\n", |
|---|
| 876 | n/a | # Use Windows line endings |
|---|
| 877 | n/a | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
|---|
| 878 | n/a | # Pathological case |
|---|
| 879 | n/a | "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
|---|
| 880 | n/a | ) |
|---|
| 881 | n/a | for text, expect in zip(self.CASES, expected): |
|---|
| 882 | n/a | self.assertEqual(indent(text, prefix, None), expect) |
|---|
| 883 | n/a | |
|---|
| 884 | n/a | def test_indent_all_lines(self): |
|---|
| 885 | n/a | # Add 'prefix' to all lines, including whitespace-only ones. |
|---|
| 886 | n/a | prefix = ' ' |
|---|
| 887 | n/a | expected = ( |
|---|
| 888 | n/a | # Basic test case |
|---|
| 889 | n/a | " Hi.\n This is a test.\n Testing.", |
|---|
| 890 | n/a | # Include a blank line |
|---|
| 891 | n/a | " Hi.\n This is a test.\n \n Testing.", |
|---|
| 892 | n/a | # Include leading and trailing blank lines |
|---|
| 893 | n/a | " \n Hi.\n This is a test.\n Testing.\n", |
|---|
| 894 | n/a | # Use Windows line endings |
|---|
| 895 | n/a | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
|---|
| 896 | n/a | # Pathological case |
|---|
| 897 | n/a | " \n Hi.\r\n This is a test.\n \r\n Testing.\r\n \n", |
|---|
| 898 | n/a | ) |
|---|
| 899 | n/a | predicate = lambda line: True |
|---|
| 900 | n/a | for text, expect in zip(self.CASES, expected): |
|---|
| 901 | n/a | self.assertEqual(indent(text, prefix, predicate), expect) |
|---|
| 902 | n/a | |
|---|
| 903 | n/a | def test_indent_empty_lines(self): |
|---|
| 904 | n/a | # Add 'prefix' solely to whitespace-only lines. |
|---|
| 905 | n/a | prefix = ' ' |
|---|
| 906 | n/a | expected = ( |
|---|
| 907 | n/a | # Basic test case |
|---|
| 908 | n/a | "Hi.\nThis is a test.\nTesting.", |
|---|
| 909 | n/a | # Include a blank line |
|---|
| 910 | n/a | "Hi.\nThis is a test.\n \nTesting.", |
|---|
| 911 | n/a | # Include leading and trailing blank lines |
|---|
| 912 | n/a | " \nHi.\nThis is a test.\nTesting.\n", |
|---|
| 913 | n/a | # Use Windows line endings |
|---|
| 914 | n/a | "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
|---|
| 915 | n/a | # Pathological case |
|---|
| 916 | n/a | " \nHi.\r\nThis is a test.\n \r\nTesting.\r\n \n", |
|---|
| 917 | n/a | ) |
|---|
| 918 | n/a | predicate = lambda line: not line.strip() |
|---|
| 919 | n/a | for text, expect in zip(self.CASES, expected): |
|---|
| 920 | n/a | self.assertEqual(indent(text, prefix, predicate), expect) |
|---|
| 921 | n/a | |
|---|
| 922 | n/a | |
|---|
| 923 | n/a | class ShortenTestCase(BaseTestCase): |
|---|
| 924 | n/a | |
|---|
| 925 | n/a | def check_shorten(self, text, width, expect, **kwargs): |
|---|
| 926 | n/a | result = shorten(text, width, **kwargs) |
|---|
| 927 | n/a | self.check(result, expect) |
|---|
| 928 | n/a | |
|---|
| 929 | n/a | def test_simple(self): |
|---|
| 930 | n/a | # Simple case: just words, spaces, and a bit of punctuation |
|---|
| 931 | n/a | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
|---|
| 932 | n/a | |
|---|
| 933 | n/a | self.check_shorten(text, 18, "Hello there, [...]") |
|---|
| 934 | n/a | self.check_shorten(text, len(text), text) |
|---|
| 935 | n/a | self.check_shorten(text, len(text) - 1, |
|---|
| 936 | n/a | "Hello there, how are you this fine day? " |
|---|
| 937 | n/a | "I'm glad to [...]") |
|---|
| 938 | n/a | |
|---|
| 939 | n/a | def test_placeholder(self): |
|---|
| 940 | n/a | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
|---|
| 941 | n/a | |
|---|
| 942 | n/a | self.check_shorten(text, 17, "Hello there,$$", placeholder='$$') |
|---|
| 943 | n/a | self.check_shorten(text, 18, "Hello there, how$$", placeholder='$$') |
|---|
| 944 | n/a | self.check_shorten(text, 18, "Hello there, $$", placeholder=' $$') |
|---|
| 945 | n/a | self.check_shorten(text, len(text), text, placeholder='$$') |
|---|
| 946 | n/a | self.check_shorten(text, len(text) - 1, |
|---|
| 947 | n/a | "Hello there, how are you this fine day? " |
|---|
| 948 | n/a | "I'm glad to hear$$", placeholder='$$') |
|---|
| 949 | n/a | |
|---|
| 950 | n/a | def test_empty_string(self): |
|---|
| 951 | n/a | self.check_shorten("", 6, "") |
|---|
| 952 | n/a | |
|---|
| 953 | n/a | def test_whitespace(self): |
|---|
| 954 | n/a | # Whitespace collapsing |
|---|
| 955 | n/a | text = """ |
|---|
| 956 | n/a | This is a paragraph that already has |
|---|
| 957 | n/a | line breaks and \t tabs too.""" |
|---|
| 958 | n/a | self.check_shorten(text, 62, |
|---|
| 959 | n/a | "This is a paragraph that already has line " |
|---|
| 960 | n/a | "breaks and tabs too.") |
|---|
| 961 | n/a | self.check_shorten(text, 61, |
|---|
| 962 | n/a | "This is a paragraph that already has line " |
|---|
| 963 | n/a | "breaks and [...]") |
|---|
| 964 | n/a | |
|---|
| 965 | n/a | self.check_shorten("hello world! ", 12, "hello world!") |
|---|
| 966 | n/a | self.check_shorten("hello world! ", 11, "hello [...]") |
|---|
| 967 | n/a | # The leading space is trimmed from the placeholder |
|---|
| 968 | n/a | # (it would be ugly otherwise). |
|---|
| 969 | n/a | self.check_shorten("hello world! ", 10, "[...]") |
|---|
| 970 | n/a | |
|---|
| 971 | n/a | def test_width_too_small_for_placeholder(self): |
|---|
| 972 | n/a | shorten("x" * 20, width=8, placeholder="(......)") |
|---|
| 973 | n/a | with self.assertRaises(ValueError): |
|---|
| 974 | n/a | shorten("x" * 20, width=8, placeholder="(.......)") |
|---|
| 975 | n/a | |
|---|
| 976 | n/a | def test_first_word_too_long_but_placeholder_fits(self): |
|---|
| 977 | n/a | self.check_shorten("Helloo", 5, "[...]") |
|---|
| 978 | n/a | |
|---|
| 979 | n/a | |
|---|
| 980 | n/a | if __name__ == '__main__': |
|---|
| 981 | n/a | unittest.main() |
|---|