blob: fc5d0cc20244736f574d47ae9d5fe5baa1d07bd8 [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
Greg Wardee413842002-08-22 18:55:38 +000036 def check_wrap (self, text, width, expect):
37 result = wrap(text, width)
38 self.check(result, expect)
39
Greg Ward715debd2002-08-22 21:16:25 +000040 def check_split (self, wrapper, text, expect):
41 result = wrapper._split(text)
42 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
Greg Wardf6765782002-08-22 18:35:49 +0000104 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000105 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000106
Greg Wardee413842002-08-22 18:55:38 +0000107 text = ("this-is-a-useful-feature-for-"
108 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000109
Greg Wardee413842002-08-22 18:55:38 +0000110 self.check_wrap(text, 40,
111 ["this-is-a-useful-feature-for-",
112 "reformatting-posts-from-tim-peters'ly"])
113 self.check_wrap(text, 41,
114 ["this-is-a-useful-feature-for-",
115 "reformatting-posts-from-tim-peters'ly"])
116 self.check_wrap(text, 42,
117 ["this-is-a-useful-feature-for-reformatting-",
118 "posts-from-tim-peters'ly"])
Greg Ward90c0b072002-08-22 18:11:10 +0000119
Greg Ward9ad15a32002-08-22 19:47:27 +0000120 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000121 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000122 text = "Em-dashes should be written -- thus."
123 self.check_wrap(text, 25,
124 ["Em-dashes should be",
125 "written -- thus."])
126
127 # Probe the boundaries of the properly written em-dash,
128 # ie. " -- ".
129 self.check_wrap(text, 29,
130 ["Em-dashes should be written",
131 "-- thus."])
132 expect = ["Em-dashes should be written --",
133 "thus."]
134 self.check_wrap(text, 30, expect)
135 self.check_wrap(text, 35, expect)
136 self.check_wrap(text, 36,
137 ["Em-dashes should be written -- thus."])
Guido van Rossum327af772002-08-22 20:13:47 +0000138
Greg Ward9ad15a32002-08-22 19:47:27 +0000139 # The improperly written em-dash is handled too, because
140 # it's adjacent to non-whitespace on both sides.
141 text = "You can also do--this or even---this."
142 expect = ["You can also do",
143 "--this or even",
144 "---this."]
145 self.check_wrap(text, 15, expect)
146 self.check_wrap(text, 16, expect)
147 expect = ["You can also do--",
148 "this or even---",
149 "this."]
150 self.check_wrap(text, 17, expect)
151 self.check_wrap(text, 19, expect)
152 expect = ["You can also do--this or even",
153 "---this."]
154 self.check_wrap(text, 29, expect)
155 self.check_wrap(text, 31, expect)
156 expect = ["You can also do--this or even---",
157 "this."]
158 self.check_wrap(text, 32, expect)
159 self.check_wrap(text, 35, expect)
160
161 # All of the above behaviour could be deduced by probing the
162 # _split() method.
163 text = "Here's an -- em-dash and--here's another---and another!"
Greg Ward9ad15a32002-08-22 19:47:27 +0000164 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
165 "and", "--", "here's", " ", "another", "---",
166 "and", " ", "another!"]
Greg Ward715debd2002-08-22 21:16:25 +0000167 self.check_split(self.wrapper, text, expect)
Greg Ward90c0b072002-08-22 18:11:10 +0000168
Greg Wardc6edb372002-08-22 21:27:05 +0000169 text = "and then--bam!--he was gone"
170 expect = ["and", " ", "then", "--", "bam!", "--",
171 "he", " ", "was", " ", "gone"]
172 self.check_split(self.wrapper, text, expect)
173
174
Greg Ward34f995b2002-08-22 21:10:07 +0000175 def test_unix_options (self):
176 # Test that Unix-style command-line options are wrapped correctly.
177 # Both Optik (OptionParser) and Docutils rely on this behaviour!
178
179 text = "You should use the -n option, or --dry-run in its long form."
180 self.check_wrap(text, 20,
181 ["You should use the",
182 "-n option, or --dry-",
183 "run in its long",
184 "form."])
185 self.check_wrap(text, 21,
186 ["You should use the -n",
187 "option, or --dry-run",
188 "in its long form."])
189 expect = ["You should use the -n option, or",
190 "--dry-run in its long form."]
191 self.check_wrap(text, 32, expect)
192 self.check_wrap(text, 34, expect)
193 self.check_wrap(text, 35, expect)
194 self.check_wrap(text, 38, expect)
195 expect = ["You should use the -n option, or --dry-",
196 "run in its long form."]
197 self.check_wrap(text, 39, expect)
198 self.check_wrap(text, 41, expect)
199 expect = ["You should use the -n option, or --dry-run",
200 "in its long form."]
201 self.check_wrap(text, 42, expect)
202
Greg Ward24a1c9c2002-08-22 21:12:54 +0000203 # Again, all of the above can be deduced from _split().
204 text = "the -n option, or --dry-run or --dryrun"
Greg Ward24a1c9c2002-08-22 21:12:54 +0000205 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
206 "--dry-", "run", " ", "or", " ", "--dryrun"]
Greg Ward715debd2002-08-22 21:16:25 +0000207 self.check_split(self.wrapper, text, expect)
Greg Ward24a1c9c2002-08-22 21:12:54 +0000208
Greg Ward90c0b072002-08-22 18:11:10 +0000209 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000210 # Ensure that the standard _split() method works as advertised
211 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000212
Greg Wardee413842002-08-22 18:55:38 +0000213 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000214
Greg Wardee413842002-08-22 18:55:38 +0000215 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000216 self.check(result,
217 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
218 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
219
220
Greg Wardfd030e42002-08-22 19:02:37 +0000221class LongWordTestCase (BaseTestCase):
222 def setUp(self):
223 self.wrapper = TextWrapper()
224 self.text = '''
Greg Ward90c0b072002-08-22 18:11:10 +0000225Did you say "supercalifragilisticexpialidocious?"
226How *do* you spell that odd word, anyways?
227'''
Greg Wardfd030e42002-08-22 19:02:37 +0000228
229 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000230 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000231
232 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000233 ['Did you say "supercalifragilis',
234 'ticexpialidocious?" How *do*',
235 'you spell that odd word,',
236 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000237 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000238 ['Did you say "supercalifragilisticexpialidocious?"',
239 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000240
241
Guido van Rossum327af772002-08-22 20:13:47 +0000242 def test_nobreak_long(self):
243 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000244 self.wrapper.break_long_words = 0
245 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000246 expect = ['Did you say',
247 '"supercalifragilisticexpialidocious?"',
248 'How *do* you spell that odd',
249 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000250 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000251 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000252 self.check(result, expect)
253
254 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000255 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000256 self.check(result, expect)
257
258
259
Greg Ward13c53c62002-08-22 18:57:26 +0000260class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000261
262 # called before each test method
263 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000264 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000265This paragraph will be filled, first without any indentation,
266and then with some (including a hanging indent).'''
267
268
Greg Wardf6765782002-08-22 18:35:49 +0000269 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000270 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000271
272 expect = '''\
273This paragraph will be filled, first
274without any indentation, and then with
275some (including a hanging indent).'''
276
Greg Wardf69d3c92002-08-22 19:06:45 +0000277 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000278 self.check(result, expect)
279
280
Greg Wardf6765782002-08-22 18:35:49 +0000281 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000282 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000283
Greg Wardf69d3c92002-08-22 19:06:45 +0000284 expect = [" This paragraph will be filled,",
285 "first without any indentation, and then",
286 "with some (including a hanging indent)."]
287 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000288 self.check(result, expect)
289
Greg Wardf69d3c92002-08-22 19:06:45 +0000290 expect = "\n".join(expect)
291 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000292 self.check(result, expect)
293
294
Greg Wardf6765782002-08-22 18:35:49 +0000295 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000296 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000297
298 expect = '''\
299 * This paragraph will be filled, first
300 without any indentation, and then
301 with some (including a hanging
302 indent).'''
303
Greg Wardf69d3c92002-08-22 19:06:45 +0000304 result = fill(self.text, 40,
305 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000306 self.check(result, expect)
307
308
Greg Wardf6765782002-08-22 18:35:49 +0000309def test_main():
310 suite = unittest.TestSuite()
311 suite.addTest(unittest.makeSuite(WrapTestCase))
Greg Wardfd030e42002-08-22 19:02:37 +0000312 suite.addTest(unittest.makeSuite(LongWordTestCase))
Greg Wardf6765782002-08-22 18:35:49 +0000313 suite.addTest(unittest.makeSuite(IndentTestCases))
314 test_support.run_suite(suite)
315
Greg Ward90c0b072002-08-22 18:11:10 +0000316if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000317 test_main()