blob: 0ce96189af16e4031e74d4d13959a734e3951346 [file] [log] [blame]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001"""
2Common tests shared by test_str, test_unicode, test_userstring and test_string.
3"""
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00004
Walter Dörwald0fd583c2003-02-21 12:53:50 +00005import unittest, string, sys
6from test import test_support
Jeremy Hylton20f41b62000-07-11 03:31:55 +00007from UserList import UserList
8
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00009class Sequence:
Walter Dörwald0fd583c2003-02-21 12:53:50 +000010 def __init__(self, seq='wxyz'): self.seq = seq
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000011 def __len__(self): return len(self.seq)
12 def __getitem__(self, i): return self.seq[i]
13
14class BadSeq1(Sequence):
15 def __init__(self): self.seq = [7, 'hello', 123L]
16
17class BadSeq2(Sequence):
18 def __init__(self): self.seq = ['a', 'b', 'c']
19 def __len__(self): return 8
20
Walter Dörwald0fd583c2003-02-21 12:53:50 +000021class CommonTest(unittest.TestCase):
22 # This testcase contains test that can be used in all
23 # stringlike classes. Currently this is str, unicode
24 # UserString and the string module.
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000025
Walter Dörwald0fd583c2003-02-21 12:53:50 +000026 # The type to be tested
27 # Change in subclasses to change the behaviour of fixtesttype()
28 type2test = None
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000029
Walter Dörwald0fd583c2003-02-21 12:53:50 +000030 # All tests pass their arguments to the testing methods
31 # as str objects. fixtesttype() can be used to propagate
32 # these arguments to the appropriate type
33 def fixtype(self, obj):
34 if isinstance(obj, str):
35 return self.__class__.type2test(obj)
36 elif isinstance(obj, list):
37 return [self.fixtype(x) for x in obj]
38 elif isinstance(obj, tuple):
39 return tuple([self.fixtype(x) for x in obj])
40 elif isinstance(obj, dict):
41 return dict([
42 (self.fixtype(key), self.fixtype(value))
43 for (key, value) in obj.iteritems()
44 ])
45 else:
46 return obj
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000047
Walter Dörwald0fd583c2003-02-21 12:53:50 +000048 # check that object.method(*args) returns result
49 def checkequal(self, result, object, methodname, *args):
50 result = self.fixtype(result)
51 object = self.fixtype(object)
52 args = self.fixtype(args)
53 realresult = getattr(object, methodname)(*args)
54 self.assertEqual(
55 result,
56 realresult
57 )
58 # if the original is returned make sure that
59 # this doesn't happen with subclasses
60 if object == realresult:
61 class subtype(self.__class__.type2test):
62 pass
63 object = subtype(object)
64 realresult = getattr(object, methodname)(*args)
65 self.assert_(object is not realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000066
Walter Dörwald0fd583c2003-02-21 12:53:50 +000067 # check that object.method(*args) raises exc
68 def checkraises(self, exc, object, methodname, *args):
69 object = self.fixtype(object)
70 args = self.fixtype(args)
71 self.assertRaises(
72 exc,
73 getattr(object, methodname),
74 *args
75 )
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000076
Walter Dörwald0fd583c2003-02-21 12:53:50 +000077 # call object.method(*args) without any checks
78 def checkcall(self, object, methodname, *args):
79 object = self.fixtype(object)
80 args = self.fixtype(args)
81 getattr(object, methodname)(*args)
82
Raymond Hettinger561fbf12004-10-26 01:52:37 +000083 def test_hash(self):
84 # SF bug 1054139: += optimization was not invalidating cached hash value
85 a = self.type2test('DNSSEC')
86 b = self.type2test('')
87 for c in a:
88 b += c
89 hash(b)
90 self.assertEqual(hash(a), hash(b))
91
Walter Dörwald0fd583c2003-02-21 12:53:50 +000092 def test_capitalize(self):
93 self.checkequal(' hello ', ' hello ', 'capitalize')
94 self.checkequal('Hello ', 'Hello ','capitalize')
95 self.checkequal('Hello ', 'hello ','capitalize')
96 self.checkequal('Aaaa', 'aaaa', 'capitalize')
97 self.checkequal('Aaaa', 'AaAa', 'capitalize')
98
99 self.checkraises(TypeError, 'hello', 'capitalize', 42)
100
101 def test_count(self):
102 self.checkequal(3, 'aaa', 'count', 'a')
103 self.checkequal(0, 'aaa', 'count', 'b')
104 self.checkequal(3, 'aaa', 'count', 'a')
105 self.checkequal(0, 'aaa', 'count', 'b')
106 self.checkequal(3, 'aaa', 'count', 'a')
107 self.checkequal(0, 'aaa', 'count', 'b')
108 self.checkequal(0, 'aaa', 'count', 'b')
109 self.checkequal(1, 'aaa', 'count', 'a', -1)
110 self.checkequal(3, 'aaa', 'count', 'a', -10)
111 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
112 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
113
114 self.checkraises(TypeError, 'hello', 'count')
115 self.checkraises(TypeError, 'hello', 'count', 42)
116
117 def test_find(self):
118 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
119 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
120 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
121
122 self.checkraises(TypeError, 'hello', 'find')
123 self.checkraises(TypeError, 'hello', 'find', 42)
124
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000125 # For a variety of combinations,
126 # verify that str.find() matches __contains__
127 # and that the found substring is really at that location
128 charset = ['', 'a', 'b', 'c']
129 digits = 5
130 base = len(charset)
131 teststrings = set()
132 for i in xrange(base ** digits):
133 entry = []
134 for j in xrange(digits):
135 i, m = divmod(i, base)
136 entry.append(charset[m])
137 teststrings.add(''.join(entry))
138 for i in teststrings:
139 i = self.fixtype(i)
140 for j in teststrings:
141 loc = i.find(j)
142 r1 = (loc != -1)
143 r2 = j in i
144 if r1 != r2:
145 self.assertEqual(r1, r2)
146 if loc != -1:
147 self.assertEqual(i[loc:loc+len(j)], j)
148
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000149 def test_rfind(self):
150 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
151 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
152 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
153 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
154
155 self.checkraises(TypeError, 'hello', 'rfind')
156 self.checkraises(TypeError, 'hello', 'rfind', 42)
157
158 def test_index(self):
159 self.checkequal(0, 'abcdefghiabc', 'index', '')
160 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
161 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
162 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
163
164 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
165 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
166 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
167 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
168
169 self.checkraises(TypeError, 'hello', 'index')
170 self.checkraises(TypeError, 'hello', 'index', 42)
171
172 def test_rindex(self):
173 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
174 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
175 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
176 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
177
178 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
179 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
180 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
181 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
182 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
183
184 self.checkraises(TypeError, 'hello', 'rindex')
185 self.checkraises(TypeError, 'hello', 'rindex', 42)
186
187 def test_lower(self):
188 self.checkequal('hello', 'HeLLo', 'lower')
189 self.checkequal('hello', 'hello', 'lower')
190 self.checkraises(TypeError, 'hello', 'lower', 42)
191
192 def test_upper(self):
193 self.checkequal('HELLO', 'HeLLo', 'upper')
194 self.checkequal('HELLO', 'HELLO', 'upper')
195 self.checkraises(TypeError, 'hello', 'upper', 42)
196
197 def test_expandtabs(self):
198 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
199 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
200 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
201 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
202 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
203 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
204 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
205
206 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
207
208 def test_split(self):
209 self.checkequal(['this', 'is', 'the', 'split', 'function'],
210 'this is the split function', 'split')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000211
212 # by whitespace
213 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000214 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
215 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
216 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
217 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
218 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
219 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000220
221 # by a char
222 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
223 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
224 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
225 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
226 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
227 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
228 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
229 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
230 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
231
232 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000233 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000234 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
235 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
236 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
237 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
238 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
239 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000240 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
241
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000242 # mixed use of str and unicode
243 self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2)
244
245 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000246 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
247
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000248 def test_rsplit(self):
249 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
250 'this is the rsplit function', 'rsplit')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000251
252 # by whitespace
253 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000254 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
255 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
256 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
257 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
258 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000259 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000260
261 # by a char
262 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
263 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
264 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
265 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
266 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
267 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
268 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
269 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
270 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
271
272 # by string
273 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
274 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
275 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
276 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
277 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
278 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
279 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
280 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
281
282 # mixed use of str and unicode
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000283 self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000284
285 # argument type
286 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000287
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000288 def test_strip(self):
289 self.checkequal('hello', ' hello ', 'strip')
290 self.checkequal('hello ', ' hello ', 'lstrip')
291 self.checkequal(' hello', ' hello ', 'rstrip')
292 self.checkequal('hello', 'hello', 'strip')
293
Neal Norwitzffe33b72003-04-10 22:35:32 +0000294 # strip/lstrip/rstrip with None arg
295 self.checkequal('hello', ' hello ', 'strip', None)
296 self.checkequal('hello ', ' hello ', 'lstrip', None)
297 self.checkequal(' hello', ' hello ', 'rstrip', None)
298 self.checkequal('hello', 'hello', 'strip', None)
299
300 # strip/lstrip/rstrip with str arg
301 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
302 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
303 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
304 self.checkequal('hello', 'hello', 'strip', 'xyz')
305
306 # strip/lstrip/rstrip with unicode arg
307 if test_support.have_unicode:
308 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
309 'strip', unicode('xyz', 'ascii'))
310 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
311 'lstrip', unicode('xyz', 'ascii'))
312 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
313 'rstrip', unicode('xyz', 'ascii'))
314 self.checkequal(unicode('hello', 'ascii'), 'hello',
315 'strip', unicode('xyz', 'ascii'))
316
317 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
318 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
319 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
320
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000321 def test_ljust(self):
322 self.checkequal('abc ', 'abc', 'ljust', 10)
323 self.checkequal('abc ', 'abc', 'ljust', 6)
324 self.checkequal('abc', 'abc', 'ljust', 3)
325 self.checkequal('abc', 'abc', 'ljust', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000326 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000327 self.checkraises(TypeError, 'abc', 'ljust')
328
329 def test_rjust(self):
330 self.checkequal(' abc', 'abc', 'rjust', 10)
331 self.checkequal(' abc', 'abc', 'rjust', 6)
332 self.checkequal('abc', 'abc', 'rjust', 3)
333 self.checkequal('abc', 'abc', 'rjust', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000334 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000335 self.checkraises(TypeError, 'abc', 'rjust')
336
337 def test_center(self):
338 self.checkequal(' abc ', 'abc', 'center', 10)
339 self.checkequal(' abc ', 'abc', 'center', 6)
340 self.checkequal('abc', 'abc', 'center', 3)
341 self.checkequal('abc', 'abc', 'center', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000342 self.checkequal('***abc****', 'abc', 'center', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000343 self.checkraises(TypeError, 'abc', 'center')
344
345 def test_swapcase(self):
346 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
347
348 self.checkraises(TypeError, 'hello', 'swapcase', 42)
349
350 def test_replace(self):
351 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
352 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
353 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
354 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
355 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
356 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
357 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
358 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
359 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
360 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
361 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
362 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
363 self.checkequal('', '', 'replace', '', '')
364 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
365 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
366 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
367 # MemoryError due to empty result (platform malloc issue when requesting
368 # 0 bytes).
369 self.checkequal('', '123', 'replace', '123', '')
370 self.checkequal('', '123123', 'replace', '123', '')
371 self.checkequal('x', '123x123', 'replace', '123', '')
372
373 self.checkraises(TypeError, 'hello', 'replace')
374 self.checkraises(TypeError, 'hello', 'replace', 42)
375 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
376 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
377
378 def test_zfill(self):
379 self.checkequal('123', '123', 'zfill', 2)
380 self.checkequal('123', '123', 'zfill', 3)
381 self.checkequal('0123', '123', 'zfill', 4)
382 self.checkequal('+123', '+123', 'zfill', 3)
383 self.checkequal('+123', '+123', 'zfill', 4)
384 self.checkequal('+0123', '+123', 'zfill', 5)
385 self.checkequal('-123', '-123', 'zfill', 3)
386 self.checkequal('-123', '-123', 'zfill', 4)
387 self.checkequal('-0123', '-123', 'zfill', 5)
388 self.checkequal('000', '', 'zfill', 3)
389 self.checkequal('34', '34', 'zfill', 1)
390 self.checkequal('0034', '34', 'zfill', 4)
391
392 self.checkraises(TypeError, '123', 'zfill')
393
394class MixinStrUnicodeUserStringTest:
395 # additional tests that only work for
396 # stringlike objects, i.e. str, unicode, UserString
397 # (but not the string module)
398
399 def test_islower(self):
400 self.checkequal(False, '', 'islower')
401 self.checkequal(True, 'a', 'islower')
402 self.checkequal(False, 'A', 'islower')
403 self.checkequal(False, '\n', 'islower')
404 self.checkequal(True, 'abc', 'islower')
405 self.checkequal(False, 'aBc', 'islower')
406 self.checkequal(True, 'abc\n', 'islower')
407 self.checkraises(TypeError, 'abc', 'islower', 42)
408
409 def test_isupper(self):
410 self.checkequal(False, '', 'isupper')
411 self.checkequal(False, 'a', 'isupper')
412 self.checkequal(True, 'A', 'isupper')
413 self.checkequal(False, '\n', 'isupper')
414 self.checkequal(True, 'ABC', 'isupper')
415 self.checkequal(False, 'AbC', 'isupper')
416 self.checkequal(True, 'ABC\n', 'isupper')
417 self.checkraises(TypeError, 'abc', 'isupper', 42)
418
419 def test_istitle(self):
420 self.checkequal(False, '', 'istitle')
421 self.checkequal(False, 'a', 'istitle')
422 self.checkequal(True, 'A', 'istitle')
423 self.checkequal(False, '\n', 'istitle')
424 self.checkequal(True, 'A Titlecased Line', 'istitle')
425 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
426 self.checkequal(True, 'A Titlecased, Line', 'istitle')
427 self.checkequal(False, 'Not a capitalized String', 'istitle')
428 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
429 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
430 self.checkequal(False, 'NOT', 'istitle')
431 self.checkraises(TypeError, 'abc', 'istitle', 42)
432
433 def test_isspace(self):
434 self.checkequal(False, '', 'isspace')
435 self.checkequal(False, 'a', 'isspace')
436 self.checkequal(True, ' ', 'isspace')
437 self.checkequal(True, '\t', 'isspace')
438 self.checkequal(True, '\r', 'isspace')
439 self.checkequal(True, '\n', 'isspace')
440 self.checkequal(True, ' \t\r\n', 'isspace')
441 self.checkequal(False, ' \t\r\na', 'isspace')
442 self.checkraises(TypeError, 'abc', 'isspace', 42)
443
444 def test_isalpha(self):
445 self.checkequal(False, '', 'isalpha')
446 self.checkequal(True, 'a', 'isalpha')
447 self.checkequal(True, 'A', 'isalpha')
448 self.checkequal(False, '\n', 'isalpha')
449 self.checkequal(True, 'abc', 'isalpha')
450 self.checkequal(False, 'aBc123', 'isalpha')
451 self.checkequal(False, 'abc\n', 'isalpha')
452 self.checkraises(TypeError, 'abc', 'isalpha', 42)
453
454 def test_isalnum(self):
455 self.checkequal(False, '', 'isalnum')
456 self.checkequal(True, 'a', 'isalnum')
457 self.checkequal(True, 'A', 'isalnum')
458 self.checkequal(False, '\n', 'isalnum')
459 self.checkequal(True, '123abc456', 'isalnum')
460 self.checkequal(True, 'a1b3c', 'isalnum')
461 self.checkequal(False, 'aBc000 ', 'isalnum')
462 self.checkequal(False, 'abc\n', 'isalnum')
463 self.checkraises(TypeError, 'abc', 'isalnum', 42)
464
465 def test_isdigit(self):
466 self.checkequal(False, '', 'isdigit')
467 self.checkequal(False, 'a', 'isdigit')
468 self.checkequal(True, '0', 'isdigit')
469 self.checkequal(True, '0123456789', 'isdigit')
470 self.checkequal(False, '0123456789a', 'isdigit')
471
472 self.checkraises(TypeError, 'abc', 'isdigit', 42)
473
474 def test_title(self):
475 self.checkequal(' Hello ', ' hello ', 'title')
476 self.checkequal('Hello ', 'hello ', 'title')
477 self.checkequal('Hello ', 'Hello ', 'title')
478 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
479 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
480 self.checkequal('Getint', "getInt", 'title')
481 self.checkraises(TypeError, 'hello', 'title', 42)
482
483 def test_splitlines(self):
484 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
485 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
486 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
487 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
488 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
489 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
490 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
491
492 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
493
494 def test_startswith(self):
495 self.checkequal(True, 'hello', 'startswith', 'he')
496 self.checkequal(True, 'hello', 'startswith', 'hello')
497 self.checkequal(False, 'hello', 'startswith', 'hello world')
498 self.checkequal(True, 'hello', 'startswith', '')
499 self.checkequal(False, 'hello', 'startswith', 'ello')
500 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
501 self.checkequal(True, 'hello', 'startswith', 'o', 4)
502 self.checkequal(False, 'hello', 'startswith', 'o', 5)
503 self.checkequal(True, 'hello', 'startswith', '', 5)
504 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
505 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
506 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
507 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
508
509 # test negative indices
510 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
511 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
512 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
513 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
514 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
515 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
516 self.checkequal(False, 'hello', 'startswith', 'o', -2)
517 self.checkequal(True, 'hello', 'startswith', 'o', -1)
518 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
519 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
520
521 self.checkraises(TypeError, 'hello', 'startswith')
522 self.checkraises(TypeError, 'hello', 'startswith', 42)
523
524 def test_endswith(self):
525 self.checkequal(True, 'hello', 'endswith', 'lo')
526 self.checkequal(False, 'hello', 'endswith', 'he')
527 self.checkequal(True, 'hello', 'endswith', '')
528 self.checkequal(False, 'hello', 'endswith', 'hello world')
529 self.checkequal(False, 'helloworld', 'endswith', 'worl')
530 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
531 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
532 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
533 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
534 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
535 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
536 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
537 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
538 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
539
540 # test negative indices
541 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
542 self.checkequal(False, 'hello', 'endswith', 'he', -2)
543 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
544 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
545 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
546 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
547 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
548 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
549 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
550 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
551 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
552 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
553 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
554
555 self.checkraises(TypeError, 'hello', 'endswith')
556 self.checkraises(TypeError, 'hello', 'endswith', 42)
557
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000558 def test___contains__(self):
559 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
560 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
561 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
562 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
563 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
564 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
565 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
566 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
567 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
568
569 def test_subscript(self):
570 self.checkequal(u'a', 'abc', '__getitem__', 0)
571 self.checkequal(u'c', 'abc', '__getitem__', -1)
572 self.checkequal(u'a', 'abc', '__getitem__', 0L)
573 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3))
574 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
575 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
576 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
577 # FIXME What about negative indizes? This is handled differently by [] and __getitem__(slice)
578
579 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
580
581 def test_slice(self):
582 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
583 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
584 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
585 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
586 self.checkequal('b', 'abc', '__getslice__', 1, 2)
587 self.checkequal('', 'abc', '__getslice__', 2, 2)
588 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
589 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
590 self.checkequal('', 'abc', '__getslice__', 2, 1)
591 # FIXME What about negative indizes? This is handled differently by [] and __getslice__
592
593 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
594
595 def test_mul(self):
596 self.checkequal('', 'abc', '__mul__', -1)
597 self.checkequal('', 'abc', '__mul__', 0)
598 self.checkequal('abc', 'abc', '__mul__', 1)
599 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
600 self.checkraises(TypeError, 'abc', '__mul__')
601 self.checkraises(TypeError, 'abc', '__mul__', '')
Neal Norwitz15ff0e92003-02-23 23:15:26 +0000602 self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000603
604 def test_join(self):
605 # join now works with any sequence type
606 # moved here, because the argument order is
607 # different in string.join (see the test in
608 # test.test_string.StringTest.test_join)
609 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
610 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
611 self.checkequal('w x y z', ' ', 'join', Sequence())
612 self.checkequal('abc', 'a', 'join', ('abc',))
613 self.checkequal('z', 'a', 'join', UserList(['z']))
614 if test_support.have_unicode:
615 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
616 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
617 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
618 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
619 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
620 for i in [5, 25, 125]:
621 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
622 ['a' * i] * i)
623 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
624 ('a' * i,) * i)
625
626 self.checkraises(TypeError, ' ', 'join', BadSeq1())
627 self.checkequal('a b c', ' ', 'join', BadSeq2())
628
629 self.checkraises(TypeError, ' ', 'join')
630 self.checkraises(TypeError, ' ', 'join', 7)
631 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
632
633 def test_formatting(self):
634 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
635 self.checkequal('+10+', '+%d+', '__mod__', 10)
636 self.checkequal('a', "%c", '__mod__', "a")
637 self.checkequal('a', "%c", '__mod__', "a")
638 self.checkequal('"', "%c", '__mod__', 34)
639 self.checkequal('$', "%c", '__mod__', 36)
640 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +0000641 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000642
643 for ordinal in (-100, 0x200000):
644 # unicode raises ValueError, str raises OverflowError
645 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
646
647 self.checkequal(' 42', '%3ld', '__mod__', 42)
648 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +0000649 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000650
651 self.checkraises(TypeError, 'abc', '__mod__')
652 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
653 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
654 self.checkraises(TypeError, '%c', '__mod__', (None,))
655 self.checkraises(ValueError, '%(foo', '__mod__', {})
656 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
657
658 # argument names with properly nested brackets are supported
659 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
660
661 # 100 is a magic number in PyUnicode_Format, this forces a resize
662 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
663
664 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
665 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
666 self.checkraises(ValueError, '%10', '__mod__', (42,))
667
668 def test_floatformatting(self):
669 # float formatting
670 for prec in xrange(100):
671 format = '%%.%if' % prec
672 value = 0.01
673 for x in xrange(60):
674 value = value * 3.141592655 / 3.0 * 10.0
675 # The formatfloat() code in stringobject.c and
676 # unicodeobject.c uses a 120 byte buffer and switches from
677 # 'f' formatting to 'g' at precision 50, so we expect
678 # OverflowErrors for the ranges x < 50 and prec >= 67.
679 if x < 50 and prec >= 67:
680 self.checkraises(OverflowError, format, "__mod__", value)
681 else:
682 self.checkcall(format, "__mod__", value)
683
Walter Dörwald57d88e52004-08-26 16:53:04 +0000684
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000685class MixinStrStringUserStringTest:
686 # Additional tests for 8bit strings, i.e. str, UserString and
687 # the string module
688
689 def test_maketrans(self):
690 self.assertEqual(
691 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
692 string.maketrans('abc', 'xyz')
693 )
694 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')
695
696 def test_translate(self):
697 table = string.maketrans('abc', 'xyz')
698 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def')
699
700 table = string.maketrans('a', 'A')
701 self.checkequal('Abc', 'abc', 'translate', table)
702 self.checkequal('xyz', 'xyz', 'translate', table)
703 self.checkequal('yz', 'xyz', 'translate', table, 'x')
704 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
705 self.checkraises(ValueError, 'xyz', 'translate', 'too short')
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +0000706
707
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000708class MixinStrUserStringTest:
709 # Additional tests that only work with
710 # 8bit compatible object, i.e. str and UserString
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +0000711
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000712 def test_encoding_decoding(self):
713 codecs = [('rot13', 'uryyb jbeyq'),
714 ('base64', 'aGVsbG8gd29ybGQ=\n'),
715 ('hex', '68656c6c6f20776f726c64'),
716 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
717 for encoding, data in codecs:
718 self.checkequal(data, 'hello world', 'encode', encoding)
719 self.checkequal('hello world', data, 'decode', encoding)
720 # zlib is optional, so we make the test optional too...
721 try:
722 import zlib
723 except ImportError:
724 pass
725 else:
726 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
727 self.checkequal(data, 'hello world', 'encode', 'zlib')
728 self.checkequal('hello world', data, 'decode', 'zlib')
Walter Dörwald97951de2003-03-26 14:31:25 +0000729
730 self.checkraises(TypeError, 'xyz', 'decode', 42)
731 self.checkraises(TypeError, 'xyz', 'encode', 42)
Walter Dörwald57d88e52004-08-26 16:53:04 +0000732
733
734class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +0000735 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +0000736
737 def test_bug1001011(self):
738 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +0000739 # involving a subclass.
740 # Make sure that it is of the appropriate type.
741 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +0000742 t = self.type2test
743 class subclass(t):
744 pass
745 s1 = subclass("abcd")
746 s2 = t().join([s1])
747 self.assert_(s1 is not s2)
748 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +0000749
750 s1 = t("abcd")
751 s2 = t().join([s1])
752 self.assert_(s1 is s2)
753
754 # Should also test mixed-type join.
755 if t is unicode:
756 s1 = subclass("abcd")
757 s2 = "".join([s1])
758 self.assert_(s1 is not s2)
759 self.assert_(type(s2) is t)
760
761 s1 = t("abcd")
762 s2 = "".join([s1])
763 self.assert_(s1 is s2)
764
765 elif t is str:
766 s1 = subclass("abcd")
767 s2 = u"".join([s1])
768 self.assert_(s1 is not s2)
769 self.assert_(type(s2) is unicode) # promotes!
770
771 s1 = t("abcd")
772 s2 = u"".join([s1])
773 self.assert_(s1 is not s2)
774 self.assert_(type(s2) is unicode) # promotes!
775
776 else:
777 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)