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