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 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 17 | class BaseTestCase(unittest.TestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 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, |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 33 | 'expected:\n%s\nbut got:\n%s' % ( |
| 34 | self.show(expect), self.show(result))) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 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 | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 41 | class WrapTestCase(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 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 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 114 | def test_em_dash(self): |
| 115 | '''Test text with em-dashes.''' |
| 116 | text = "Em-dashes should be written -- thus." |
| 117 | self.check_wrap(text, 25, |
| 118 | ["Em-dashes should be", |
| 119 | "written -- thus."]) |
| 120 | |
| 121 | # Probe the boundaries of the properly written em-dash, |
| 122 | # ie. " -- ". |
| 123 | self.check_wrap(text, 29, |
| 124 | ["Em-dashes should be written", |
| 125 | "-- thus."]) |
| 126 | expect = ["Em-dashes should be written --", |
| 127 | "thus."] |
| 128 | self.check_wrap(text, 30, expect) |
| 129 | self.check_wrap(text, 35, expect) |
| 130 | self.check_wrap(text, 36, |
| 131 | ["Em-dashes should be written -- thus."]) |
| 132 | |
| 133 | # The improperly written em-dash is handled too, because |
| 134 | # it's adjacent to non-whitespace on both sides. |
| 135 | text = "You can also do--this or even---this." |
| 136 | expect = ["You can also do", |
| 137 | "--this or even", |
| 138 | "---this."] |
| 139 | self.check_wrap(text, 15, expect) |
| 140 | self.check_wrap(text, 16, expect) |
| 141 | expect = ["You can also do--", |
| 142 | "this or even---", |
| 143 | "this."] |
| 144 | self.check_wrap(text, 17, expect) |
| 145 | self.check_wrap(text, 19, expect) |
| 146 | expect = ["You can also do--this or even", |
| 147 | "---this."] |
| 148 | self.check_wrap(text, 29, expect) |
| 149 | self.check_wrap(text, 31, expect) |
| 150 | expect = ["You can also do--this or even---", |
| 151 | "this."] |
| 152 | self.check_wrap(text, 32, expect) |
| 153 | self.check_wrap(text, 35, expect) |
| 154 | |
| 155 | # All of the above behaviour could be deduced by probing the |
| 156 | # _split() method. |
| 157 | text = "Here's an -- em-dash and--here's another---and another!" |
| 158 | result = self.wrapper._split(text) |
| 159 | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
| 160 | "and", "--", "here's", " ", "another", "---", |
| 161 | "and", " ", "another!"] |
| 162 | self.assertEquals(result, expect, |
| 163 | "\nexpected %r\n" |
| 164 | "but got %r" % (expect, result)) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 165 | |
| 166 | def test_split(self): |
| 167 | '''Ensure that the standard _split() method works as advertised |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 168 | in the comments.''' |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 169 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 170 | text = "Hello there -- you goof-ball, use the -b option!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 171 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 172 | result = self.wrapper._split(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 173 | self.check(result, |
| 174 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 175 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 176 | |
| 177 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 178 | class LongWordTestCase (BaseTestCase): |
| 179 | def setUp(self): |
| 180 | self.wrapper = TextWrapper() |
| 181 | self.text = ''' |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 182 | Did you say "supercalifragilisticexpialidocious?" |
| 183 | How *do* you spell that odd word, anyways? |
| 184 | ''' |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 185 | |
| 186 | def test_break_long(self): |
| 187 | '''Wrap text with long words and lots of punctuation.''' |
| 188 | |
| 189 | self.check_wrap(self.text, 30, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 190 | ['Did you say "supercalifragilis', |
| 191 | 'ticexpialidocious?" How *do*', |
| 192 | 'you spell that odd word,', |
| 193 | 'anyways?']) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 194 | self.check_wrap(self.text, 50, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 195 | ['Did you say "supercalifragilisticexpialidocious?"', |
| 196 | 'How *do* you spell that odd word, anyways?']) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 197 | |
| 198 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 199 | def test_nobreak_long(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 200 | '''Test with break_long_words disabled.''' |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 201 | self.wrapper.break_long_words = 0 |
| 202 | self.wrapper.width = 30 |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 203 | expect = ['Did you say', |
| 204 | '"supercalifragilisticexpialidocious?"', |
| 205 | 'How *do* you spell that odd', |
| 206 | 'word, anyways?' |
| 207 | ] |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 208 | result = self.wrapper.wrap(self.text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 209 | self.check(result, expect) |
| 210 | |
| 211 | # Same thing with kwargs passed to standalone wrap() function. |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 212 | result = wrap(self.text, width=30, break_long_words=0) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 213 | self.check(result, expect) |
| 214 | |
| 215 | |
| 216 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 217 | class IndentTestCases(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 218 | |
| 219 | # called before each test method |
| 220 | def setUp(self): |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 221 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 222 | This paragraph will be filled, first without any indentation, |
| 223 | and then with some (including a hanging indent).''' |
| 224 | |
| 225 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 226 | def test_fill(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 227 | '''Test the fill() method.''' |
| 228 | |
| 229 | expect = '''\ |
| 230 | This paragraph will be filled, first |
| 231 | without any indentation, and then with |
| 232 | some (including a hanging indent).''' |
| 233 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 234 | result = fill(self.text, 40) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 235 | self.check(result, expect) |
| 236 | |
| 237 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 238 | def test_initial_indent(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 239 | '''Test initial_indent parameter.''' |
| 240 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 241 | expect = [" This paragraph will be filled,", |
| 242 | "first without any indentation, and then", |
| 243 | "with some (including a hanging indent)."] |
| 244 | result = wrap(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 245 | self.check(result, expect) |
| 246 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 247 | expect = "\n".join(expect) |
| 248 | result = fill(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 249 | self.check(result, expect) |
| 250 | |
| 251 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 252 | def test_subsequent_indent(self): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 253 | '''Test subsequent_indent parameter.''' |
| 254 | |
| 255 | expect = '''\ |
| 256 | * This paragraph will be filled, first |
| 257 | without any indentation, and then |
| 258 | with some (including a hanging |
| 259 | indent).''' |
| 260 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 261 | result = fill(self.text, 40, |
| 262 | initial_indent=" * ", subsequent_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 263 | self.check(result, expect) |
| 264 | |
| 265 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 266 | def test_main(): |
| 267 | suite = unittest.TestSuite() |
| 268 | suite.addTest(unittest.makeSuite(WrapTestCase)) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 269 | suite.addTest(unittest.makeSuite(LongWordTestCase)) |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 270 | suite.addTest(unittest.makeSuite(IndentTestCases)) |
| 271 | test_support.run_suite(suite) |
| 272 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 273 | if __name__ == '__main__': |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 274 | test_main() |