blob: bec5ac5902653162af6d0f73eef5c1b510e99752 [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):
32 self.assertEquals(result, expect,
33 'Expected:\n%s\nbut got:\n%s' % (
34 self.show(result), self.show(expect)))
35
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):
Greg Ward90c0b072002-08-22 18:11:10 +000047 '''Simple case: just words, spaces, and a bit of punctuation.'''
48
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):
Greg Ward90c0b072002-08-22 18:11:10 +000065 '''Whitespace munging and end-of-sentence detection.'''
66
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):
Greg Ward90c0b072002-08-22 18:11:10 +000089 '''Wrapping to make short lines longer.'''
90
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):
Greg Ward90c0b072002-08-22 18:11:10 +000099 '''Test breaking hyphenated words.'''
100
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
114
115 def test_split(self):
116 '''Ensure that the standard _split() method works as advertised
Greg Wardf6765782002-08-22 18:35:49 +0000117 in the comments.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000118
Greg Wardee413842002-08-22 18:55:38 +0000119 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000120
Greg Wardee413842002-08-22 18:55:38 +0000121 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000122 self.check(result,
123 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
124 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
125
126
Greg Wardfd030e42002-08-22 19:02:37 +0000127class LongWordTestCase (BaseTestCase):
128 def setUp(self):
129 self.wrapper = TextWrapper()
130 self.text = '''
Greg Ward90c0b072002-08-22 18:11:10 +0000131Did you say "supercalifragilisticexpialidocious?"
132How *do* you spell that odd word, anyways?
133'''
Greg Wardfd030e42002-08-22 19:02:37 +0000134
135 def test_break_long(self):
136 '''Wrap text with long words and lots of punctuation.'''
137
138 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000139 ['Did you say "supercalifragilis',
140 'ticexpialidocious?" How *do*',
141 'you spell that odd word,',
142 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000143 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000144 ['Did you say "supercalifragilisticexpialidocious?"',
145 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000146
147
Greg Wardfd030e42002-08-22 19:02:37 +0000148 def test_nobreak_long(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000149 '''Test with break_long_words disabled.'''
Greg Ward90c0b072002-08-22 18:11:10 +0000150 self.wrapper.break_long_words = 0
151 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000152 expect = ['Did you say',
153 '"supercalifragilisticexpialidocious?"',
154 'How *do* you spell that odd',
155 'word, anyways?'
156 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000157 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000158 self.check(result, expect)
159
160 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000161 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000162 self.check(result, expect)
163
164
165
Greg Ward13c53c62002-08-22 18:57:26 +0000166class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000167
168 # called before each test method
169 def setUp(self):
170 self.testString = '''\
171This paragraph will be filled, first without any indentation,
172and then with some (including a hanging indent).'''
173
174
Greg Wardf6765782002-08-22 18:35:49 +0000175 def test_fill(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000176 '''Test the fill() method.'''
177
178 expect = '''\
179This paragraph will be filled, first
180without any indentation, and then with
181some (including a hanging indent).'''
182
183 result = fill(self.testString, 40)
184 self.check(result, expect)
185
186
Greg Wardf6765782002-08-22 18:35:49 +0000187 def test_initial_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000188 '''Test initial_indent parameter.'''
189
190 expect = [
191 " This paragraph will be filled,",
192 "first without any indentation, and then",
193 "with some (including a hanging indent)."]
194
195 result = wrap(self.testString, 40, initial_indent=" ")
196 self.check(result, expect)
197
198 expect = '''\
199 This paragraph will be filled,
200first without any indentation, and then
201with some (including a hanging indent).'''
202
203 result = fill(self.testString, 40, initial_indent=" ")
204 self.check(result, expect)
205
206
Greg Wardf6765782002-08-22 18:35:49 +0000207 def test_subsequent_indent(self):
Greg Ward90c0b072002-08-22 18:11:10 +0000208 '''Test subsequent_indent parameter.'''
209
210 expect = '''\
211 * This paragraph will be filled, first
212 without any indentation, and then
213 with some (including a hanging
214 indent).'''
215
216 result = fill(self.testString, 40, initial_indent=" * ",
217 subsequent_indent=" ")
218 self.check(result, expect)
219
220
Greg Wardf6765782002-08-22 18:35:49 +0000221def test_main():
222 suite = unittest.TestSuite()
223 suite.addTest(unittest.makeSuite(WrapTestCase))
Greg Wardfd030e42002-08-22 19:02:37 +0000224 suite.addTest(unittest.makeSuite(LongWordTestCase))
Greg Wardf6765782002-08-22 18:35:49 +0000225 suite.addTest(unittest.makeSuite(IndentTestCases))
226 test_support.run_suite(suite)
227
Greg Ward90c0b072002-08-22 18:11:10 +0000228if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000229 test_main()