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