blob: 838596d78fc9dafdbffb269d64c0bcc1eb4814d5 [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 Ward90c0b072002-08-22 18:11:10 +000040
Greg Ward13c53c62002-08-22 18:57:26 +000041class WrapTestCase(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +000042
Greg Ward90c0b072002-08-22 18:11:10 +000043 def setUp(self):
44 self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
45
Greg Wardf6765782002-08-22 18:35:49 +000046 def test_simple(self):
Guido van Rossum327af772002-08-22 20:13:47 +000047 # Simple case: just words, spaces, and a bit of punctuation
Greg Ward90c0b072002-08-22 18:11:10 +000048
Greg Wardee413842002-08-22 18:55:38 +000049 text = "Hello there, how are you this fine day? I'm glad to hear it!"
Greg Ward90c0b072002-08-22 18:11:10 +000050
Greg Wardee413842002-08-22 18:55:38 +000051 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 Ward90c0b072002-08-22 18:11:10 +000062
63
Greg Wardf6765782002-08-22 18:35:49 +000064 def test_whitespace(self):
Guido van Rossum327af772002-08-22 20:13:47 +000065 # Whitespace munging and end-of-sentence detection
Greg Ward90c0b072002-08-22 18:11:10 +000066
Greg Wardee413842002-08-22 18:55:38 +000067 text = """\
Greg Ward90c0b072002-08-22 18:11:10 +000068This is a paragraph that already has
69line breaks. But some of its lines are much longer than the others,
70so it needs to be wrapped.
71Some lines are \ttabbed too.
72What a mess!
73"""
74
Greg Wardee413842002-08-22 18:55:38 +000075 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 Ward90c0b072002-08-22 18:11:10 +000080
Greg Wardee413842002-08-22 18:55:38 +000081 result = self.wrapper.wrap(text)
Greg Ward90c0b072002-08-22 18:11:10 +000082 self.check(result, expect)
83
Greg Wardee413842002-08-22 18:55:38 +000084 result = self.wrapper.fill(text)
Greg Ward90c0b072002-08-22 18:11:10 +000085 self.check(result, '\n'.join(expect))
86
87
Greg Wardf6765782002-08-22 18:35:49 +000088 def test_wrap_short(self):
Guido van Rossum327af772002-08-22 20:13:47 +000089 # Wrapping to make short lines longer
Greg Ward90c0b072002-08-22 18:11:10 +000090
Greg Wardee413842002-08-22 18:55:38 +000091 text = "This is a\nshort paragraph."
Greg Ward90c0b072002-08-22 18:11:10 +000092
Greg Wardee413842002-08-22 18:55:38 +000093 self.check_wrap(text, 20, ["This is a short",
94 "paragraph."])
95 self.check_wrap(text, 40, ["This is a short paragraph."])
Greg Ward90c0b072002-08-22 18:11:10 +000096
97
Greg Wardf6765782002-08-22 18:35:49 +000098 def test_hyphenated(self):
Guido van Rossum327af772002-08-22 20:13:47 +000099 # Test breaking hyphenated words
Greg Ward90c0b072002-08-22 18:11:10 +0000100
Greg Wardee413842002-08-22 18:55:38 +0000101 text = ("this-is-a-useful-feature-for-"
102 "reformatting-posts-from-tim-peters'ly")
Greg Ward90c0b072002-08-22 18:11:10 +0000103
Greg Wardee413842002-08-22 18:55:38 +0000104 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 Ward90c0b072002-08-22 18:11:10 +0000113
Greg Ward9ad15a32002-08-22 19:47:27 +0000114 def test_em_dash(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000115 # Test text with em-dashes
Greg Ward9ad15a32002-08-22 19:47:27 +0000116 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."])
Guido van Rossum327af772002-08-22 20:13:47 +0000132
Greg Ward9ad15a32002-08-22 19:47:27 +0000133 # 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 Ward90c0b072002-08-22 18:11:10 +0000165
Greg Ward34f995b2002-08-22 21:10:07 +0000166 def test_unix_options (self):
167 # Test that Unix-style command-line options are wrapped correctly.
168 # Both Optik (OptionParser) and Docutils rely on this behaviour!
169
170 text = "You should use the -n option, or --dry-run in its long form."
171 self.check_wrap(text, 20,
172 ["You should use the",
173 "-n option, or --dry-",
174 "run in its long",
175 "form."])
176 self.check_wrap(text, 21,
177 ["You should use the -n",
178 "option, or --dry-run",
179 "in its long form."])
180 expect = ["You should use the -n option, or",
181 "--dry-run in its long form."]
182 self.check_wrap(text, 32, expect)
183 self.check_wrap(text, 34, expect)
184 self.check_wrap(text, 35, expect)
185 self.check_wrap(text, 38, expect)
186 expect = ["You should use the -n option, or --dry-",
187 "run in its long form."]
188 self.check_wrap(text, 39, expect)
189 self.check_wrap(text, 41, expect)
190 expect = ["You should use the -n option, or --dry-run",
191 "in its long form."]
192 self.check_wrap(text, 42, expect)
193
Greg Ward24a1c9c2002-08-22 21:12:54 +0000194 # Again, all of the above can be deduced from _split().
195 text = "the -n option, or --dry-run or --dryrun"
196 result = self.wrapper._split(text)
197 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
198 "--dry-", "run", " ", "or", " ", "--dryrun"]
199 self.assertEquals(result, expect,
200 "\nexpected %r\n"
201 "but got %r" % (expect, result))
202
Greg Ward90c0b072002-08-22 18:11:10 +0000203 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000204 # Ensure that the standard _split() method works as advertised
205 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000206
Greg Wardee413842002-08-22 18:55:38 +0000207 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000208
Greg Wardee413842002-08-22 18:55:38 +0000209 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000210 self.check(result,
211 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
212 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
213
214
Greg Wardfd030e42002-08-22 19:02:37 +0000215class LongWordTestCase (BaseTestCase):
216 def setUp(self):
217 self.wrapper = TextWrapper()
218 self.text = '''
Greg Ward90c0b072002-08-22 18:11:10 +0000219Did you say "supercalifragilisticexpialidocious?"
220How *do* you spell that odd word, anyways?
221'''
Greg Wardfd030e42002-08-22 19:02:37 +0000222
223 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000224 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000225
226 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000227 ['Did you say "supercalifragilis',
228 'ticexpialidocious?" How *do*',
229 'you spell that odd word,',
230 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000231 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000232 ['Did you say "supercalifragilisticexpialidocious?"',
233 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000234
235
Guido van Rossum327af772002-08-22 20:13:47 +0000236 def test_nobreak_long(self):
237 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000238 self.wrapper.break_long_words = 0
239 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000240 expect = ['Did you say',
241 '"supercalifragilisticexpialidocious?"',
242 'How *do* you spell that odd',
243 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000244 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000245 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000246 self.check(result, expect)
247
248 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000249 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000250 self.check(result, expect)
251
252
253
Greg Ward13c53c62002-08-22 18:57:26 +0000254class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000255
256 # called before each test method
257 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000258 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000259This paragraph will be filled, first without any indentation,
260and then with some (including a hanging indent).'''
261
262
Greg Wardf6765782002-08-22 18:35:49 +0000263 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000264 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000265
266 expect = '''\
267This paragraph will be filled, first
268without any indentation, and then with
269some (including a hanging indent).'''
270
Greg Wardf69d3c92002-08-22 19:06:45 +0000271 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000272 self.check(result, expect)
273
274
Greg Wardf6765782002-08-22 18:35:49 +0000275 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000276 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000277
Greg Wardf69d3c92002-08-22 19:06:45 +0000278 expect = [" This paragraph will be filled,",
279 "first without any indentation, and then",
280 "with some (including a hanging indent)."]
281 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000282 self.check(result, expect)
283
Greg Wardf69d3c92002-08-22 19:06:45 +0000284 expect = "\n".join(expect)
285 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000286 self.check(result, expect)
287
288
Greg Wardf6765782002-08-22 18:35:49 +0000289 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000290 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000291
292 expect = '''\
293 * This paragraph will be filled, first
294 without any indentation, and then
295 with some (including a hanging
296 indent).'''
297
Greg Wardf69d3c92002-08-22 19:06:45 +0000298 result = fill(self.text, 40,
299 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000300 self.check(result, expect)
301
302
Greg Wardf6765782002-08-22 18:35:49 +0000303def test_main():
304 suite = unittest.TestSuite()
305 suite.addTest(unittest.makeSuite(WrapTestCase))
Greg Wardfd030e42002-08-22 19:02:37 +0000306 suite.addTest(unittest.makeSuite(LongWordTestCase))
Greg Wardf6765782002-08-22 18:35:49 +0000307 suite.addTest(unittest.makeSuite(IndentTestCases))
308 test_support.run_suite(suite)
309
Greg Ward90c0b072002-08-22 18:11:10 +0000310if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000311 test_main()