Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Lib / test / test_textwrap.py
blobb1cee3053914b7d9621b236b44bc0516ed2506cd
2 # Test script for the textwrap module.
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.
8 # $Id$
11 import unittest
12 from test import test_support
14 from textwrap import TextWrapper, wrap, fill
17 class BaseTestCase(unittest.TestCase):
18 '''Parent class with utility methods for textwrap tests.'''
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
31 def check(self, result, expect):
32 self.assertEquals(result, expect,
33 'expected:\n%s\nbut got:\n%s' % (
34 self.show(expect), self.show(result)))
36 def check_wrap(self, text, width, expect, **kwargs):
37 result = wrap(text, width, **kwargs)
38 self.check(result, expect)
40 def check_split(self, text, expect):
41 result = self.wrapper._split(text)
42 self.assertEquals(result, expect,
43 "\nexpected %r\n"
44 "but got %r" % (expect, result))
47 class WrapTestCase(BaseTestCase):
49 def setUp(self):
50 self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
52 def test_simple(self):
53 # Simple case: just words, spaces, and a bit of punctuation
55 text = "Hello there, how are you this fine day? I'm glad to hear it!"
57 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])
70 def test_whitespace(self):
71 # Whitespace munging and end-of-sentence detection
73 text = """\
74 This is a paragraph that already has
75 line breaks. But some of its lines are much longer than the others,
76 so it needs to be wrapped.
77 Some lines are \ttabbed too.
78 What a mess!
79 """
81 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!"]
87 result = self.wrapper.wrap(text)
88 self.check(result, expect)
90 result = self.wrapper.fill(text)
91 self.check(result, '\n'.join(expect))
94 def test_wrap_short(self):
95 # Wrapping to make short lines longer
97 text = "This is a\nshort paragraph."
99 self.check_wrap(text, 20, ["This is a short",
100 "paragraph."])
101 self.check_wrap(text, 40, ["This is a short paragraph."])
104 def test_wrap_short_1line(self):
105 # Test endcases
107 text = "This is a short line."
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) ")
114 def test_hyphenated(self):
115 # Test breaking hyphenated words
117 text = ("this-is-a-useful-feature-for-"
118 "reformatting-posts-from-tim-peters'ly")
120 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"])
130 def test_em_dash(self):
131 # Test text with em-dashes
132 text = "Em-dashes should be written -- thus."
133 self.check_wrap(text, 25,
134 ["Em-dashes should be",
135 "written -- thus."])
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."])
149 # 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)
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!"
174 expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
175 "and", "--", "here's", " ", "another", "---",
176 "and", " ", "another!"]
177 self.check_split(text, expect)
179 text = "and then--bam!--he was gone"
180 expect = ["and", " ", "then", "--", "bam!", "--",
181 "he", " ", "was", " ", "gone"]
182 self.check_split(text, expect)
185 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!
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)
213 # Again, all of the above can be deduced from _split().
214 text = "the -n option, or --dry-run or --dryrun"
215 expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
216 "--dry-", "run", " ", "or", " ", "--dryrun"]
217 self.check_split(text, expect)
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--."])
227 # My initial mis-interpretation of part of the bug report --
228 # These were always handled correctly, but it can't hurt to make
229 # sure that they *stay* correct!
230 self.check_split("--option", ["--option"])
231 self.check_split("--option-opt", ["--option-", "opt"])
233 def test_initial_whitespace(self):
234 # SF bug #622849 reported inconsistent handling of leading
235 # whitespace; let's test that a bit, shall we?
236 text = " This is a sentence with leading whitespace."
237 self.check_wrap(text, 50,
238 [" This is a sentence with leading whitespace."])
239 self.check_wrap(text, 30,
240 [" This is a sentence with", "leading whitespace."])
242 def test_unicode(self):
243 # *Very* simple test of wrapping Unicode strings. I'm sure
244 # there's more to it than this, but let's at least make
245 # sure textwrap doesn't crash on Unicode input!
246 text = u"Hello there, how are you today?"
247 self.check_wrap(text, 50, [u"Hello there, how are you today?"])
248 self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
249 olines = self.wrapper.wrap(text)
250 assert isinstance(olines, list) and isinstance(olines[0], unicode)
251 otext = self.wrapper.fill(text)
252 assert isinstance(otext, unicode)
254 def test_split(self):
255 # Ensure that the standard _split() method works as advertised
256 # in the comments
258 text = "Hello there -- you goof-ball, use the -b option!"
260 result = self.wrapper._split(text)
261 self.check(result,
262 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
263 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
266 class LongWordTestCase (BaseTestCase):
267 def setUp(self):
268 self.wrapper = TextWrapper()
269 self.text = '''\
270 Did you say "supercalifragilisticexpialidocious?"
271 How *do* you spell that odd word, anyways?
274 def test_break_long(self):
275 # Wrap text with long words and lots of punctuation
277 self.check_wrap(self.text, 30,
278 ['Did you say "supercalifragilis',
279 'ticexpialidocious?" How *do*',
280 'you spell that odd word,',
281 'anyways?'])
282 self.check_wrap(self.text, 50,
283 ['Did you say "supercalifragilisticexpialidocious?"',
284 'How *do* you spell that odd word, anyways?'])
287 def test_nobreak_long(self):
288 # Test with break_long_words disabled
289 self.wrapper.break_long_words = 0
290 self.wrapper.width = 30
291 expect = ['Did you say',
292 '"supercalifragilisticexpialidocious?"',
293 'How *do* you spell that odd',
294 'word, anyways?'
296 result = self.wrapper.wrap(self.text)
297 self.check(result, expect)
299 # Same thing with kwargs passed to standalone wrap() function.
300 result = wrap(self.text, width=30, break_long_words=0)
301 self.check(result, expect)
305 class IndentTestCases(BaseTestCase):
307 # called before each test method
308 def setUp(self):
309 self.text = '''\
310 This paragraph will be filled, first without any indentation,
311 and then with some (including a hanging indent).'''
314 def test_fill(self):
315 # Test the fill() method
317 expect = '''\
318 This paragraph will be filled, first
319 without any indentation, and then with
320 some (including a hanging indent).'''
322 result = fill(self.text, 40)
323 self.check(result, expect)
326 def test_initial_indent(self):
327 # Test initial_indent parameter
329 expect = [" This paragraph will be filled,",
330 "first without any indentation, and then",
331 "with some (including a hanging indent)."]
332 result = wrap(self.text, 40, initial_indent=" ")
333 self.check(result, expect)
335 expect = "\n".join(expect)
336 result = fill(self.text, 40, initial_indent=" ")
337 self.check(result, expect)
340 def test_subsequent_indent(self):
341 # Test subsequent_indent parameter
343 expect = '''\
344 * This paragraph will be filled, first
345 without any indentation, and then
346 with some (including a hanging
347 indent).'''
349 result = fill(self.text, 40,
350 initial_indent=" * ", subsequent_indent=" ")
351 self.check(result, expect)
354 def test_main():
355 suite = unittest.TestSuite()
356 suite.addTest(unittest.makeSuite(WrapTestCase))
357 suite.addTest(unittest.makeSuite(LongWordTestCase))
358 suite.addTest(unittest.makeSuite(IndentTestCases))
359 test_support.run_suite(suite)
361 if __name__ == '__main__':
362 test_main()