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