blob: 4a98972fc064aa13f94e3ae3f98c9d66c69eef4f [file] [log] [blame]
Greg Ward3dc94e12002-08-22 18:37:50 +00001#
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 Ward90c0b072002-08-22 18:11:10 +000011import unittest
Greg Wardf6765782002-08-22 18:35:49 +000012from test import test_support
Greg Ward90c0b072002-08-22 18:11:10 +000013
Greg Ward90c0b072002-08-22 18:11:10 +000014from textwrap import TextWrapper, wrap, fill
15
16
Greg Ward13c53c62002-08-22 18:57:26 +000017class BaseTestCase(unittest.TestCase):
Greg Ward90c0b072002-08-22 18:11:10 +000018 '''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):
Guido van Rossum327af772002-08-22 20:13:47 +000032 self.assertEquals(result, expect,
Greg Ward9ad15a32002-08-22 19:47:27 +000033 'expected:\n%s\nbut got:\n%s' % (
34 self.show(expect), self.show(result)))
Greg Ward90c0b072002-08-22 18:11:10 +000035
Guido van Rossumeb287a22002-10-02 15:47:32 +000036 def check_wrap(self, text, width, expect, **kwargs):
37 result = wrap(text, width, **kwargs)
Greg Wardee413842002-08-22 18:55:38 +000038 self.check(result, expect)
39
Greg Wardd1a72a02002-10-31 16:11:18 +000040 def check_split(self, text, expect):
41 result = self.wrapper._split(text)
Greg Ward715debd2002-08-22 21:16:25 +000042 self.assertEquals(result, expect,
43 "\nexpected %r\n"
44 "but got %r" % (expect, result))
45
Greg Ward90c0b072002-08-22 18:11:10 +000046
Greg Ward13c53c62002-08-22 18:57:26 +000047class WrapTestCase(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +000048
Greg Ward90c0b072002-08-22 18:11:10 +000049 def setUp(self):
50 self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
51
Greg Wardf6765782002-08-22 18:35:49 +000052 def test_simple(self):
Guido van Rossum327af772002-08-22 20:13:47 +000053 # Simple case: just words, spaces, and a bit of punctuation
Greg Ward90c0b072002-08-22 18:11:10 +000054
Greg Wardee413842002-08-22 18:55:38 +000055 text = "Hello there, how are you this fine day? I'm glad to hear it!"
Greg Ward90c0b072002-08-22 18:11:10 +000056
Greg Wardee413842002-08-22 18:55:38 +000057 self.check_wrap(text, 12,
58 ["Hello there,",
59 "how are you",
60 "this fine",
61 "day? I'm",
62 "glad to hear",
63 "it!"])
64 self.check_wrap(text, 42,
65 ["Hello there, how are you this fine day?",
66 "I'm glad to hear it!"])
67 self.check_wrap(text, 80, [text])
Greg Ward90c0b072002-08-22 18:11:10 +000068
69
Greg Wardf6765782002-08-22 18:35:49 +000070 def test_whitespace(self):
Guido van Rossum327af772002-08-22 20:13:47 +000071 # Whitespace munging and end-of-sentence detection
Greg Ward90c0b072002-08-22 18:11:10 +000072
Greg Wardee413842002-08-22 18:55:38 +000073 text = """\
Greg Ward90c0b072002-08-22 18:11:10 +000074This is a paragraph that already has
75line breaks. But some of its lines are much longer than the others,
76so it needs to be wrapped.
77Some lines are \ttabbed too.
78What a mess!
79"""
80
Greg Wardee413842002-08-22 18:55:38 +000081 expect = ["This is a paragraph that already has line",
82 "breaks. But some of its lines are much",
83 "longer than the others, so it needs to be",
84 "wrapped. Some lines are tabbed too. What a",
85 "mess!"]
Greg Ward90c0b072002-08-22 18:11:10 +000086
Greg Wardee413842002-08-22 18:55:38 +000087 result = self.wrapper.wrap(text)
Greg Ward90c0b072002-08-22 18:11:10 +000088 self.check(result, expect)
89
Greg Wardee413842002-08-22 18:55:38 +000090 result = self.wrapper.fill(text)
Greg Ward90c0b072002-08-22 18:11:10 +000091 self.check(result, '\n'.join(expect))
92
93
Greg Wardf6765782002-08-22 18:35:49 +000094 def test_wrap_short(self):
Guido van Rossum327af772002-08-22 20:13:47 +000095 # Wrapping to make short lines longer
Greg Ward90c0b072002-08-22 18:11:10 +000096
Greg Wardee413842002-08-22 18:55:38 +000097 text = "This is a\nshort paragraph."
Greg Ward90c0b072002-08-22 18:11:10 +000098
Greg Wardee413842002-08-22 18:55:38 +000099 self.check_wrap(text, 20, ["This is a short",
100 "paragraph."])
101 self.check_wrap(text, 40, ["This is a short paragraph."])
Greg Ward90c0b072002-08-22 18:11:10 +0000102
103
Guido van Rossumeb287a22002-10-02 15:47:32 +0000104 def test_wrap_short_1line(self):
105 # Test endcases
106
107 text = "This is a short line."
108
109 self.check_wrap(text, 30, ["This is a short line."])
110 self.check_wrap(text, 30, ["(1) This is a short line."],
111 initial_indent="(1) ")
112
113
Greg Wardf6765782002-08-22 18:35:49 +0000114 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000115 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000116
Greg Wardee413842002-08-22 18:55:38 +0000117 text = ("this-is-a-useful-feature-for-"
118 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000119
Greg Wardee413842002-08-22 18:55:38 +0000120 self.check_wrap(text, 40,
121 ["this-is-a-useful-feature-for-",
122 "reformatting-posts-from-tim-peters'ly"])
123 self.check_wrap(text, 41,
124 ["this-is-a-useful-feature-for-",
125 "reformatting-posts-from-tim-peters'ly"])
126 self.check_wrap(text, 42,
127 ["this-is-a-useful-feature-for-reformatting-",
128 "posts-from-tim-peters'ly"])
Greg Ward90c0b072002-08-22 18:11:10 +0000129
Greg Ward9ad15a32002-08-22 19:47:27 +0000130 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000131 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000132 text = "Em-dashes should be written -- thus."
133 self.check_wrap(text, 25,
134 ["Em-dashes should be",
135 "written -- thus."])
136
137 # Probe the boundaries of the properly written em-dash,
138 # ie. " -- ".
139 self.check_wrap(text, 29,
140 ["Em-dashes should be written",
141 "-- thus."])
142 expect = ["Em-dashes should be written --",
143 "thus."]
144 self.check_wrap(text, 30, expect)
145 self.check_wrap(text, 35, expect)
146 self.check_wrap(text, 36,
147 ["Em-dashes should be written -- thus."])
Guido van Rossum327af772002-08-22 20:13:47 +0000148
Greg Ward9ad15a32002-08-22 19:47:27 +0000149 # The improperly written em-dash is handled too, because
150 # it's adjacent to non-whitespace on both sides.
151 text = "You can also do--this or even---this."
152 expect = ["You can also do",
153 "--this or even",
154 "---this."]
155 self.check_wrap(text, 15, expect)
156 self.check_wrap(text, 16, expect)
157 expect = ["You can also do--",
158 "this or even---",
159 "this."]
160 self.check_wrap(text, 17, expect)
161 self.check_wrap(text, 19, expect)
162 expect = ["You can also do--this or even",
163 "---this."]
164 self.check_wrap(text, 29, expect)
165 self.check_wrap(text, 31, expect)
166 expect = ["You can also do--this or even---",
167 "this."]
168 self.check_wrap(text, 32, expect)
169 self.check_wrap(text, 35, expect)
170
171 # All of the above behaviour could be deduced by probing the
172 # _split() method.
173 text = "Here's an -- em-dash and--here's another---and another!"
Greg Ward9ad15a32002-08-22 19:47:27 +0000174 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
175 "and", "--", "here's", " ", "another", "---",
176 "and", " ", "another!"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000177 self.check_split(text, expect)
Greg Ward90c0b072002-08-22 18:11:10 +0000178
Greg Wardc6edb372002-08-22 21:27:05 +0000179 text = "and then--bam!--he was gone"
180 expect = ["and", " ", "then", "--", "bam!", "--",
181 "he", " ", "was", " ", "gone"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000182 self.check_split(text, expect)
Greg Wardc6edb372002-08-22 21:27:05 +0000183
184
Greg Ward34f995b2002-08-22 21:10:07 +0000185 def test_unix_options (self):
186 # Test that Unix-style command-line options are wrapped correctly.
187 # Both Optik (OptionParser) and Docutils rely on this behaviour!
188
189 text = "You should use the -n option, or --dry-run in its long form."
190 self.check_wrap(text, 20,
191 ["You should use the",
192 "-n option, or --dry-",
193 "run in its long",
194 "form."])
195 self.check_wrap(text, 21,
196 ["You should use the -n",
197 "option, or --dry-run",
198 "in its long form."])
199 expect = ["You should use the -n option, or",
200 "--dry-run in its long form."]
201 self.check_wrap(text, 32, expect)
202 self.check_wrap(text, 34, expect)
203 self.check_wrap(text, 35, expect)
204 self.check_wrap(text, 38, expect)
205 expect = ["You should use the -n option, or --dry-",
206 "run in its long form."]
207 self.check_wrap(text, 39, expect)
208 self.check_wrap(text, 41, expect)
209 expect = ["You should use the -n option, or --dry-run",
210 "in its long form."]
211 self.check_wrap(text, 42, expect)
212
Greg Ward24a1c9c2002-08-22 21:12:54 +0000213 # Again, all of the above can be deduced from _split().
214 text = "the -n option, or --dry-run or --dryrun"
Greg Ward24a1c9c2002-08-22 21:12:54 +0000215 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
216 "--dry-", "run", " ", "or", " ", "--dryrun"]
Greg Wardd1a72a02002-10-31 16:11:18 +0000217 self.check_split(text, expect)
218
219 def test_funky_hyphens (self):
220 # Screwy edge cases cooked up by David Goodger. All reported
221 # in SF bug #596434.
222 self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
223 self.check_split("what the--", ["what", " ", "the--"])
224 self.check_split("what the--.", ["what", " ", "the--."])
225 self.check_split("--text--.", ["--text--."])
226
227 # I think David got this wrong in the bug report, but it can't
228 # hurt to make sure it stays right!
229 self.check_split("--option", ["--option"])
230 self.check_split("--option-opt", ["--option-", "opt"])
Greg Ward24a1c9c2002-08-22 21:12:54 +0000231
Greg Ward90c0b072002-08-22 18:11:10 +0000232 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000233 # Ensure that the standard _split() method works as advertised
234 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000235
Greg Wardee413842002-08-22 18:55:38 +0000236 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000237
Greg Wardee413842002-08-22 18:55:38 +0000238 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000239 self.check(result,
240 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
241 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
242
243
Greg Wardfd030e42002-08-22 19:02:37 +0000244class LongWordTestCase (BaseTestCase):
245 def setUp(self):
246 self.wrapper = TextWrapper()
247 self.text = '''
Greg Ward90c0b072002-08-22 18:11:10 +0000248Did you say "supercalifragilisticexpialidocious?"
249How *do* you spell that odd word, anyways?
250'''
Greg Wardfd030e42002-08-22 19:02:37 +0000251
252 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000253 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000254
255 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000256 ['Did you say "supercalifragilis',
257 'ticexpialidocious?" How *do*',
258 'you spell that odd word,',
259 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000260 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000261 ['Did you say "supercalifragilisticexpialidocious?"',
262 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000263
264
Guido van Rossum327af772002-08-22 20:13:47 +0000265 def test_nobreak_long(self):
266 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000267 self.wrapper.break_long_words = 0
268 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000269 expect = ['Did you say',
270 '"supercalifragilisticexpialidocious?"',
271 'How *do* you spell that odd',
272 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000273 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000274 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000275 self.check(result, expect)
276
277 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000278 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000279 self.check(result, expect)
280
281
282
Greg Ward13c53c62002-08-22 18:57:26 +0000283class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000284
285 # called before each test method
286 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000287 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000288This paragraph will be filled, first without any indentation,
289and then with some (including a hanging indent).'''
290
291
Greg Wardf6765782002-08-22 18:35:49 +0000292 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000293 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000294
295 expect = '''\
296This paragraph will be filled, first
297without any indentation, and then with
298some (including a hanging indent).'''
299
Greg Wardf69d3c92002-08-22 19:06:45 +0000300 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000301 self.check(result, expect)
302
303
Greg Wardf6765782002-08-22 18:35:49 +0000304 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000305 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000306
Greg Wardf69d3c92002-08-22 19:06:45 +0000307 expect = [" This paragraph will be filled,",
308 "first without any indentation, and then",
309 "with some (including a hanging indent)."]
310 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000311 self.check(result, expect)
312
Greg Wardf69d3c92002-08-22 19:06:45 +0000313 expect = "\n".join(expect)
314 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000315 self.check(result, expect)
316
317
Greg Wardf6765782002-08-22 18:35:49 +0000318 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000319 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000320
321 expect = '''\
322 * This paragraph will be filled, first
323 without any indentation, and then
324 with some (including a hanging
325 indent).'''
326
Greg Wardf69d3c92002-08-22 19:06:45 +0000327 result = fill(self.text, 40,
328 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000329 self.check(result, expect)
330
331
Greg Wardf6765782002-08-22 18:35:49 +0000332def test_main():
333 suite = unittest.TestSuite()
334 suite.addTest(unittest.makeSuite(WrapTestCase))
Greg Wardfd030e42002-08-22 19:02:37 +0000335 suite.addTest(unittest.makeSuite(LongWordTestCase))
Greg Wardf6765782002-08-22 18:35:49 +0000336 suite.addTest(unittest.makeSuite(IndentTestCases))
337 test_support.run_suite(suite)
338
Greg Ward90c0b072002-08-22 18:11:10 +0000339if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000340 test_main()