blob: e6b419f7c523c8ee328758764a986df031dcd141 [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):
Guido van Rossume2a383d2007-01-15 16:59:06 +000015 def __init__(self): self.seq = [7, 'hello', 123]
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000016
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))
Guido van Rossumcc2b0162007-02-11 06:12:03 +000043 for (key, value) in obj.items()
Walter Dörwald0fd583c2003-02-21 12:53:50 +000044 ])
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')
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000109 self.checkequal(2, 'aaa', 'count', 'a', 1)
110 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000111 self.checkequal(1, 'aaa', 'count', 'a', -1)
112 self.checkequal(3, 'aaa', 'count', 'a', -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000113 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
114 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000115 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
116 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000117 self.checkequal(3, 'aaa', 'count', '', 1)
118 self.checkequal(1, 'aaa', 'count', '', 3)
119 self.checkequal(0, 'aaa', 'count', '', 10)
120 self.checkequal(2, 'aaa', 'count', '', -1)
121 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000122
123 self.checkraises(TypeError, 'hello', 'count')
124 self.checkraises(TypeError, 'hello', 'count', 42)
125
Raymond Hettinger57e74472005-02-20 09:54:53 +0000126 # For a variety of combinations,
127 # verify that str.count() matches an equivalent function
128 # replacing all occurrences and then differencing the string lengths
129 charset = ['', 'a', 'b']
130 digits = 7
131 base = len(charset)
132 teststrings = set()
133 for i in xrange(base ** digits):
134 entry = []
135 for j in xrange(digits):
136 i, m = divmod(i, base)
137 entry.append(charset[m])
138 teststrings.add(''.join(entry))
139 teststrings = list(teststrings)
140 for i in teststrings:
141 i = self.fixtype(i)
142 n = len(i)
143 for j in teststrings:
144 r1 = i.count(j)
145 if j:
146 r2, rem = divmod(n - len(i.replace(j, '')), len(j))
147 else:
148 r2, rem = len(i)+1, 0
149 if rem or r1 != r2:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
151 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000152
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000153 def test_find(self):
154 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
155 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
156 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
157
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000158 self.checkequal(0, 'abc', 'find', '', 0)
159 self.checkequal(3, 'abc', 'find', '', 3)
160 self.checkequal(-1, 'abc', 'find', '', 4)
161
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000162 self.checkraises(TypeError, 'hello', 'find')
163 self.checkraises(TypeError, 'hello', 'find', 42)
164
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000165 # For a variety of combinations,
166 # verify that str.find() matches __contains__
167 # and that the found substring is really at that location
168 charset = ['', 'a', 'b', 'c']
169 digits = 5
170 base = len(charset)
171 teststrings = set()
172 for i in xrange(base ** digits):
173 entry = []
174 for j in xrange(digits):
175 i, m = divmod(i, base)
176 entry.append(charset[m])
177 teststrings.add(''.join(entry))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000178 teststrings = list(teststrings)
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000179 for i in teststrings:
180 i = self.fixtype(i)
181 for j in teststrings:
182 loc = i.find(j)
183 r1 = (loc != -1)
184 r2 = j in i
185 if r1 != r2:
186 self.assertEqual(r1, r2)
187 if loc != -1:
188 self.assertEqual(i[loc:loc+len(j)], j)
189
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000190 def test_rfind(self):
191 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
192 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
193 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
194 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
195
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000196 self.checkequal(3, 'abc', 'rfind', '', 0)
197 self.checkequal(3, 'abc', 'rfind', '', 3)
198 self.checkequal(-1, 'abc', 'rfind', '', 4)
199
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000200 self.checkraises(TypeError, 'hello', 'rfind')
201 self.checkraises(TypeError, 'hello', 'rfind', 42)
202
203 def test_index(self):
204 self.checkequal(0, 'abcdefghiabc', 'index', '')
205 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
206 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
207 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
208
209 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
210 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
211 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
212 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
213
214 self.checkraises(TypeError, 'hello', 'index')
215 self.checkraises(TypeError, 'hello', 'index', 42)
216
217 def test_rindex(self):
218 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
219 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
220 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
221 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
222
223 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
224 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
225 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
226 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
227 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
228
229 self.checkraises(TypeError, 'hello', 'rindex')
230 self.checkraises(TypeError, 'hello', 'rindex', 42)
231
232 def test_lower(self):
233 self.checkequal('hello', 'HeLLo', 'lower')
234 self.checkequal('hello', 'hello', 'lower')
235 self.checkraises(TypeError, 'hello', 'lower', 42)
236
237 def test_upper(self):
238 self.checkequal('HELLO', 'HeLLo', 'upper')
239 self.checkequal('HELLO', 'HELLO', 'upper')
240 self.checkraises(TypeError, 'hello', 'upper', 42)
241
242 def test_expandtabs(self):
243 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
244 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
245 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
246 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
247 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
248 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
249 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
250
251 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
252
253 def test_split(self):
254 self.checkequal(['this', 'is', 'the', 'split', 'function'],
255 'this is the split function', 'split')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000256
257 # by whitespace
258 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000259 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
260 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
261 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
262 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000263 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
264 sys.maxint-1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000265 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000266 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000267 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000268
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269 self.checkequal([], ' ', 'split')
270 self.checkequal(['a'], ' a ', 'split')
271 self.checkequal(['a', 'b'], ' a b ', 'split')
272 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
273 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
274 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
275 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
276 aaa = ' a '*20
277 self.checkequal(['a']*20, aaa, 'split')
278 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
279 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
280
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000281 # by a char
282 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000283 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000284 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
285 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
286 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
287 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000288 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
289 sys.maxint-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000290 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
291 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
292 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000293 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
294 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000295 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
296
Thomas Wouters477c8d52006-05-27 19:21:47 +0000297 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
298 self.checkequal(['a']*15 +['a|a|a|a|a'],
299 ('a|'*20)[:-1], 'split', '|', 15)
300
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000301 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000302 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000303 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
304 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
305 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
306 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000307 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
308 sys.maxint-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000309 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
310 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000311 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000312 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
313 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
314 'split', 'test')
315 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
316 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
317 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
318 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
319 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
320 self.checkequal([''], '', 'split', 'aaa')
321 self.checkequal(['aa'], 'aa', 'split', 'aaa')
322 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
323 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
324
325 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
326 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
327 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
328 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000329
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000330 # mixed use of str and unicode
331 self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2)
332
333 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000334 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
335
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336 # null case
337 self.checkraises(ValueError, 'hello', 'split', '')
338 self.checkraises(ValueError, 'hello', 'split', '', 0)
339
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000340 def test_rsplit(self):
341 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
342 'this is the rsplit function', 'rsplit')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000343
344 # by whitespace
345 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000346 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
347 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
348 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
349 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
351 sys.maxint-20)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000352 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000353 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000354 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000355
Thomas Wouters477c8d52006-05-27 19:21:47 +0000356 self.checkequal([], ' ', 'rsplit')
357 self.checkequal(['a'], ' a ', 'rsplit')
358 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
359 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
360 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
361 None, 1)
362 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
363 None, 2)
364 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
365 aaa = ' a '*20
366 self.checkequal(['a']*20, aaa, 'rsplit')
367 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
368 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
369
370
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000371 # by a char
372 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
373 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
374 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
375 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
376 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000377 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
378 sys.maxint-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000379 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
380 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
381 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000382 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
383 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
384
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000385 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
386
Thomas Wouters477c8d52006-05-27 19:21:47 +0000387 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
388 self.checkequal(['a|a|a|a|a']+['a']*15,
389 ('a|'*20)[:-1], 'rsplit', '|', 15)
390
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000391 # by string
392 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
393 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
394 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
395 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
396 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000397 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
398 sys.maxint-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000399 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
400 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
401 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000402 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
403 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
404 'rsplit', 'test')
405 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
406 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
407 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
408 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
409 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
410 self.checkequal([''], '', 'rsplit', 'aaa')
411 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
412 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
413 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
414
415 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
416 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
417 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
418 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000419
420 # mixed use of str and unicode
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000421 self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000422
423 # argument type
424 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000425
Thomas Wouters477c8d52006-05-27 19:21:47 +0000426 # null case
427 self.checkraises(ValueError, 'hello', 'rsplit', '')
428 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
429
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000430 def test_strip(self):
431 self.checkequal('hello', ' hello ', 'strip')
432 self.checkequal('hello ', ' hello ', 'lstrip')
433 self.checkequal(' hello', ' hello ', 'rstrip')
434 self.checkequal('hello', 'hello', 'strip')
435
Neal Norwitzffe33b72003-04-10 22:35:32 +0000436 # strip/lstrip/rstrip with None arg
437 self.checkequal('hello', ' hello ', 'strip', None)
438 self.checkequal('hello ', ' hello ', 'lstrip', None)
439 self.checkequal(' hello', ' hello ', 'rstrip', None)
440 self.checkequal('hello', 'hello', 'strip', None)
441
442 # strip/lstrip/rstrip with str arg
443 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
444 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
445 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
446 self.checkequal('hello', 'hello', 'strip', 'xyz')
447
448 # strip/lstrip/rstrip with unicode arg
449 if test_support.have_unicode:
450 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
451 'strip', unicode('xyz', 'ascii'))
452 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
453 'lstrip', unicode('xyz', 'ascii'))
454 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
455 'rstrip', unicode('xyz', 'ascii'))
456 self.checkequal(unicode('hello', 'ascii'), 'hello',
457 'strip', unicode('xyz', 'ascii'))
458
459 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
460 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
461 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
462
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000463 def test_ljust(self):
464 self.checkequal('abc ', 'abc', 'ljust', 10)
465 self.checkequal('abc ', 'abc', 'ljust', 6)
466 self.checkequal('abc', 'abc', 'ljust', 3)
467 self.checkequal('abc', 'abc', 'ljust', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000468 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000469 self.checkraises(TypeError, 'abc', 'ljust')
470
471 def test_rjust(self):
472 self.checkequal(' abc', 'abc', 'rjust', 10)
473 self.checkequal(' abc', 'abc', 'rjust', 6)
474 self.checkequal('abc', 'abc', 'rjust', 3)
475 self.checkequal('abc', 'abc', 'rjust', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000476 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000477 self.checkraises(TypeError, 'abc', 'rjust')
478
479 def test_center(self):
480 self.checkequal(' abc ', 'abc', 'center', 10)
481 self.checkequal(' abc ', 'abc', 'center', 6)
482 self.checkequal('abc', 'abc', 'center', 3)
483 self.checkequal('abc', 'abc', 'center', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000484 self.checkequal('***abc****', 'abc', 'center', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000485 self.checkraises(TypeError, 'abc', 'center')
486
487 def test_swapcase(self):
488 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
489
490 self.checkraises(TypeError, 'hello', 'swapcase', 42)
491
492 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000493 EQ = self.checkequal
494
495 # Operations on the empty string
496 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000497 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000498 EQ("", "", "replace", "A", "")
499 EQ("", "", "replace", "A", "A")
500 EQ("", "", "replace", "", "", 100)
501 EQ("", "", "replace", "", "", sys.maxint)
502
503 # interleave (from=="", 'to' gets inserted everywhere)
504 EQ("A", "A", "replace", "", "")
505 EQ("*A*", "A", "replace", "", "*")
506 EQ("*1A*1", "A", "replace", "", "*1")
507 EQ("*-#A*-#", "A", "replace", "", "*-#")
508 EQ("*-A*-A*-", "AA", "replace", "", "*-")
509 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
510 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
511 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
512 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
513 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
514 EQ("*-AA", "AA", "replace", "", "*-", 1)
515 EQ("AA", "AA", "replace", "", "*-", 0)
516
517 # single character deletion (from=="A", to=="")
518 EQ("", "A", "replace", "A", "")
519 EQ("", "AAA", "replace", "A", "")
520 EQ("", "AAA", "replace", "A", "", -1)
521 EQ("", "AAA", "replace", "A", "", sys.maxint)
522 EQ("", "AAA", "replace", "A", "", 4)
523 EQ("", "AAA", "replace", "A", "", 3)
524 EQ("A", "AAA", "replace", "A", "", 2)
525 EQ("AA", "AAA", "replace", "A", "", 1)
526 EQ("AAA", "AAA", "replace", "A", "", 0)
527 EQ("", "AAAAAAAAAA", "replace", "A", "")
528 EQ("BCD", "ABACADA", "replace", "A", "")
529 EQ("BCD", "ABACADA", "replace", "A", "", -1)
530 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
531 EQ("BCD", "ABACADA", "replace", "A", "", 5)
532 EQ("BCD", "ABACADA", "replace", "A", "", 4)
533 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
534 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
535 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
536 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
537 EQ("BCD", "ABCAD", "replace", "A", "")
538 EQ("BCD", "ABCADAA", "replace", "A", "")
539 EQ("BCD", "BCD", "replace", "A", "")
540 EQ("*************", "*************", "replace", "A", "")
541 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
542
543 # substring deletion (from=="the", to=="")
544 EQ("", "the", "replace", "the", "")
545 EQ("ater", "theater", "replace", "the", "")
546 EQ("", "thethe", "replace", "the", "")
547 EQ("", "thethethethe", "replace", "the", "")
548 EQ("aaaa", "theatheatheathea", "replace", "the", "")
549 EQ("that", "that", "replace", "the", "")
550 EQ("thaet", "thaet", "replace", "the", "")
551 EQ("here and re", "here and there", "replace", "the", "")
552 EQ("here and re and re", "here and there and there",
553 "replace", "the", "", sys.maxint)
554 EQ("here and re and re", "here and there and there",
555 "replace", "the", "", -1)
556 EQ("here and re and re", "here and there and there",
557 "replace", "the", "", 3)
558 EQ("here and re and re", "here and there and there",
559 "replace", "the", "", 2)
560 EQ("here and re and there", "here and there and there",
561 "replace", "the", "", 1)
562 EQ("here and there and there", "here and there and there",
563 "replace", "the", "", 0)
564 EQ("here and re and re", "here and there and there", "replace", "the", "")
565
566 EQ("abc", "abc", "replace", "the", "")
567 EQ("abcdefg", "abcdefg", "replace", "the", "")
568
569 # substring deletion (from=="bob", to=="")
570 EQ("bob", "bbobob", "replace", "bob", "")
571 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
572 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
573 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
574
575 # single character replace in place (len(from)==len(to)==1)
576 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
577 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
578 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
579 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
580 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
581 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
582 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
583 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
584
585 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
586 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
587 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
588 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
589 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
590
591 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
592
593 # substring replace in place (len(from)==len(to) > 1)
594 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
595 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
596 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
597 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
598 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
599 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
600 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
601 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
602 EQ("cobob", "bobob", "replace", "bob", "cob")
603 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
604 EQ("bobob", "bobob", "replace", "bot", "bot")
605
606 # replace single character (len(from)==1, len(to)>1)
607 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
608 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
609 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
610 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
611 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
612 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
613 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
614
615 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
616
617 # replace substring (len(from)>1, len(to)!=len(from))
618 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
619 "replace", "spam", "ham")
620 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
621 "replace", "spam", "ham", sys.maxint)
622 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
623 "replace", "spam", "ham", -1)
624 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
625 "replace", "spam", "ham", 4)
626 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
627 "replace", "spam", "ham", 3)
628 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
629 "replace", "spam", "ham", 2)
630 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
631 "replace", "spam", "ham", 1)
632 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
633 "replace", "spam", "ham", 0)
634
635 EQ("bobob", "bobobob", "replace", "bobob", "bob")
636 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
637 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
638
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639 ba = buffer('a')
640 bb = buffer('b')
641 EQ("bbc", "abc", "replace", ba, bb)
642 EQ("aac", "abc", "replace", bb, ba)
643
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000645 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
646 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
647 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
648 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
649 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
650 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
651 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
652 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
653 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
654 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
655 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
656 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
657 self.checkequal('', '', 'replace', '', '')
658 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
659 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
660 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
661 # MemoryError due to empty result (platform malloc issue when requesting
662 # 0 bytes).
663 self.checkequal('', '123', 'replace', '123', '')
664 self.checkequal('', '123123', 'replace', '123', '')
665 self.checkequal('x', '123x123', 'replace', '123', '')
666
667 self.checkraises(TypeError, 'hello', 'replace')
668 self.checkraises(TypeError, 'hello', 'replace', 42)
669 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
670 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
671
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 def test_replace_overflow(self):
673 # Check for overflow checking on 32 bit machines
674 if sys.maxint != 2147483647:
675 return
676 A2_16 = "A" * (2**16)
677 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
678 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
679 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
680
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000681 def test_zfill(self):
682 self.checkequal('123', '123', 'zfill', 2)
683 self.checkequal('123', '123', 'zfill', 3)
684 self.checkequal('0123', '123', 'zfill', 4)
685 self.checkequal('+123', '+123', 'zfill', 3)
686 self.checkequal('+123', '+123', 'zfill', 4)
687 self.checkequal('+0123', '+123', 'zfill', 5)
688 self.checkequal('-123', '-123', 'zfill', 3)
689 self.checkequal('-123', '-123', 'zfill', 4)
690 self.checkequal('-0123', '-123', 'zfill', 5)
691 self.checkequal('000', '', 'zfill', 3)
692 self.checkequal('34', '34', 'zfill', 1)
693 self.checkequal('0034', '34', 'zfill', 4)
694
695 self.checkraises(TypeError, '123', 'zfill')
696
697class MixinStrUnicodeUserStringTest:
698 # additional tests that only work for
699 # stringlike objects, i.e. str, unicode, UserString
700 # (but not the string module)
701
702 def test_islower(self):
703 self.checkequal(False, '', 'islower')
704 self.checkequal(True, 'a', 'islower')
705 self.checkequal(False, 'A', 'islower')
706 self.checkequal(False, '\n', 'islower')
707 self.checkequal(True, 'abc', 'islower')
708 self.checkequal(False, 'aBc', 'islower')
709 self.checkequal(True, 'abc\n', 'islower')
710 self.checkraises(TypeError, 'abc', 'islower', 42)
711
712 def test_isupper(self):
713 self.checkequal(False, '', 'isupper')
714 self.checkequal(False, 'a', 'isupper')
715 self.checkequal(True, 'A', 'isupper')
716 self.checkequal(False, '\n', 'isupper')
717 self.checkequal(True, 'ABC', 'isupper')
718 self.checkequal(False, 'AbC', 'isupper')
719 self.checkequal(True, 'ABC\n', 'isupper')
720 self.checkraises(TypeError, 'abc', 'isupper', 42)
721
722 def test_istitle(self):
723 self.checkequal(False, '', 'istitle')
724 self.checkequal(False, 'a', 'istitle')
725 self.checkequal(True, 'A', 'istitle')
726 self.checkequal(False, '\n', 'istitle')
727 self.checkequal(True, 'A Titlecased Line', 'istitle')
728 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
729 self.checkequal(True, 'A Titlecased, Line', 'istitle')
730 self.checkequal(False, 'Not a capitalized String', 'istitle')
731 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
732 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
733 self.checkequal(False, 'NOT', 'istitle')
734 self.checkraises(TypeError, 'abc', 'istitle', 42)
735
736 def test_isspace(self):
737 self.checkequal(False, '', 'isspace')
738 self.checkequal(False, 'a', 'isspace')
739 self.checkequal(True, ' ', 'isspace')
740 self.checkequal(True, '\t', 'isspace')
741 self.checkequal(True, '\r', 'isspace')
742 self.checkequal(True, '\n', 'isspace')
743 self.checkequal(True, ' \t\r\n', 'isspace')
744 self.checkequal(False, ' \t\r\na', 'isspace')
745 self.checkraises(TypeError, 'abc', 'isspace', 42)
746
747 def test_isalpha(self):
748 self.checkequal(False, '', 'isalpha')
749 self.checkequal(True, 'a', 'isalpha')
750 self.checkequal(True, 'A', 'isalpha')
751 self.checkequal(False, '\n', 'isalpha')
752 self.checkequal(True, 'abc', 'isalpha')
753 self.checkequal(False, 'aBc123', 'isalpha')
754 self.checkequal(False, 'abc\n', 'isalpha')
755 self.checkraises(TypeError, 'abc', 'isalpha', 42)
756
757 def test_isalnum(self):
758 self.checkequal(False, '', 'isalnum')
759 self.checkequal(True, 'a', 'isalnum')
760 self.checkequal(True, 'A', 'isalnum')
761 self.checkequal(False, '\n', 'isalnum')
762 self.checkequal(True, '123abc456', 'isalnum')
763 self.checkequal(True, 'a1b3c', 'isalnum')
764 self.checkequal(False, 'aBc000 ', 'isalnum')
765 self.checkequal(False, 'abc\n', 'isalnum')
766 self.checkraises(TypeError, 'abc', 'isalnum', 42)
767
768 def test_isdigit(self):
769 self.checkequal(False, '', 'isdigit')
770 self.checkequal(False, 'a', 'isdigit')
771 self.checkequal(True, '0', 'isdigit')
772 self.checkequal(True, '0123456789', 'isdigit')
773 self.checkequal(False, '0123456789a', 'isdigit')
774
775 self.checkraises(TypeError, 'abc', 'isdigit', 42)
776
777 def test_title(self):
778 self.checkequal(' Hello ', ' hello ', 'title')
779 self.checkequal('Hello ', 'hello ', 'title')
780 self.checkequal('Hello ', 'Hello ', 'title')
781 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
782 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
783 self.checkequal('Getint', "getInt", 'title')
784 self.checkraises(TypeError, 'hello', 'title', 42)
785
786 def test_splitlines(self):
787 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
788 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
789 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
790 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
791 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
792 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
793 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
794
795 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
796
797 def test_startswith(self):
798 self.checkequal(True, 'hello', 'startswith', 'he')
799 self.checkequal(True, 'hello', 'startswith', 'hello')
800 self.checkequal(False, 'hello', 'startswith', 'hello world')
801 self.checkequal(True, 'hello', 'startswith', '')
802 self.checkequal(False, 'hello', 'startswith', 'ello')
803 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
804 self.checkequal(True, 'hello', 'startswith', 'o', 4)
805 self.checkequal(False, 'hello', 'startswith', 'o', 5)
806 self.checkequal(True, 'hello', 'startswith', '', 5)
807 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
808 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
809 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
810 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
811
812 # test negative indices
813 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
814 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
815 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
816 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
817 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
818 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
819 self.checkequal(False, 'hello', 'startswith', 'o', -2)
820 self.checkequal(True, 'hello', 'startswith', 'o', -1)
821 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
822 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
823
824 self.checkraises(TypeError, 'hello', 'startswith')
825 self.checkraises(TypeError, 'hello', 'startswith', 42)
826
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000827 # test tuple arguments
828 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
829 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
830 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
831 self.checkequal(False, 'hello', 'startswith', ())
832 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
833 'rld', 'lowo'), 3)
834 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
835 'rld'), 3)
836 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
837 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
838 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
839
840 self.checkraises(TypeError, 'hello', 'startswith', (42,))
841
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000842 def test_endswith(self):
843 self.checkequal(True, 'hello', 'endswith', 'lo')
844 self.checkequal(False, 'hello', 'endswith', 'he')
845 self.checkequal(True, 'hello', 'endswith', '')
846 self.checkequal(False, 'hello', 'endswith', 'hello world')
847 self.checkequal(False, 'helloworld', 'endswith', 'worl')
848 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
849 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
850 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
851 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
852 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
853 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
854 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
855 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
856 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
857
858 # test negative indices
859 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
860 self.checkequal(False, 'hello', 'endswith', 'he', -2)
861 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
862 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
863 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
864 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
865 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
866 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
867 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
868 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
869 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
870 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
871 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
872
873 self.checkraises(TypeError, 'hello', 'endswith')
874 self.checkraises(TypeError, 'hello', 'endswith', 42)
875
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000876 # test tuple arguments
877 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
878 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
879 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
880 self.checkequal(False, 'hello', 'endswith', ())
881 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
882 'rld', 'lowo'), 3)
883 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
884 'rld'), 3, -1)
885 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
886 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
887 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
888
889 self.checkraises(TypeError, 'hello', 'endswith', (42,))
890
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000891 def test___contains__(self):
892 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
893 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
894 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
895 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
896 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
897 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
898 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
899 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
900 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
901
902 def test_subscript(self):
903 self.checkequal(u'a', 'abc', '__getitem__', 0)
904 self.checkequal(u'c', 'abc', '__getitem__', -1)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000905 self.checkequal(u'a', 'abc', '__getitem__', 0)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000906 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3))
907 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
908 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
909 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000910 # FIXME What about negative indices? This is handled differently by [] and __getitem__(slice)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000911
912 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
913
914 def test_slice(self):
915 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
916 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
917 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
918 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
919 self.checkequal('b', 'abc', '__getslice__', 1, 2)
920 self.checkequal('', 'abc', '__getslice__', 2, 2)
921 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
922 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
923 self.checkequal('', 'abc', '__getslice__', 2, 1)
924 # FIXME What about negative indizes? This is handled differently by [] and __getslice__
925
926 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
927
928 def test_mul(self):
929 self.checkequal('', 'abc', '__mul__', -1)
930 self.checkequal('', 'abc', '__mul__', 0)
931 self.checkequal('abc', 'abc', '__mul__', 1)
932 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
933 self.checkraises(TypeError, 'abc', '__mul__')
934 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +0000935 # XXX: on a 64-bit system, this doesn't raise an overflow error,
936 # but either raises a MemoryError, or succeeds (if you have 54TiB)
937 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000938
939 def test_join(self):
940 # join now works with any sequence type
941 # moved here, because the argument order is
942 # different in string.join (see the test in
943 # test.test_string.StringTest.test_join)
944 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
945 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000946 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
947 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000948 self.checkequal('w x y z', ' ', 'join', Sequence())
949 self.checkequal('abc', 'a', 'join', ('abc',))
950 self.checkequal('z', 'a', 'join', UserList(['z']))
951 if test_support.have_unicode:
952 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
953 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
954 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
955 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
956 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
957 for i in [5, 25, 125]:
958 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
959 ['a' * i] * i)
960 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
961 ('a' * i,) * i)
962
963 self.checkraises(TypeError, ' ', 'join', BadSeq1())
964 self.checkequal('a b c', ' ', 'join', BadSeq2())
965
966 self.checkraises(TypeError, ' ', 'join')
967 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000968 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123]))
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +0000969 try:
970 def f():
971 yield 4 + ""
972 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +0000973 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +0000974 if '+' not in str(e):
975 self.fail('join() ate exception message')
976 else:
977 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000978
979 def test_formatting(self):
980 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
981 self.checkequal('+10+', '+%d+', '__mod__', 10)
982 self.checkequal('a', "%c", '__mod__', "a")
983 self.checkequal('a', "%c", '__mod__', "a")
984 self.checkequal('"', "%c", '__mod__', 34)
985 self.checkequal('$', "%c", '__mod__', 36)
986 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +0000987 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000988
989 for ordinal in (-100, 0x200000):
990 # unicode raises ValueError, str raises OverflowError
991 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
992
993 self.checkequal(' 42', '%3ld', '__mod__', 42)
994 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +0000995 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000996
997 self.checkraises(TypeError, 'abc', '__mod__')
998 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
999 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1000 self.checkraises(TypeError, '%c', '__mod__', (None,))
1001 self.checkraises(ValueError, '%(foo', '__mod__', {})
1002 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1003
1004 # argument names with properly nested brackets are supported
1005 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1006
1007 # 100 is a magic number in PyUnicode_Format, this forces a resize
1008 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1009
1010 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1011 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1012 self.checkraises(ValueError, '%10', '__mod__', (42,))
1013
1014 def test_floatformatting(self):
1015 # float formatting
1016 for prec in xrange(100):
1017 format = '%%.%if' % prec
1018 value = 0.01
1019 for x in xrange(60):
1020 value = value * 3.141592655 / 3.0 * 10.0
1021 # The formatfloat() code in stringobject.c and
1022 # unicodeobject.c uses a 120 byte buffer and switches from
1023 # 'f' formatting to 'g' at precision 50, so we expect
1024 # OverflowErrors for the ranges x < 50 and prec >= 67.
1025 if x < 50 and prec >= 67:
1026 self.checkraises(OverflowError, format, "__mod__", value)
1027 else:
1028 self.checkcall(format, "__mod__", value)
1029
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 def test_inplace_rewrites(self):
1031 # Check that strings don't copy and modify cached single-character strings
1032 self.checkequal('a', 'A', 'lower')
1033 self.checkequal(True, 'A', 'isupper')
1034 self.checkequal('A', 'a', 'upper')
1035 self.checkequal(True, 'a', 'islower')
1036
1037 self.checkequal('a', 'A', 'replace', 'A', 'a')
1038 self.checkequal(True, 'A', 'isupper')
1039
1040 self.checkequal('A', 'a', 'capitalize')
1041 self.checkequal(True, 'a', 'islower')
1042
1043 self.checkequal('A', 'a', 'swapcase')
1044 self.checkequal(True, 'a', 'islower')
1045
1046 self.checkequal('A', 'a', 'title')
1047 self.checkequal(True, 'a', 'islower')
1048
1049 def test_partition(self):
1050
1051 self.checkequal(('this is the par', 'ti', 'tion method'),
1052 'this is the partition method', 'partition', 'ti')
1053
1054 # from raymond's original specification
1055 S = 'http://www.python.org'
1056 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1057 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1058 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1059 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1060
1061 self.checkraises(ValueError, S, 'partition', '')
1062 self.checkraises(TypeError, S, 'partition', None)
1063
1064 def test_rpartition(self):
1065
1066 self.checkequal(('this is the rparti', 'ti', 'on method'),
1067 'this is the rpartition method', 'rpartition', 'ti')
1068
1069 # from raymond's original specification
1070 S = 'http://www.python.org'
1071 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001072 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1074 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1075
1076 self.checkraises(ValueError, S, 'rpartition', '')
1077 self.checkraises(TypeError, S, 'rpartition', None)
1078
Walter Dörwald57d88e52004-08-26 16:53:04 +00001079
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001080class MixinStrStringUserStringTest:
1081 # Additional tests for 8bit strings, i.e. str, UserString and
1082 # the string module
1083
1084 def test_maketrans(self):
1085 self.assertEqual(
1086 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
1087 string.maketrans('abc', 'xyz')
1088 )
1089 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')
1090
1091 def test_translate(self):
1092 table = string.maketrans('abc', 'xyz')
1093 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def')
1094
1095 table = string.maketrans('a', 'A')
1096 self.checkequal('Abc', 'abc', 'translate', table)
1097 self.checkequal('xyz', 'xyz', 'translate', table)
1098 self.checkequal('yz', 'xyz', 'translate', table, 'x')
1099 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
1100 self.checkraises(ValueError, 'xyz', 'translate', 'too short')
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00001101
1102
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001103class MixinStrUserStringTest:
1104 # Additional tests that only work with
1105 # 8bit compatible object, i.e. str and UserString
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00001106
Walter Dörwald6eea7892005-07-28 16:49:15 +00001107 if test_support.have_unicode:
1108 def test_encoding_decoding(self):
1109 codecs = [('rot13', 'uryyb jbeyq'),
1110 ('base64', 'aGVsbG8gd29ybGQ=\n'),
1111 ('hex', '68656c6c6f20776f726c64'),
1112 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
1113 for encoding, data in codecs:
1114 self.checkequal(data, 'hello world', 'encode', encoding)
1115 self.checkequal('hello world', data, 'decode', encoding)
1116 # zlib is optional, so we make the test optional too...
1117 try:
1118 import zlib
1119 except ImportError:
1120 pass
1121 else:
1122 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
1123 self.checkequal(data, 'hello world', 'encode', 'zlib')
1124 self.checkequal('hello world', data, 'decode', 'zlib')
Walter Dörwald97951de2003-03-26 14:31:25 +00001125
Walter Dörwald6eea7892005-07-28 16:49:15 +00001126 self.checkraises(TypeError, 'xyz', 'decode', 42)
1127 self.checkraises(TypeError, 'xyz', 'encode', 42)
Walter Dörwald57d88e52004-08-26 16:53:04 +00001128
1129
1130class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001131 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001132
1133 def test_bug1001011(self):
1134 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001135 # involving a subclass.
1136 # Make sure that it is of the appropriate type.
1137 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001138 t = self.type2test
1139 class subclass(t):
1140 pass
1141 s1 = subclass("abcd")
1142 s2 = t().join([s1])
1143 self.assert_(s1 is not s2)
1144 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001145
1146 s1 = t("abcd")
1147 s2 = t().join([s1])
1148 self.assert_(s1 is s2)
1149
1150 # Should also test mixed-type join.
1151 if t is unicode:
1152 s1 = subclass("abcd")
1153 s2 = "".join([s1])
1154 self.assert_(s1 is not s2)
1155 self.assert_(type(s2) is t)
1156
1157 s1 = t("abcd")
1158 s2 = "".join([s1])
1159 self.assert_(s1 is s2)
1160
1161 elif t is str:
1162 s1 = subclass("abcd")
1163 s2 = u"".join([s1])
1164 self.assert_(s1 is not s2)
1165 self.assert_(type(s2) is unicode) # promotes!
1166
1167 s1 = t("abcd")
1168 s2 = u"".join([s1])
1169 self.assert_(s1 is not s2)
1170 self.assert_(type(s2) is unicode) # promotes!
1171
1172 else:
1173 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)