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 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 36 | def check_wrap(self, text, width, expect, **kwargs): |
| 37 | result = wrap(text, width, **kwargs) |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 38 | self.check(result, expect) |
| 39 | |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 40 | def check_split(self, text, expect): |
| 41 | result = self.wrapper._split(text) |
Greg Ward | 715debd | 2002-08-22 21:16:25 +0000 | [diff] [blame] | 42 | self.assertEquals(result, expect, |
| 43 | "\nexpected %r\n" |
| 44 | "but got %r" % (expect, result)) |
| 45 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 46 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 47 | class WrapTestCase(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 48 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 49 | def setUp(self): |
| 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 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 104 | def test_wrap_short_1line(self): |
| 105 | # Test endcases |
| 106 | |
| 107 | text = "This is a short line." |
| 108 | |
| 109 | self.check_wrap(text, 30, ["This is a short line."]) |
| 110 | self.check_wrap(text, 30, ["(1) This is a short line."], |
| 111 | initial_indent="(1) ") |
| 112 | |
| 113 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 114 | def test_hyphenated(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 115 | # Test breaking hyphenated words |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 116 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 117 | text = ("this-is-a-useful-feature-for-" |
| 118 | "reformatting-posts-from-tim-peters'ly") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 119 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 120 | self.check_wrap(text, 40, |
| 121 | ["this-is-a-useful-feature-for-", |
| 122 | "reformatting-posts-from-tim-peters'ly"]) |
| 123 | self.check_wrap(text, 41, |
| 124 | ["this-is-a-useful-feature-for-", |
| 125 | "reformatting-posts-from-tim-peters'ly"]) |
| 126 | self.check_wrap(text, 42, |
| 127 | ["this-is-a-useful-feature-for-reformatting-", |
| 128 | "posts-from-tim-peters'ly"]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 129 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 130 | def test_em_dash(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 131 | # Test text with em-dashes |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 132 | text = "Em-dashes should be written -- thus." |
| 133 | self.check_wrap(text, 25, |
| 134 | ["Em-dashes should be", |
| 135 | "written -- thus."]) |
| 136 | |
| 137 | # Probe the boundaries of the properly written em-dash, |
| 138 | # ie. " -- ". |
| 139 | self.check_wrap(text, 29, |
| 140 | ["Em-dashes should be written", |
| 141 | "-- thus."]) |
| 142 | expect = ["Em-dashes should be written --", |
| 143 | "thus."] |
| 144 | self.check_wrap(text, 30, expect) |
| 145 | self.check_wrap(text, 35, expect) |
| 146 | self.check_wrap(text, 36, |
| 147 | ["Em-dashes should be written -- thus."]) |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 148 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 149 | # The improperly written em-dash is handled too, because |
| 150 | # it's adjacent to non-whitespace on both sides. |
| 151 | text = "You can also do--this or even---this." |
| 152 | expect = ["You can also do", |
| 153 | "--this or even", |
| 154 | "---this."] |
| 155 | self.check_wrap(text, 15, expect) |
| 156 | self.check_wrap(text, 16, expect) |
| 157 | expect = ["You can also do--", |
| 158 | "this or even---", |
| 159 | "this."] |
| 160 | self.check_wrap(text, 17, expect) |
| 161 | self.check_wrap(text, 19, expect) |
| 162 | expect = ["You can also do--this or even", |
| 163 | "---this."] |
| 164 | self.check_wrap(text, 29, expect) |
| 165 | self.check_wrap(text, 31, expect) |
| 166 | expect = ["You can also do--this or even---", |
| 167 | "this."] |
| 168 | self.check_wrap(text, 32, expect) |
| 169 | self.check_wrap(text, 35, expect) |
| 170 | |
| 171 | # All of the above behaviour could be deduced by probing the |
| 172 | # _split() method. |
| 173 | text = "Here's an -- em-dash and--here's another---and another!" |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 174 | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
| 175 | "and", "--", "here's", " ", "another", "---", |
| 176 | "and", " ", "another!"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 177 | self.check_split(text, expect) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 178 | |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 179 | text = "and then--bam!--he was gone" |
| 180 | expect = ["and", " ", "then", "--", "bam!", "--", |
| 181 | "he", " ", "was", " ", "gone"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 182 | self.check_split(text, expect) |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 183 | |
| 184 | |
Greg Ward | 34f995b | 2002-08-22 21:10:07 +0000 | [diff] [blame] | 185 | def test_unix_options (self): |
| 186 | # Test that Unix-style command-line options are wrapped correctly. |
| 187 | # Both Optik (OptionParser) and Docutils rely on this behaviour! |
| 188 | |
| 189 | text = "You should use the -n option, or --dry-run in its long form." |
| 190 | self.check_wrap(text, 20, |
| 191 | ["You should use the", |
| 192 | "-n option, or --dry-", |
| 193 | "run in its long", |
| 194 | "form."]) |
| 195 | self.check_wrap(text, 21, |
| 196 | ["You should use the -n", |
| 197 | "option, or --dry-run", |
| 198 | "in its long form."]) |
| 199 | expect = ["You should use the -n option, or", |
| 200 | "--dry-run in its long form."] |
| 201 | self.check_wrap(text, 32, expect) |
| 202 | self.check_wrap(text, 34, expect) |
| 203 | self.check_wrap(text, 35, expect) |
| 204 | self.check_wrap(text, 38, expect) |
| 205 | expect = ["You should use the -n option, or --dry-", |
| 206 | "run in its long form."] |
| 207 | self.check_wrap(text, 39, expect) |
| 208 | self.check_wrap(text, 41, expect) |
| 209 | expect = ["You should use the -n option, or --dry-run", |
| 210 | "in its long form."] |
| 211 | self.check_wrap(text, 42, expect) |
| 212 | |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 213 | # Again, all of the above can be deduced from _split(). |
| 214 | text = "the -n option, or --dry-run or --dryrun" |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 215 | expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", |
| 216 | "--dry-", "run", " ", "or", " ", "--dryrun"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 217 | self.check_split(text, expect) |
| 218 | |
| 219 | def test_funky_hyphens (self): |
| 220 | # Screwy edge cases cooked up by David Goodger. All reported |
| 221 | # in SF bug #596434. |
| 222 | self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"]) |
| 223 | self.check_split("what the--", ["what", " ", "the--"]) |
| 224 | self.check_split("what the--.", ["what", " ", "the--."]) |
| 225 | self.check_split("--text--.", ["--text--."]) |
| 226 | |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 227 | # My initial mis-interpretation of part of the bug report -- |
| 228 | # These were always handled correctly, but it can't hurt to make |
| 229 | # sure that they *stay* correct! |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 230 | self.check_split("--option", ["--option"]) |
| 231 | self.check_split("--option-opt", ["--option-", "opt"]) |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 232 | |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 233 | def test_initial_whitespace(self): |
| 234 | # SF bug #622849 reported inconsistent handling of leading |
| 235 | # whitespace; let's test that a bit, shall we? |
| 236 | text = " This is a sentence with leading whitespace." |
| 237 | self.check_wrap(text, 50, |
| 238 | [" This is a sentence with leading whitespace."]) |
| 239 | self.check_wrap(text, 30, |
| 240 | [" This is a sentence with", "leading whitespace."]) |
| 241 | |
Greg Ward | c7e3c5e | 2002-12-09 16:32:41 +0000 | [diff] [blame] | 242 | def test_unicode(self): |
| 243 | # *Very* simple test of wrapping Unicode strings. I'm sure |
| 244 | # there's more to it than this, but let's at least make |
| 245 | # sure textwrap doesn't crash on Unicode input! |
| 246 | text = u"Hello there, how are you today?" |
| 247 | self.check_wrap(text, 50, [u"Hello there, how are you today?"]) |
| 248 | self.check_wrap(text, 20, [u"Hello there, how are", "you today?"]) |
| 249 | olines = self.wrapper.wrap(text) |
| 250 | assert isinstance(olines, list) and isinstance(olines[0], unicode) |
| 251 | otext = self.wrapper.fill(text) |
| 252 | assert isinstance(otext, unicode) |
| 253 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 254 | def test_split(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 255 | # Ensure that the standard _split() method works as advertised |
| 256 | # in the comments |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 257 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 258 | text = "Hello there -- you goof-ball, use the -b option!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 259 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 260 | result = self.wrapper._split(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 261 | self.check(result, |
| 262 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 263 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 264 | |
| 265 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 266 | class LongWordTestCase (BaseTestCase): |
| 267 | def setUp(self): |
| 268 | self.wrapper = TextWrapper() |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 269 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 270 | Did you say "supercalifragilisticexpialidocious?" |
| 271 | How *do* you spell that odd word, anyways? |
| 272 | ''' |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 273 | |
| 274 | def test_break_long(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 275 | # Wrap text with long words and lots of punctuation |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 276 | |
| 277 | self.check_wrap(self.text, 30, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 278 | ['Did you say "supercalifragilis', |
| 279 | 'ticexpialidocious?" How *do*', |
| 280 | 'you spell that odd word,', |
| 281 | 'anyways?']) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 282 | self.check_wrap(self.text, 50, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 283 | ['Did you say "supercalifragilisticexpialidocious?"', |
| 284 | 'How *do* you spell that odd word, anyways?']) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 285 | |
| 286 | |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 287 | def test_nobreak_long(self): |
| 288 | # Test with break_long_words disabled |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 289 | self.wrapper.break_long_words = 0 |
| 290 | self.wrapper.width = 30 |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 291 | expect = ['Did you say', |
| 292 | '"supercalifragilisticexpialidocious?"', |
| 293 | 'How *do* you spell that odd', |
| 294 | 'word, anyways?' |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 295 | ] |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 296 | result = self.wrapper.wrap(self.text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 297 | self.check(result, expect) |
| 298 | |
| 299 | # Same thing with kwargs passed to standalone wrap() function. |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 300 | result = wrap(self.text, width=30, break_long_words=0) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 301 | self.check(result, expect) |
| 302 | |
| 303 | |
| 304 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 305 | class IndentTestCases(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 306 | |
| 307 | # called before each test method |
| 308 | def setUp(self): |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 309 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 310 | This paragraph will be filled, first without any indentation, |
| 311 | and then with some (including a hanging indent).''' |
| 312 | |
| 313 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 314 | def test_fill(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 315 | # Test the fill() method |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 316 | |
| 317 | expect = '''\ |
| 318 | This paragraph will be filled, first |
| 319 | without any indentation, and then with |
| 320 | some (including a hanging indent).''' |
| 321 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 322 | result = fill(self.text, 40) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 323 | self.check(result, expect) |
| 324 | |
| 325 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 326 | def test_initial_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 327 | # Test initial_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 328 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 329 | expect = [" This paragraph will be filled,", |
| 330 | "first without any indentation, and then", |
| 331 | "with some (including a hanging indent)."] |
| 332 | result = wrap(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 333 | self.check(result, expect) |
| 334 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 335 | expect = "\n".join(expect) |
| 336 | result = fill(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 337 | self.check(result, expect) |
| 338 | |
| 339 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 340 | def test_subsequent_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 341 | # Test subsequent_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 342 | |
| 343 | expect = '''\ |
| 344 | * This paragraph will be filled, first |
| 345 | without any indentation, and then |
| 346 | with some (including a hanging |
| 347 | indent).''' |
| 348 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 349 | result = fill(self.text, 40, |
| 350 | initial_indent=" * ", subsequent_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 351 | self.check(result, expect) |
| 352 | |
| 353 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 354 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame^] | 355 | test_support.run_unittest(WrapTestCase, LongWordTestCase, IndentTestCases) |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 356 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 357 | if __name__ == '__main__': |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 358 | test_main() |