blob: a19e3ba89afff6e44c9f059b14dda5759e74eb45 [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
166 def test_split(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000167 # Ensure that the standard _split() method works as advertised
168 # in the comments
Greg Ward90c0b072002-08-22 18:11:10 +0000169
Greg Wardee413842002-08-22 18:55:38 +0000170 text = "Hello there -- you goof-ball, use the -b option!"
Greg Ward90c0b072002-08-22 18:11:10 +0000171
Greg Wardee413842002-08-22 18:55:38 +0000172 result = self.wrapper._split(text)
Greg Ward90c0b072002-08-22 18:11:10 +0000173 self.check(result,
174 ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
175 "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
176
177
Greg Wardfd030e42002-08-22 19:02:37 +0000178class LongWordTestCase (BaseTestCase):
179 def setUp(self):
180 self.wrapper = TextWrapper()
181 self.text = '''
Greg Ward90c0b072002-08-22 18:11:10 +0000182Did you say "supercalifragilisticexpialidocious?"
183How *do* you spell that odd word, anyways?
184'''
Greg Wardfd030e42002-08-22 19:02:37 +0000185
186 def test_break_long(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000187 # Wrap text with long words and lots of punctuation
Greg Wardfd030e42002-08-22 19:02:37 +0000188
189 self.check_wrap(self.text, 30,
Greg Wardee413842002-08-22 18:55:38 +0000190 ['Did you say "supercalifragilis',
191 'ticexpialidocious?" How *do*',
192 'you spell that odd word,',
193 'anyways?'])
Greg Wardfd030e42002-08-22 19:02:37 +0000194 self.check_wrap(self.text, 50,
Greg Wardee413842002-08-22 18:55:38 +0000195 ['Did you say "supercalifragilisticexpialidocious?"',
196 'How *do* you spell that odd word, anyways?'])
Greg Ward90c0b072002-08-22 18:11:10 +0000197
198
Guido van Rossum327af772002-08-22 20:13:47 +0000199 def test_nobreak_long(self):
200 # Test with break_long_words disabled
Greg Ward90c0b072002-08-22 18:11:10 +0000201 self.wrapper.break_long_words = 0
202 self.wrapper.width = 30
Greg Wardee413842002-08-22 18:55:38 +0000203 expect = ['Did you say',
204 '"supercalifragilisticexpialidocious?"',
205 'How *do* you spell that odd',
206 'word, anyways?'
Guido van Rossum327af772002-08-22 20:13:47 +0000207 ]
Greg Wardfd030e42002-08-22 19:02:37 +0000208 result = self.wrapper.wrap(self.text)
Greg Ward90c0b072002-08-22 18:11:10 +0000209 self.check(result, expect)
210
211 # Same thing with kwargs passed to standalone wrap() function.
Greg Wardfd030e42002-08-22 19:02:37 +0000212 result = wrap(self.text, width=30, break_long_words=0)
Greg Ward90c0b072002-08-22 18:11:10 +0000213 self.check(result, expect)
214
215
216
Greg Ward13c53c62002-08-22 18:57:26 +0000217class IndentTestCases(BaseTestCase):
Greg Ward90c0b072002-08-22 18:11:10 +0000218
219 # called before each test method
220 def setUp(self):
Greg Wardf69d3c92002-08-22 19:06:45 +0000221 self.text = '''\
Greg Ward90c0b072002-08-22 18:11:10 +0000222This paragraph will be filled, first without any indentation,
223and then with some (including a hanging indent).'''
224
225
Greg Wardf6765782002-08-22 18:35:49 +0000226 def test_fill(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000227 # Test the fill() method
Greg Ward90c0b072002-08-22 18:11:10 +0000228
229 expect = '''\
230This paragraph will be filled, first
231without any indentation, and then with
232some (including a hanging indent).'''
233
Greg Wardf69d3c92002-08-22 19:06:45 +0000234 result = fill(self.text, 40)
Greg Ward90c0b072002-08-22 18:11:10 +0000235 self.check(result, expect)
236
237
Greg Wardf6765782002-08-22 18:35:49 +0000238 def test_initial_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000239 # Test initial_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000240
Greg Wardf69d3c92002-08-22 19:06:45 +0000241 expect = [" This paragraph will be filled,",
242 "first without any indentation, and then",
243 "with some (including a hanging indent)."]
244 result = wrap(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000245 self.check(result, expect)
246
Greg Wardf69d3c92002-08-22 19:06:45 +0000247 expect = "\n".join(expect)
248 result = fill(self.text, 40, initial_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000249 self.check(result, expect)
250
251
Greg Wardf6765782002-08-22 18:35:49 +0000252 def test_subsequent_indent(self):
Guido van Rossum327af772002-08-22 20:13:47 +0000253 # Test subsequent_indent parameter
Greg Ward90c0b072002-08-22 18:11:10 +0000254
255 expect = '''\
256 * This paragraph will be filled, first
257 without any indentation, and then
258 with some (including a hanging
259 indent).'''
260
Greg Wardf69d3c92002-08-22 19:06:45 +0000261 result = fill(self.text, 40,
262 initial_indent=" * ", subsequent_indent=" ")
Greg Ward90c0b072002-08-22 18:11:10 +0000263 self.check(result, expect)
264
265
Greg Wardf6765782002-08-22 18:35:49 +0000266def test_main():
267 suite = unittest.TestSuite()
268 suite.addTest(unittest.makeSuite(WrapTestCase))
Greg Wardfd030e42002-08-22 19:02:37 +0000269 suite.addTest(unittest.makeSuite(LongWordTestCase))
Greg Wardf6765782002-08-22 18:35:49 +0000270 suite.addTest(unittest.makeSuite(IndentTestCases))
271 test_support.run_suite(suite)
272
Greg Ward90c0b072002-08-22 18:11:10 +0000273if __name__ == '__main__':
Greg Wardf6765782002-08-22 18:35:49 +0000274 test_main()