Greg Ward | 3dc94e1 | 2002-08-22 18:37:50 +0000 | [diff] [blame] | 1 | # |
| 2 | # Test script for the textwrap module. |
| 3 | # |
| 4 | # Original tests written by Greg Ward <gward@python.net>. |
| 5 | # Converted to PyUnit by Peter Hansen <peter@engcorp.com>. |
| 6 | # Currently maintained by Greg Ward. |
| 7 | # |
| 8 | # $Id$ |
| 9 | # |
| 10 | |
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 | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 14 | from textwrap import TextWrapper, wrap, fill |
| 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) |
| 26 | elif isinstance(textin, (str, unicode)): |
| 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 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 36 | def check_wrap (self, text, width, expect): |
| 37 | result = wrap(text, width) |
| 38 | self.check(result, expect) |
| 39 | |
Greg Ward | 715debd | 2002-08-22 21:16:25 +0000 | [diff] [blame] | 40 | def check_split (self, wrapper, text, expect): |
| 41 | result = wrapper._split(text) |
| 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): |
| 50 | self.wrapper = TextWrapper(width=45, fix_sentence_endings=True) |
| 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 | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 87 | result = self.wrapper.wrap(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 88 | self.check(result, expect) |
| 89 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 90 | result = self.wrapper.fill(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 91 | self.check(result, '\n'.join(expect)) |
| 92 | |
| 93 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 94 | def test_wrap_short(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 95 | # Wrapping to make short lines longer |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 96 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 97 | text = "This is a\nshort paragraph." |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 98 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 99 | self.check_wrap(text, 20, ["This is a short", |
| 100 | "paragraph."]) |
| 101 | self.check_wrap(text, 40, ["This is a short paragraph."]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 102 | |
| 103 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 104 | def test_hyphenated(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 105 | # Test breaking hyphenated words |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 106 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 107 | text = ("this-is-a-useful-feature-for-" |
| 108 | "reformatting-posts-from-tim-peters'ly") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 109 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 110 | self.check_wrap(text, 40, |
| 111 | ["this-is-a-useful-feature-for-", |
| 112 | "reformatting-posts-from-tim-peters'ly"]) |
| 113 | self.check_wrap(text, 41, |
| 114 | ["this-is-a-useful-feature-for-", |
| 115 | "reformatting-posts-from-tim-peters'ly"]) |
| 116 | self.check_wrap(text, 42, |
| 117 | ["this-is-a-useful-feature-for-reformatting-", |
| 118 | "posts-from-tim-peters'ly"]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 119 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 120 | def test_em_dash(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 121 | # Test text with em-dashes |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 122 | text = "Em-dashes should be written -- thus." |
| 123 | self.check_wrap(text, 25, |
| 124 | ["Em-dashes should be", |
| 125 | "written -- thus."]) |
| 126 | |
| 127 | # Probe the boundaries of the properly written em-dash, |
| 128 | # ie. " -- ". |
| 129 | self.check_wrap(text, 29, |
| 130 | ["Em-dashes should be written", |
| 131 | "-- thus."]) |
| 132 | expect = ["Em-dashes should be written --", |
| 133 | "thus."] |
| 134 | self.check_wrap(text, 30, expect) |
| 135 | self.check_wrap(text, 35, expect) |
| 136 | self.check_wrap(text, 36, |
| 137 | ["Em-dashes should be written -- thus."]) |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 138 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 139 | # The improperly written em-dash is handled too, because |
| 140 | # it's adjacent to non-whitespace on both sides. |
| 141 | text = "You can also do--this or even---this." |
| 142 | expect = ["You can also do", |
| 143 | "--this or even", |
| 144 | "---this."] |
| 145 | self.check_wrap(text, 15, expect) |
| 146 | self.check_wrap(text, 16, expect) |
| 147 | expect = ["You can also do--", |
| 148 | "this or even---", |
| 149 | "this."] |
| 150 | self.check_wrap(text, 17, expect) |
| 151 | self.check_wrap(text, 19, expect) |
| 152 | expect = ["You can also do--this or even", |
| 153 | "---this."] |
| 154 | self.check_wrap(text, 29, expect) |
| 155 | self.check_wrap(text, 31, expect) |
| 156 | expect = ["You can also do--this or even---", |
| 157 | "this."] |
| 158 | self.check_wrap(text, 32, expect) |
| 159 | self.check_wrap(text, 35, expect) |
| 160 | |
| 161 | # All of the above behaviour could be deduced by probing the |
| 162 | # _split() method. |
| 163 | text = "Here's an -- em-dash and--here's another---and another!" |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 164 | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
| 165 | "and", "--", "here's", " ", "another", "---", |
| 166 | "and", " ", "another!"] |
Greg Ward | 715debd | 2002-08-22 21:16:25 +0000 | [diff] [blame] | 167 | self.check_split(self.wrapper, text, expect) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 168 | |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 169 | text = "and then--bam!--he was gone" |
| 170 | expect = ["and", " ", "then", "--", "bam!", "--", |
| 171 | "he", " ", "was", " ", "gone"] |
| 172 | self.check_split(self.wrapper, text, expect) |
| 173 | |
| 174 | |
Greg Ward | 34f995b | 2002-08-22 21:10:07 +0000 | [diff] [blame] | 175 | def test_unix_options (self): |
| 176 | # Test that Unix-style command-line options are wrapped correctly. |
| 177 | # Both Optik (OptionParser) and Docutils rely on this behaviour! |
| 178 | |
| 179 | text = "You should use the -n option, or --dry-run in its long form." |
| 180 | self.check_wrap(text, 20, |
| 181 | ["You should use the", |
| 182 | "-n option, or --dry-", |
| 183 | "run in its long", |
| 184 | "form."]) |
| 185 | self.check_wrap(text, 21, |
| 186 | ["You should use the -n", |
| 187 | "option, or --dry-run", |
| 188 | "in its long form."]) |
| 189 | expect = ["You should use the -n option, or", |
| 190 | "--dry-run in its long form."] |
| 191 | self.check_wrap(text, 32, expect) |
| 192 | self.check_wrap(text, 34, expect) |
| 193 | self.check_wrap(text, 35, expect) |
| 194 | self.check_wrap(text, 38, expect) |
| 195 | expect = ["You should use the -n option, or --dry-", |
| 196 | "run in its long form."] |
| 197 | self.check_wrap(text, 39, expect) |
| 198 | self.check_wrap(text, 41, expect) |
| 199 | expect = ["You should use the -n option, or --dry-run", |
| 200 | "in its long form."] |
| 201 | self.check_wrap(text, 42, expect) |
| 202 | |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 203 | # Again, all of the above can be deduced from _split(). |
| 204 | text = "the -n option, or --dry-run or --dryrun" |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 205 | expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", |
| 206 | "--dry-", "run", " ", "or", " ", "--dryrun"] |
Greg Ward | 715debd | 2002-08-22 21:16:25 +0000 | [diff] [blame] | 207 | self.check_split(self.wrapper, text, expect) |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 208 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 209 | def test_split(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 210 | # Ensure that the standard _split() method works as advertised |
| 211 | # in the comments |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 212 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 213 | text = "Hello there -- you goof-ball, use the -b option!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 214 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 215 | result = self.wrapper._split(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 216 | self.check(result, |
| 217 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 218 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 219 | |
| 220 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 221 | class LongWordTestCase (BaseTestCase): |
| 222 | def setUp(self): |
| 223 | self.wrapper = TextWrapper() |
| 224 | self.text = ''' |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 225 | Did you say "supercalifragilisticexpialidocious?" |
| 226 | How *do* you spell that odd word, anyways? |
| 227 | ''' |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 228 | |
| 229 | def test_break_long(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 230 | # Wrap text with long words and lots of punctuation |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 231 | |
| 232 | self.check_wrap(self.text, 30, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 233 | ['Did you say "supercalifragilis', |
| 234 | 'ticexpialidocious?" How *do*', |
| 235 | 'you spell that odd word,', |
| 236 | 'anyways?']) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 237 | self.check_wrap(self.text, 50, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 238 | ['Did you say "supercalifragilisticexpialidocious?"', |
| 239 | 'How *do* you spell that odd word, anyways?']) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 240 | |
| 241 | |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 242 | def test_nobreak_long(self): |
| 243 | # Test with break_long_words disabled |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 244 | self.wrapper.break_long_words = 0 |
| 245 | self.wrapper.width = 30 |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 246 | expect = ['Did you say', |
| 247 | '"supercalifragilisticexpialidocious?"', |
| 248 | 'How *do* you spell that odd', |
| 249 | 'word, anyways?' |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 250 | ] |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 251 | result = self.wrapper.wrap(self.text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 252 | self.check(result, expect) |
| 253 | |
| 254 | # Same thing with kwargs passed to standalone wrap() function. |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 255 | result = wrap(self.text, width=30, break_long_words=0) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 256 | self.check(result, expect) |
| 257 | |
| 258 | |
| 259 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 260 | class IndentTestCases(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 261 | |
| 262 | # called before each test method |
| 263 | def setUp(self): |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 264 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 265 | This paragraph will be filled, first without any indentation, |
| 266 | and then with some (including a hanging indent).''' |
| 267 | |
| 268 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 269 | def test_fill(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 270 | # Test the fill() method |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 271 | |
| 272 | expect = '''\ |
| 273 | This paragraph will be filled, first |
| 274 | without any indentation, and then with |
| 275 | some (including a hanging indent).''' |
| 276 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 277 | result = fill(self.text, 40) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 278 | self.check(result, expect) |
| 279 | |
| 280 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 281 | def test_initial_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 282 | # Test initial_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 283 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 284 | expect = [" This paragraph will be filled,", |
| 285 | "first without any indentation, and then", |
| 286 | "with some (including a hanging indent)."] |
| 287 | result = wrap(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 288 | self.check(result, expect) |
| 289 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 290 | expect = "\n".join(expect) |
| 291 | result = fill(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 292 | self.check(result, expect) |
| 293 | |
| 294 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 295 | def test_subsequent_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 296 | # Test subsequent_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 297 | |
| 298 | expect = '''\ |
| 299 | * This paragraph will be filled, first |
| 300 | without any indentation, and then |
| 301 | with some (including a hanging |
| 302 | indent).''' |
| 303 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 304 | result = fill(self.text, 40, |
| 305 | initial_indent=" * ", subsequent_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 306 | self.check(result, expect) |
| 307 | |
| 308 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 309 | def test_main(): |
| 310 | suite = unittest.TestSuite() |
| 311 | suite.addTest(unittest.makeSuite(WrapTestCase)) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 312 | suite.addTest(unittest.makeSuite(LongWordTestCase)) |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 313 | suite.addTest(unittest.makeSuite(IndentTestCases)) |
| 314 | test_support.run_suite(suite) |
| 315 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 316 | if __name__ == '__main__': |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 317 | test_main() |