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 | |
| 17 | class WrapperTestCase(unittest.TestCase): |
| 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): |
| 32 | self.assertEquals(result, expect, |
| 33 | 'Expected:\n%s\nbut got:\n%s' % ( |
| 34 | self.show(result), self.show(expect))) |
| 35 | |
| 36 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 37 | class WrapTestCase(WrapperTestCase): |
| 38 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 39 | def setUp(self): |
| 40 | self.wrapper = TextWrapper(width=45, fix_sentence_endings=True) |
| 41 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 42 | def test_simple(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 43 | '''Simple case: just words, spaces, and a bit of punctuation.''' |
| 44 | |
| 45 | t = "Hello there, how are you this fine day? I'm glad to hear it!" |
| 46 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 47 | subcases = [ |
| 48 | ( (t, 12), [ |
| 49 | "Hello there,", |
| 50 | "how are you", |
| 51 | "this fine", |
| 52 | "day? I'm", |
| 53 | "glad to hear", |
| 54 | "it!" |
| 55 | ] ), |
| 56 | ( (t, 42), [ |
| 57 | "Hello there, how are you this fine day?", |
| 58 | "I'm glad to hear it!" |
| 59 | ] ), |
| 60 | ( (t, 80), [ |
| 61 | t |
| 62 | ] ), |
| 63 | ] |
| 64 | |
| 65 | for test, expect in subcases: |
| 66 | result = wrap(*test) |
| 67 | self.check(result, expect) |
| 68 | |
| 69 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 70 | def test_whitespace(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 71 | '''Whitespace munging and end-of-sentence detection.''' |
| 72 | |
| 73 | t = """\ |
| 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 | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 81 | expect = [ |
| 82 | "This is a paragraph that already has line", |
| 83 | "breaks. But some of its lines are much", |
| 84 | "longer than the others, so it needs to be", |
| 85 | "wrapped. Some lines are tabbed too. What a", |
| 86 | "mess!" |
| 87 | ] |
| 88 | |
| 89 | result = self.wrapper.wrap(t) |
| 90 | self.check(result, expect) |
| 91 | |
| 92 | result = self.wrapper.fill(t) |
| 93 | self.check(result, '\n'.join(expect)) |
| 94 | |
| 95 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 96 | def test_wrap_short(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 97 | '''Wrapping to make short lines longer.''' |
| 98 | |
| 99 | t = "This is a\nshort paragraph." |
| 100 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 101 | subcases = [ |
| 102 | ( (t, 20), [ |
| 103 | "This is a short", |
| 104 | "paragraph." |
| 105 | ] ), |
| 106 | ( (t, 40), [ |
| 107 | "This is a short paragraph." |
| 108 | ] ), |
| 109 | ] |
| 110 | |
| 111 | for test, expect in subcases: |
| 112 | result = wrap(*test) |
| 113 | self.check(result, expect) |
| 114 | |
| 115 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 116 | def test_hyphenated(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 117 | '''Test breaking hyphenated words.''' |
| 118 | |
| 119 | t = "this-is-a-useful-feature-for-reformatting-posts-from-tim-peters'ly" |
| 120 | |
| 121 | subcases = [ |
| 122 | ( (t, 40), [ |
| 123 | "this-is-a-useful-feature-for-", |
| 124 | "reformatting-posts-from-tim-peters'ly" |
| 125 | ] ), |
| 126 | ( (t, 41), [ |
| 127 | "this-is-a-useful-feature-for-", |
| 128 | "reformatting-posts-from-tim-peters'ly" |
| 129 | ] ), |
| 130 | ( (t, 42), [ |
| 131 | "this-is-a-useful-feature-for-reformatting-", |
| 132 | "posts-from-tim-peters'ly" |
| 133 | ] ), |
| 134 | ] |
| 135 | |
| 136 | for test, expect in subcases: |
| 137 | result = wrap(*test) |
| 138 | self.check(result, expect) |
| 139 | |
| 140 | |
| 141 | def test_split(self): |
| 142 | '''Ensure that the standard _split() method works as advertised |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 143 | in the comments.''' |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 144 | |
| 145 | t = "Hello there -- you goof-ball, use the -b option!" |
| 146 | |
| 147 | result = self.wrapper._split(t) |
| 148 | self.check(result, |
| 149 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 150 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 151 | |
| 152 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 153 | def test_funky_punc(self): |
| 154 | '''Wrap text with long words and lots of punctuation.''' |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 155 | |
| 156 | t = ''' |
| 157 | Did you say "supercalifragilisticexpialidocious?" |
| 158 | How *do* you spell that odd word, anyways? |
| 159 | ''' |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 160 | subcases = [ |
| 161 | ( (t, 30), [ |
| 162 | 'Did you say "supercalifragilis', |
| 163 | 'ticexpialidocious?" How *do*', |
| 164 | 'you spell that odd word,', |
| 165 | 'anyways?' |
| 166 | ] ), |
| 167 | ( (t, 50), [ |
| 168 | 'Did you say "supercalifragilisticexpialidocious?"', |
| 169 | 'How *do* you spell that odd word, anyways?' |
| 170 | ] ), |
| 171 | ] |
| 172 | |
| 173 | for test, expect in subcases: |
| 174 | result = wrap(*test) |
| 175 | self.check(result, expect) |
| 176 | |
| 177 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 178 | def test_long_words(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 179 | '''Test with break_long_words disabled.''' |
| 180 | t = ''' |
| 181 | Did you say "supercalifragilisticexpialidocious?" |
| 182 | How *do* you spell that odd word, anyways? |
| 183 | ''' |
| 184 | self.wrapper.break_long_words = 0 |
| 185 | self.wrapper.width = 30 |
| 186 | result = self.wrapper.wrap(t) |
| 187 | expect = [ |
| 188 | 'Did you say', |
| 189 | '"supercalifragilisticexpialidocious?"', |
| 190 | 'How *do* you spell that odd', |
| 191 | 'word, anyways?' |
| 192 | ] |
| 193 | self.check(result, expect) |
| 194 | |
| 195 | # Same thing with kwargs passed to standalone wrap() function. |
| 196 | result = wrap(t, width=30, break_long_words=0) |
| 197 | self.check(result, expect) |
| 198 | |
| 199 | |
| 200 | |
| 201 | class IndentTestCases(WrapperTestCase): |
| 202 | |
| 203 | # called before each test method |
| 204 | def setUp(self): |
| 205 | self.testString = '''\ |
| 206 | This paragraph will be filled, first without any indentation, |
| 207 | and then with some (including a hanging indent).''' |
| 208 | |
| 209 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 210 | def test_fill(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 211 | '''Test the fill() method.''' |
| 212 | |
| 213 | expect = '''\ |
| 214 | This paragraph will be filled, first |
| 215 | without any indentation, and then with |
| 216 | some (including a hanging indent).''' |
| 217 | |
| 218 | result = fill(self.testString, 40) |
| 219 | self.check(result, expect) |
| 220 | |
| 221 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 222 | def test_initial_indent(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 223 | '''Test initial_indent parameter.''' |
| 224 | |
| 225 | expect = [ |
| 226 | " This paragraph will be filled,", |
| 227 | "first without any indentation, and then", |
| 228 | "with some (including a hanging indent)."] |
| 229 | |
| 230 | result = wrap(self.testString, 40, initial_indent=" ") |
| 231 | self.check(result, expect) |
| 232 | |
| 233 | expect = '''\ |
| 234 | This paragraph will be filled, |
| 235 | first without any indentation, and then |
| 236 | with some (including a hanging indent).''' |
| 237 | |
| 238 | result = fill(self.testString, 40, initial_indent=" ") |
| 239 | self.check(result, expect) |
| 240 | |
| 241 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 242 | def test_subsequent_indent(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 243 | '''Test subsequent_indent parameter.''' |
| 244 | |
| 245 | expect = '''\ |
| 246 | * This paragraph will be filled, first |
| 247 | without any indentation, and then |
| 248 | with some (including a hanging |
| 249 | indent).''' |
| 250 | |
| 251 | result = fill(self.testString, 40, initial_indent=" * ", |
| 252 | subsequent_indent=" ") |
| 253 | self.check(result, expect) |
| 254 | |
| 255 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 256 | def test_main(): |
| 257 | suite = unittest.TestSuite() |
| 258 | suite.addTest(unittest.makeSuite(WrapTestCase)) |
| 259 | suite.addTest(unittest.makeSuite(IndentTestCases)) |
| 260 | test_support.run_suite(suite) |
| 261 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 262 | if __name__ == '__main__': |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 263 | test_main() |