blob: fe159b666592bd6730fef897af199fc52bf88f39 [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
Guido van Rossum360e4b82007-05-14 22:51:27 +00005import unittest, string, sys, struct
Walter Dörwald0fd583c2003-02-21 12:53:50 +00006from 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
Georg Brandlc7885542007-03-06 19:16:20 +000021class BaseTest(unittest.TestCase):
22 # These tests are for buffers of values (bytes) and not
23 # specific to character interpretation, used for bytes objects
24 # and various string implementations
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
Walter Dörwald0fd583c2003-02-21 12:53:50 +000083 def test_count(self):
84 self.checkequal(3, 'aaa', 'count', 'a')
85 self.checkequal(0, 'aaa', 'count', 'b')
86 self.checkequal(3, 'aaa', 'count', 'a')
87 self.checkequal(0, 'aaa', 'count', 'b')
88 self.checkequal(3, 'aaa', 'count', 'a')
89 self.checkequal(0, 'aaa', 'count', 'b')
90 self.checkequal(0, 'aaa', 'count', 'b')
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000091 self.checkequal(2, 'aaa', 'count', 'a', 1)
92 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000093 self.checkequal(1, 'aaa', 'count', 'a', -1)
94 self.checkequal(3, 'aaa', 'count', 'a', -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000095 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
96 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000097 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
98 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000099 self.checkequal(3, 'aaa', 'count', '', 1)
100 self.checkequal(1, 'aaa', 'count', '', 3)
101 self.checkequal(0, 'aaa', 'count', '', 10)
102 self.checkequal(2, 'aaa', 'count', '', -1)
103 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000104
105 self.checkraises(TypeError, 'hello', 'count')
106 self.checkraises(TypeError, 'hello', 'count', 42)
107
Raymond Hettinger57e74472005-02-20 09:54:53 +0000108 # For a variety of combinations,
109 # verify that str.count() matches an equivalent function
110 # replacing all occurrences and then differencing the string lengths
111 charset = ['', 'a', 'b']
112 digits = 7
113 base = len(charset)
114 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000115 for i in range(base ** digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000116 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000117 for j in range(digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000118 i, m = divmod(i, base)
119 entry.append(charset[m])
120 teststrings.add(''.join(entry))
121 teststrings = list(teststrings)
122 for i in teststrings:
123 i = self.fixtype(i)
124 n = len(i)
125 for j in teststrings:
126 r1 = i.count(j)
127 if j:
128 r2, rem = divmod(n - len(i.replace(j, '')), len(j))
129 else:
130 r2, rem = len(i)+1, 0
131 if rem or r1 != r2:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000132 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
133 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000134
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000135 def test_find(self):
136 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
137 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
138 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
139
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000140 self.checkequal(0, 'abc', 'find', '', 0)
141 self.checkequal(3, 'abc', 'find', '', 3)
142 self.checkequal(-1, 'abc', 'find', '', 4)
143
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000144 self.checkraises(TypeError, 'hello', 'find')
145 self.checkraises(TypeError, 'hello', 'find', 42)
146
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000147 # For a variety of combinations,
148 # verify that str.find() matches __contains__
149 # and that the found substring is really at that location
150 charset = ['', 'a', 'b', 'c']
151 digits = 5
152 base = len(charset)
153 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000154 for i in range(base ** digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000155 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000156 for j in range(digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000157 i, m = divmod(i, base)
158 entry.append(charset[m])
159 teststrings.add(''.join(entry))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000160 teststrings = list(teststrings)
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000161 for i in teststrings:
162 i = self.fixtype(i)
163 for j in teststrings:
164 loc = i.find(j)
165 r1 = (loc != -1)
166 r2 = j in i
167 if r1 != r2:
168 self.assertEqual(r1, r2)
169 if loc != -1:
170 self.assertEqual(i[loc:loc+len(j)], j)
171
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000172 def test_rfind(self):
173 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
174 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
175 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
176 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
177
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000178 self.checkequal(3, 'abc', 'rfind', '', 0)
179 self.checkequal(3, 'abc', 'rfind', '', 3)
180 self.checkequal(-1, 'abc', 'rfind', '', 4)
181
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000182 self.checkraises(TypeError, 'hello', 'rfind')
183 self.checkraises(TypeError, 'hello', 'rfind', 42)
184
185 def test_index(self):
186 self.checkequal(0, 'abcdefghiabc', 'index', '')
187 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
188 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
189 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
190
191 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
192 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
193 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
194 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
195
196 self.checkraises(TypeError, 'hello', 'index')
197 self.checkraises(TypeError, 'hello', 'index', 42)
198
199 def test_rindex(self):
200 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
201 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
202 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
203 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
204
205 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
206 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
207 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
208 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
209 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
210
211 self.checkraises(TypeError, 'hello', 'rindex')
212 self.checkraises(TypeError, 'hello', 'rindex', 42)
213
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000214 def test_lower(self):
215 self.checkequal('hello', 'HeLLo', 'lower')
216 self.checkequal('hello', 'hello', 'lower')
217 self.checkraises(TypeError, 'hello', 'lower', 42)
218
219 def test_upper(self):
220 self.checkequal('HELLO', 'HeLLo', 'upper')
221 self.checkequal('HELLO', 'HELLO', 'upper')
222 self.checkraises(TypeError, 'hello', 'upper', 42)
223
224 def test_expandtabs(self):
225 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
226 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
227 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
228 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
229 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
230 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
231 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
232 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
233
234 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
235 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
236 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
237 self.checkraises(OverflowError,
238 '\ta\n\tb', 'expandtabs', sys.maxint)
239
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000240 def test_split(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000241 # by a char
242 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000243 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000244 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
245 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
246 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
247 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000248 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
249 sys.maxint-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000250 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
251 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
252 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000253 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
254 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000255 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
256
Thomas Wouters477c8d52006-05-27 19:21:47 +0000257 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
258 self.checkequal(['a']*15 +['a|a|a|a|a'],
259 ('a|'*20)[:-1], 'split', '|', 15)
260
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000261 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000262 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000263 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
264 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
265 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
266 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000267 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
268 sys.maxint-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000269 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
270 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000271 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000272 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
273 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
274 'split', 'test')
275 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
276 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
277 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
278 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
279 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
280 self.checkequal([''], '', 'split', 'aaa')
281 self.checkequal(['aa'], 'aa', 'split', 'aaa')
282 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
283 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
284
285 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
286 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
287 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
288 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000289
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000290 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000291 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
292
Thomas Wouters477c8d52006-05-27 19:21:47 +0000293 # null case
294 self.checkraises(ValueError, 'hello', 'split', '')
295 self.checkraises(ValueError, 'hello', 'split', '', 0)
296
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000297 def test_rsplit(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000298 # by a char
299 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
300 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
301 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
302 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
303 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000304 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
305 sys.maxint-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000306 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
307 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
308 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
310 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
311
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000312 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
313
Thomas Wouters477c8d52006-05-27 19:21:47 +0000314 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
315 self.checkequal(['a|a|a|a|a']+['a']*15,
316 ('a|'*20)[:-1], 'rsplit', '|', 15)
317
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000318 # by string
319 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
320 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
321 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
322 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
323 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
325 sys.maxint-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000326 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
327 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
328 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
330 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
331 'rsplit', 'test')
332 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
333 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
334 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
335 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
336 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
337 self.checkequal([''], '', 'rsplit', 'aaa')
338 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
339 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
340 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
341
342 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
343 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
344 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
345 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000346
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000347 # argument type
348 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000349
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350 # null case
351 self.checkraises(ValueError, 'hello', 'rsplit', '')
352 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
353
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000354 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000355 EQ = self.checkequal
356
357 # Operations on the empty string
358 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000359 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000360 EQ("", "", "replace", "A", "")
361 EQ("", "", "replace", "A", "A")
362 EQ("", "", "replace", "", "", 100)
363 EQ("", "", "replace", "", "", sys.maxint)
364
365 # interleave (from=="", 'to' gets inserted everywhere)
366 EQ("A", "A", "replace", "", "")
367 EQ("*A*", "A", "replace", "", "*")
368 EQ("*1A*1", "A", "replace", "", "*1")
369 EQ("*-#A*-#", "A", "replace", "", "*-#")
370 EQ("*-A*-A*-", "AA", "replace", "", "*-")
371 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
372 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
373 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
374 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
375 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
376 EQ("*-AA", "AA", "replace", "", "*-", 1)
377 EQ("AA", "AA", "replace", "", "*-", 0)
378
379 # single character deletion (from=="A", to=="")
380 EQ("", "A", "replace", "A", "")
381 EQ("", "AAA", "replace", "A", "")
382 EQ("", "AAA", "replace", "A", "", -1)
383 EQ("", "AAA", "replace", "A", "", sys.maxint)
384 EQ("", "AAA", "replace", "A", "", 4)
385 EQ("", "AAA", "replace", "A", "", 3)
386 EQ("A", "AAA", "replace", "A", "", 2)
387 EQ("AA", "AAA", "replace", "A", "", 1)
388 EQ("AAA", "AAA", "replace", "A", "", 0)
389 EQ("", "AAAAAAAAAA", "replace", "A", "")
390 EQ("BCD", "ABACADA", "replace", "A", "")
391 EQ("BCD", "ABACADA", "replace", "A", "", -1)
392 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
393 EQ("BCD", "ABACADA", "replace", "A", "", 5)
394 EQ("BCD", "ABACADA", "replace", "A", "", 4)
395 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
396 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
397 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
398 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
399 EQ("BCD", "ABCAD", "replace", "A", "")
400 EQ("BCD", "ABCADAA", "replace", "A", "")
401 EQ("BCD", "BCD", "replace", "A", "")
402 EQ("*************", "*************", "replace", "A", "")
403 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
404
405 # substring deletion (from=="the", to=="")
406 EQ("", "the", "replace", "the", "")
407 EQ("ater", "theater", "replace", "the", "")
408 EQ("", "thethe", "replace", "the", "")
409 EQ("", "thethethethe", "replace", "the", "")
410 EQ("aaaa", "theatheatheathea", "replace", "the", "")
411 EQ("that", "that", "replace", "the", "")
412 EQ("thaet", "thaet", "replace", "the", "")
413 EQ("here and re", "here and there", "replace", "the", "")
414 EQ("here and re and re", "here and there and there",
415 "replace", "the", "", sys.maxint)
416 EQ("here and re and re", "here and there and there",
417 "replace", "the", "", -1)
418 EQ("here and re and re", "here and there and there",
419 "replace", "the", "", 3)
420 EQ("here and re and re", "here and there and there",
421 "replace", "the", "", 2)
422 EQ("here and re and there", "here and there and there",
423 "replace", "the", "", 1)
424 EQ("here and there and there", "here and there and there",
425 "replace", "the", "", 0)
426 EQ("here and re and re", "here and there and there", "replace", "the", "")
427
428 EQ("abc", "abc", "replace", "the", "")
429 EQ("abcdefg", "abcdefg", "replace", "the", "")
430
431 # substring deletion (from=="bob", to=="")
432 EQ("bob", "bbobob", "replace", "bob", "")
433 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
434 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
435 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
436
437 # single character replace in place (len(from)==len(to)==1)
438 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
439 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
440 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
441 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
442 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
443 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
444 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
445 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
446
447 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
448 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
449 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
450 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
451 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
452
453 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
454
455 # substring replace in place (len(from)==len(to) > 1)
456 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
457 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
458 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
459 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
460 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
461 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
462 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
463 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
464 EQ("cobob", "bobob", "replace", "bob", "cob")
465 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
466 EQ("bobob", "bobob", "replace", "bot", "bot")
467
468 # replace single character (len(from)==1, len(to)>1)
469 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
470 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
471 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
472 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
473 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
474 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
475 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
476
477 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
478
479 # replace substring (len(from)>1, len(to)!=len(from))
480 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
481 "replace", "spam", "ham")
482 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
483 "replace", "spam", "ham", sys.maxint)
484 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
485 "replace", "spam", "ham", -1)
486 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
487 "replace", "spam", "ham", 4)
488 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
489 "replace", "spam", "ham", 3)
490 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
491 "replace", "spam", "ham", 2)
492 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
493 "replace", "spam", "ham", 1)
494 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
495 "replace", "spam", "ham", 0)
496
497 EQ("bobob", "bobobob", "replace", "bobob", "bob")
498 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
499 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
500
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000501 ba = buffer('a')
502 bb = buffer('b')
503 EQ("bbc", "abc", "replace", ba, bb)
504 EQ("aac", "abc", "replace", bb, ba)
505
Thomas Wouters477c8d52006-05-27 19:21:47 +0000506 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000507 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
508 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
509 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
510 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
511 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
512 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
513 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
514 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
515 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
516 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
517 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
518 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
519 self.checkequal('', '', 'replace', '', '')
520 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
521 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
522 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
523 # MemoryError due to empty result (platform malloc issue when requesting
524 # 0 bytes).
525 self.checkequal('', '123', 'replace', '123', '')
526 self.checkequal('', '123123', 'replace', '123', '')
527 self.checkequal('x', '123x123', 'replace', '123', '')
528
529 self.checkraises(TypeError, 'hello', 'replace')
530 self.checkraises(TypeError, 'hello', 'replace', 42)
531 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
532 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
533
Thomas Wouters477c8d52006-05-27 19:21:47 +0000534 def test_replace_overflow(self):
535 # Check for overflow checking on 32 bit machines
Guido van Rossum360e4b82007-05-14 22:51:27 +0000536 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000537 return
538 A2_16 = "A" * (2**16)
539 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
540 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
541 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
542
Georg Brandlc7885542007-03-06 19:16:20 +0000543
544
545class CommonTest(BaseTest):
546 # This testcase contains test that can be used in all
547 # stringlike classes. Currently this is str, unicode
548 # UserString and the string module.
549
550 def test_hash(self):
551 # SF bug 1054139: += optimization was not invalidating cached hash value
552 a = self.type2test('DNSSEC')
553 b = self.type2test('')
554 for c in a:
555 b += c
556 hash(b)
557 self.assertEqual(hash(a), hash(b))
558
559 def test_capitalize(self):
560 self.checkequal(' hello ', ' hello ', 'capitalize')
561 self.checkequal('Hello ', 'Hello ','capitalize')
562 self.checkequal('Hello ', 'hello ','capitalize')
563 self.checkequal('Aaaa', 'aaaa', 'capitalize')
564 self.checkequal('Aaaa', 'AaAa', 'capitalize')
565
566 self.checkraises(TypeError, 'hello', 'capitalize', 42)
567
568 def test_lower(self):
569 self.checkequal('hello', 'HeLLo', 'lower')
570 self.checkequal('hello', 'hello', 'lower')
571 self.checkraises(TypeError, 'hello', 'lower', 42)
572
573 def test_upper(self):
574 self.checkequal('HELLO', 'HeLLo', 'upper')
575 self.checkequal('HELLO', 'HELLO', 'upper')
576 self.checkraises(TypeError, 'hello', 'upper', 42)
577
578 def test_expandtabs(self):
579 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
580 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
581 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
582 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
583 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
584 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
585 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
586
587 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
588
589 def test_additional_split(self):
590 self.checkequal(['this', 'is', 'the', 'split', 'function'],
591 'this is the split function', 'split')
592
593 # by whitespace
594 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
595 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
596 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
597 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
598 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
599 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
600 sys.maxint-1)
601 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
602 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
603 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
604
605 self.checkequal([], ' ', 'split')
606 self.checkequal(['a'], ' a ', 'split')
607 self.checkequal(['a', 'b'], ' a b ', 'split')
608 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
609 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
610 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
611 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
612 aaa = ' a '*20
613 self.checkequal(['a']*20, aaa, 'split')
614 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
615 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
616
617 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000618 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000619
620 def test_additional_rsplit(self):
621 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
622 'this is the rsplit function', 'rsplit')
623
624 # by whitespace
625 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
626 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
627 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
628 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
629 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
630 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
631 sys.maxint-20)
632 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
633 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
634 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
635
636 self.checkequal([], ' ', 'rsplit')
637 self.checkequal(['a'], ' a ', 'rsplit')
638 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
639 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
640 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
641 None, 1)
642 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
643 None, 2)
644 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
645 aaa = ' a '*20
646 self.checkequal(['a']*20, aaa, 'rsplit')
647 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
648 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
649
650 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000651 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000652
653 def test_strip(self):
654 self.checkequal('hello', ' hello ', 'strip')
655 self.checkequal('hello ', ' hello ', 'lstrip')
656 self.checkequal(' hello', ' hello ', 'rstrip')
657 self.checkequal('hello', 'hello', 'strip')
658
659 # strip/lstrip/rstrip with None arg
660 self.checkequal('hello', ' hello ', 'strip', None)
661 self.checkequal('hello ', ' hello ', 'lstrip', None)
662 self.checkequal(' hello', ' hello ', 'rstrip', None)
663 self.checkequal('hello', 'hello', 'strip', None)
664
665 # strip/lstrip/rstrip with str arg
666 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
667 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
668 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
669 self.checkequal('hello', 'hello', 'strip', 'xyz')
670
Georg Brandlc7885542007-03-06 19:16:20 +0000671 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
672 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
673 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
674
675 def test_ljust(self):
676 self.checkequal('abc ', 'abc', 'ljust', 10)
677 self.checkequal('abc ', 'abc', 'ljust', 6)
678 self.checkequal('abc', 'abc', 'ljust', 3)
679 self.checkequal('abc', 'abc', 'ljust', 2)
680 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
681 self.checkraises(TypeError, 'abc', 'ljust')
682
683 def test_rjust(self):
684 self.checkequal(' abc', 'abc', 'rjust', 10)
685 self.checkequal(' abc', 'abc', 'rjust', 6)
686 self.checkequal('abc', 'abc', 'rjust', 3)
687 self.checkequal('abc', 'abc', 'rjust', 2)
688 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
689 self.checkraises(TypeError, 'abc', 'rjust')
690
691 def test_center(self):
692 self.checkequal(' abc ', 'abc', 'center', 10)
693 self.checkequal(' abc ', 'abc', 'center', 6)
694 self.checkequal('abc', 'abc', 'center', 3)
695 self.checkequal('abc', 'abc', 'center', 2)
696 self.checkequal('***abc****', 'abc', 'center', 10, '*')
697 self.checkraises(TypeError, 'abc', 'center')
698
699 def test_swapcase(self):
700 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
701
702 self.checkraises(TypeError, 'hello', 'swapcase', 42)
703
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000704 def test_zfill(self):
705 self.checkequal('123', '123', 'zfill', 2)
706 self.checkequal('123', '123', 'zfill', 3)
707 self.checkequal('0123', '123', 'zfill', 4)
708 self.checkequal('+123', '+123', 'zfill', 3)
709 self.checkequal('+123', '+123', 'zfill', 4)
710 self.checkequal('+0123', '+123', 'zfill', 5)
711 self.checkequal('-123', '-123', 'zfill', 3)
712 self.checkequal('-123', '-123', 'zfill', 4)
713 self.checkequal('-0123', '-123', 'zfill', 5)
714 self.checkequal('000', '', 'zfill', 3)
715 self.checkequal('34', '34', 'zfill', 1)
716 self.checkequal('0034', '34', 'zfill', 4)
717
718 self.checkraises(TypeError, '123', 'zfill')
719
720class MixinStrUnicodeUserStringTest:
721 # additional tests that only work for
722 # stringlike objects, i.e. str, unicode, UserString
723 # (but not the string module)
724
725 def test_islower(self):
726 self.checkequal(False, '', 'islower')
727 self.checkequal(True, 'a', 'islower')
728 self.checkequal(False, 'A', 'islower')
729 self.checkequal(False, '\n', 'islower')
730 self.checkequal(True, 'abc', 'islower')
731 self.checkequal(False, 'aBc', 'islower')
732 self.checkequal(True, 'abc\n', 'islower')
733 self.checkraises(TypeError, 'abc', 'islower', 42)
734
735 def test_isupper(self):
736 self.checkequal(False, '', 'isupper')
737 self.checkequal(False, 'a', 'isupper')
738 self.checkequal(True, 'A', 'isupper')
739 self.checkequal(False, '\n', 'isupper')
740 self.checkequal(True, 'ABC', 'isupper')
741 self.checkequal(False, 'AbC', 'isupper')
742 self.checkequal(True, 'ABC\n', 'isupper')
743 self.checkraises(TypeError, 'abc', 'isupper', 42)
744
745 def test_istitle(self):
746 self.checkequal(False, '', 'istitle')
747 self.checkequal(False, 'a', 'istitle')
748 self.checkequal(True, 'A', 'istitle')
749 self.checkequal(False, '\n', 'istitle')
750 self.checkequal(True, 'A Titlecased Line', 'istitle')
751 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
752 self.checkequal(True, 'A Titlecased, Line', 'istitle')
753 self.checkequal(False, 'Not a capitalized String', 'istitle')
754 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
755 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
756 self.checkequal(False, 'NOT', 'istitle')
757 self.checkraises(TypeError, 'abc', 'istitle', 42)
758
759 def test_isspace(self):
760 self.checkequal(False, '', 'isspace')
761 self.checkequal(False, 'a', 'isspace')
762 self.checkequal(True, ' ', 'isspace')
763 self.checkequal(True, '\t', 'isspace')
764 self.checkequal(True, '\r', 'isspace')
765 self.checkequal(True, '\n', 'isspace')
766 self.checkequal(True, ' \t\r\n', 'isspace')
767 self.checkequal(False, ' \t\r\na', 'isspace')
768 self.checkraises(TypeError, 'abc', 'isspace', 42)
769
770 def test_isalpha(self):
771 self.checkequal(False, '', 'isalpha')
772 self.checkequal(True, 'a', 'isalpha')
773 self.checkequal(True, 'A', 'isalpha')
774 self.checkequal(False, '\n', 'isalpha')
775 self.checkequal(True, 'abc', 'isalpha')
776 self.checkequal(False, 'aBc123', 'isalpha')
777 self.checkequal(False, 'abc\n', 'isalpha')
778 self.checkraises(TypeError, 'abc', 'isalpha', 42)
779
780 def test_isalnum(self):
781 self.checkequal(False, '', 'isalnum')
782 self.checkequal(True, 'a', 'isalnum')
783 self.checkequal(True, 'A', 'isalnum')
784 self.checkequal(False, '\n', 'isalnum')
785 self.checkequal(True, '123abc456', 'isalnum')
786 self.checkequal(True, 'a1b3c', 'isalnum')
787 self.checkequal(False, 'aBc000 ', 'isalnum')
788 self.checkequal(False, 'abc\n', 'isalnum')
789 self.checkraises(TypeError, 'abc', 'isalnum', 42)
790
791 def test_isdigit(self):
792 self.checkequal(False, '', 'isdigit')
793 self.checkequal(False, 'a', 'isdigit')
794 self.checkequal(True, '0', 'isdigit')
795 self.checkequal(True, '0123456789', 'isdigit')
796 self.checkequal(False, '0123456789a', 'isdigit')
797
798 self.checkraises(TypeError, 'abc', 'isdigit', 42)
799
800 def test_title(self):
801 self.checkequal(' Hello ', ' hello ', 'title')
802 self.checkequal('Hello ', 'hello ', 'title')
803 self.checkequal('Hello ', 'Hello ', 'title')
804 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
805 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
806 self.checkequal('Getint', "getInt", 'title')
807 self.checkraises(TypeError, 'hello', 'title', 42)
808
809 def test_splitlines(self):
810 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
811 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
812 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
813 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
814 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
815 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
816 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
817
818 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
819
820 def test_startswith(self):
821 self.checkequal(True, 'hello', 'startswith', 'he')
822 self.checkequal(True, 'hello', 'startswith', 'hello')
823 self.checkequal(False, 'hello', 'startswith', 'hello world')
824 self.checkequal(True, 'hello', 'startswith', '')
825 self.checkequal(False, 'hello', 'startswith', 'ello')
826 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
827 self.checkequal(True, 'hello', 'startswith', 'o', 4)
828 self.checkequal(False, 'hello', 'startswith', 'o', 5)
829 self.checkequal(True, 'hello', 'startswith', '', 5)
830 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
831 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
832 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
833 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
834
835 # test negative indices
836 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
837 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
838 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
839 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
840 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
841 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
842 self.checkequal(False, 'hello', 'startswith', 'o', -2)
843 self.checkequal(True, 'hello', 'startswith', 'o', -1)
844 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
845 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
846
847 self.checkraises(TypeError, 'hello', 'startswith')
848 self.checkraises(TypeError, 'hello', 'startswith', 42)
849
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000850 # test tuple arguments
851 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
852 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
853 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
854 self.checkequal(False, 'hello', 'startswith', ())
855 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
856 'rld', 'lowo'), 3)
857 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
858 'rld'), 3)
859 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
860 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
861 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
862
863 self.checkraises(TypeError, 'hello', 'startswith', (42,))
864
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000865 def test_endswith(self):
866 self.checkequal(True, 'hello', 'endswith', 'lo')
867 self.checkequal(False, 'hello', 'endswith', 'he')
868 self.checkequal(True, 'hello', 'endswith', '')
869 self.checkequal(False, 'hello', 'endswith', 'hello world')
870 self.checkequal(False, 'helloworld', 'endswith', 'worl')
871 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
872 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
873 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
874 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
875 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
876 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
877 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
878 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
879 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
880
881 # test negative indices
882 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
883 self.checkequal(False, 'hello', 'endswith', 'he', -2)
884 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
885 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
886 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
887 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
888 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
889 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
890 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
891 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
892 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
893 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
894 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
895
896 self.checkraises(TypeError, 'hello', 'endswith')
897 self.checkraises(TypeError, 'hello', 'endswith', 42)
898
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000899 # test tuple arguments
900 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
901 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
902 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
903 self.checkequal(False, 'hello', 'endswith', ())
904 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
905 'rld', 'lowo'), 3)
906 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
907 'rld'), 3, -1)
908 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
909 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
910 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
911
912 self.checkraises(TypeError, 'hello', 'endswith', (42,))
913
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000914 def test___contains__(self):
915 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
916 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
917 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
918 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
919 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
920 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
921 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
922 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
923 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
924
925 def test_subscript(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000926 self.checkequal('a', 'abc', '__getitem__', 0)
927 self.checkequal('c', 'abc', '__getitem__', -1)
928 self.checkequal('a', 'abc', '__getitem__', 0)
929 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
930 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
931 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
932 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000933 # FIXME What about negative indices? This is handled differently by [] and __getitem__(slice)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000934
935 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
936
937 def test_slice(self):
938 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
939 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
940 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
941 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
942 self.checkequal('b', 'abc', '__getslice__', 1, 2)
943 self.checkequal('', 'abc', '__getslice__', 2, 2)
944 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
945 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
946 self.checkequal('', 'abc', '__getslice__', 2, 1)
947 # FIXME What about negative indizes? This is handled differently by [] and __getslice__
948
949 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
950
951 def test_mul(self):
952 self.checkequal('', 'abc', '__mul__', -1)
953 self.checkequal('', 'abc', '__mul__', 0)
954 self.checkequal('abc', 'abc', '__mul__', 1)
955 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
956 self.checkraises(TypeError, 'abc', '__mul__')
957 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +0000958 # XXX: on a 64-bit system, this doesn't raise an overflow error,
959 # but either raises a MemoryError, or succeeds (if you have 54TiB)
960 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000961
962 def test_join(self):
963 # join now works with any sequence type
964 # moved here, because the argument order is
965 # different in string.join (see the test in
966 # test.test_string.StringTest.test_join)
967 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
968 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000969 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
970 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000971 self.checkequal('w x y z', ' ', 'join', Sequence())
972 self.checkequal('abc', 'a', 'join', ('abc',))
973 self.checkequal('z', 'a', 'join', UserList(['z']))
Walter Dörwald67e83882007-05-05 12:26:27 +0000974 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
975 self.checkraises(TypeError, '.', 'join', ['a', 'b', 3])
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000976 for i in [5, 25, 125]:
977 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
978 ['a' * i] * i)
979 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
980 ('a' * i,) * i)
981
982 self.checkraises(TypeError, ' ', 'join', BadSeq1())
983 self.checkequal('a b c', ' ', 'join', BadSeq2())
984
985 self.checkraises(TypeError, ' ', 'join')
986 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000987 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123]))
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +0000988 try:
989 def f():
990 yield 4 + ""
991 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +0000992 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +0000993 if '+' not in str(e):
994 self.fail('join() ate exception message')
995 else:
996 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000997
998 def test_formatting(self):
999 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1000 self.checkequal('+10+', '+%d+', '__mod__', 10)
1001 self.checkequal('a', "%c", '__mod__', "a")
1002 self.checkequal('a', "%c", '__mod__', "a")
1003 self.checkequal('"', "%c", '__mod__', 34)
1004 self.checkequal('$', "%c", '__mod__', 36)
1005 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001006 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001007
1008 for ordinal in (-100, 0x200000):
1009 # unicode raises ValueError, str raises OverflowError
1010 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1011
1012 self.checkequal(' 42', '%3ld', '__mod__', 42)
1013 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001014 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001015
1016 self.checkraises(TypeError, 'abc', '__mod__')
1017 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1018 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1019 self.checkraises(TypeError, '%c', '__mod__', (None,))
1020 self.checkraises(ValueError, '%(foo', '__mod__', {})
1021 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1022
1023 # argument names with properly nested brackets are supported
1024 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1025
1026 # 100 is a magic number in PyUnicode_Format, this forces a resize
1027 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1028
1029 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1030 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1031 self.checkraises(ValueError, '%10', '__mod__', (42,))
1032
1033 def test_floatformatting(self):
1034 # float formatting
Guido van Rossum805365e2007-05-07 22:24:25 +00001035 for prec in range(100):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001036 format = '%%.%if' % prec
1037 value = 0.01
Guido van Rossum805365e2007-05-07 22:24:25 +00001038 for x in range(60):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001039 value = value * 3.141592655 / 3.0 * 10.0
1040 # The formatfloat() code in stringobject.c and
1041 # unicodeobject.c uses a 120 byte buffer and switches from
1042 # 'f' formatting to 'g' at precision 50, so we expect
1043 # OverflowErrors for the ranges x < 50 and prec >= 67.
1044 if x < 50 and prec >= 67:
1045 self.checkraises(OverflowError, format, "__mod__", value)
1046 else:
1047 self.checkcall(format, "__mod__", value)
1048
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049 def test_inplace_rewrites(self):
1050 # Check that strings don't copy and modify cached single-character strings
1051 self.checkequal('a', 'A', 'lower')
1052 self.checkequal(True, 'A', 'isupper')
1053 self.checkequal('A', 'a', 'upper')
1054 self.checkequal(True, 'a', 'islower')
1055
1056 self.checkequal('a', 'A', 'replace', 'A', 'a')
1057 self.checkequal(True, 'A', 'isupper')
1058
1059 self.checkequal('A', 'a', 'capitalize')
1060 self.checkequal(True, 'a', 'islower')
1061
1062 self.checkequal('A', 'a', 'swapcase')
1063 self.checkequal(True, 'a', 'islower')
1064
1065 self.checkequal('A', 'a', 'title')
1066 self.checkequal(True, 'a', 'islower')
1067
1068 def test_partition(self):
1069
1070 self.checkequal(('this is the par', 'ti', 'tion method'),
1071 'this is the partition method', 'partition', 'ti')
1072
1073 # from raymond's original specification
1074 S = 'http://www.python.org'
1075 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1076 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1077 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1078 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1079
1080 self.checkraises(ValueError, S, 'partition', '')
1081 self.checkraises(TypeError, S, 'partition', None)
1082
1083 def test_rpartition(self):
1084
1085 self.checkequal(('this is the rparti', 'ti', 'on method'),
1086 'this is the rpartition method', 'rpartition', 'ti')
1087
1088 # from raymond's original specification
1089 S = 'http://www.python.org'
1090 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001091 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001092 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1093 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1094
1095 self.checkraises(ValueError, S, 'rpartition', '')
1096 self.checkraises(TypeError, S, 'rpartition', None)
1097
Walter Dörwald57d88e52004-08-26 16:53:04 +00001098
Walter Dörwald57d88e52004-08-26 16:53:04 +00001099class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001100 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001101
1102 def test_bug1001011(self):
1103 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001104 # involving a subclass.
1105 # Make sure that it is of the appropriate type.
1106 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001107 t = self.type2test
1108 class subclass(t):
1109 pass
1110 s1 = subclass("abcd")
1111 s2 = t().join([s1])
1112 self.assert_(s1 is not s2)
1113 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001114
1115 s1 = t("abcd")
1116 s2 = t().join([s1])
1117 self.assert_(s1 is s2)
1118
1119 # Should also test mixed-type join.
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001120 if t is str:
Tim Peters108f1372004-08-27 05:36:07 +00001121 s1 = subclass("abcd")
1122 s2 = "".join([s1])
1123 self.assert_(s1 is not s2)
1124 self.assert_(type(s2) is t)
1125
1126 s1 = t("abcd")
1127 s2 = "".join([s1])
1128 self.assert_(s1 is s2)
1129
Walter Dörwald4c271fe2007-06-07 13:52:37 +00001130 elif t is str8:
Tim Peters108f1372004-08-27 05:36:07 +00001131 s1 = subclass("abcd")
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001132 s2 = "".join([s1])
Tim Peters108f1372004-08-27 05:36:07 +00001133 self.assert_(s1 is not s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001134 self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001135
1136 s1 = t("abcd")
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001137 s2 = "".join([s1])
Tim Peters108f1372004-08-27 05:36:07 +00001138 self.assert_(s1 is not s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001139 self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001140
1141 else:
1142 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)