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