2 Common tests shared by test_str, test_unicode, test_userstring and test_string.
5 import unittest
, string
, sys
6 from test
import test_support
7 from UserList
import UserList
10 def __init__(self
, seq
='wxyz'): self
.seq
= seq
11 def __len__(self
): return len(self
.seq
)
12 def __getitem__(self
, i
): return self
.seq
[i
]
14 class BadSeq1(Sequence
):
15 def __init__(self
): self
.seq
= [7, 'hello', 123L]
17 class BadSeq2(Sequence
):
18 def __init__(self
): self
.seq
= ['a', 'b', 'c']
19 def __len__(self
): return 8
21 class 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.
26 # The type to be tested
27 # Change in subclasses to change the behaviour of fixtesttype()
30 # 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):
42 (self
.fixtype(key
), self
.fixtype(value
))
43 for (key
, value
) in obj
.iteritems()
48 # 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
)
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
):
63 object = subtype(object)
64 realresult
= getattr(object, methodname
)(*args
)
65 self
.assert_(object is not realresult
)
67 # 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
)
73 getattr(object, methodname
),
77 # 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
)
84 # SF bug 1054139: += optimization was not invalidating cached hash value
85 a
= self
.type2test('DNSSEC')
86 b
= self
.type2test('')
90 self
.assertEqual(hash(a
), hash(b
))
92 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')
99 self
.checkraises(TypeError, 'hello', 'capitalize', 42)
101 def test_count(self
):
102 self
.checkequal(3, 'aaa', 'count', 'a')
103 self
.checkequal(0, 'aaa', 'count', 'b')
104 self
.checkequal(3, 'aaa', 'count', 'a')
105 self
.checkequal(0, 'aaa', 'count', 'b')
106 self
.checkequal(3, 'aaa', 'count', 'a')
107 self
.checkequal(0, 'aaa', 'count', 'b')
108 self
.checkequal(0, 'aaa', 'count', 'b')
109 self
.checkequal(1, 'aaa', 'count', 'a', -1)
110 self
.checkequal(3, 'aaa', 'count', 'a', -10)
111 self
.checkequal(2, 'aaa', 'count', 'a', 0, -1)
112 self
.checkequal(0, 'aaa', 'count', 'a', 0, -10)
114 self
.checkraises(TypeError, 'hello', 'count')
115 self
.checkraises(TypeError, 'hello', 'count', 42)
117 # For a variety of combinations,
118 # verify that str.count() matches an equivalent function
119 # replacing all occurrences and then differencing the string lengths
120 charset
= ['', 'a', 'b']
124 for i
in xrange(base
** digits
):
126 for j
in xrange(digits
):
127 i
, m
= divmod(i
, base
)
128 entry
.append(charset
[m
])
129 teststrings
.add(''.join(entry
))
130 teststrings
= list(teststrings
)
131 for i
in teststrings
:
134 for j
in teststrings
:
137 r2
, rem
= divmod(n
- len(i
.replace(j
, '')), len(j
))
139 r2
, rem
= len(i
)+1, 0
141 self
.assertEqual(rem
, 0)
142 self
.assertEqual(r1
, r2
)
145 self
.checkequal(0, 'abcdefghiabc', 'find', 'abc')
146 self
.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
147 self
.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
149 self
.checkraises(TypeError, 'hello', 'find')
150 self
.checkraises(TypeError, 'hello', 'find', 42)
152 # For a variety of combinations,
153 # verify that str.find() matches __contains__
154 # and that the found substring is really at that location
155 charset
= ['', 'a', 'b', 'c']
159 for i
in xrange(base
** digits
):
161 for j
in xrange(digits
):
162 i
, m
= divmod(i
, base
)
163 entry
.append(charset
[m
])
164 teststrings
.add(''.join(entry
))
165 teststrings
= list(teststrings
)
166 for i
in teststrings
:
168 for j
in teststrings
:
173 self
.assertEqual(r1
, r2
)
175 self
.assertEqual(i
[loc
:loc
+len(j
)], j
)
177 def test_rfind(self
):
178 self
.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
179 self
.checkequal(12, 'abcdefghiabc', 'rfind', '')
180 self
.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
181 self
.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
183 self
.checkraises(TypeError, 'hello', 'rfind')
184 self
.checkraises(TypeError, 'hello', 'rfind', 42)
186 def test_index(self
):
187 self
.checkequal(0, 'abcdefghiabc', 'index', '')
188 self
.checkequal(3, 'abcdefghiabc', 'index', 'def')
189 self
.checkequal(0, 'abcdefghiabc', 'index', 'abc')
190 self
.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
192 self
.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
193 self
.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
194 self
.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
195 self
.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
197 self
.checkraises(TypeError, 'hello', 'index')
198 self
.checkraises(TypeError, 'hello', 'index', 42)
200 def test_rindex(self
):
201 self
.checkequal(12, 'abcdefghiabc', 'rindex', '')
202 self
.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
203 self
.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
204 self
.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
206 self
.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
207 self
.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
208 self
.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
209 self
.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
210 self
.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
212 self
.checkraises(TypeError, 'hello', 'rindex')
213 self
.checkraises(TypeError, 'hello', 'rindex', 42)
215 def test_lower(self
):
216 self
.checkequal('hello', 'HeLLo', 'lower')
217 self
.checkequal('hello', 'hello', 'lower')
218 self
.checkraises(TypeError, 'hello', 'lower', 42)
220 def test_upper(self
):
221 self
.checkequal('HELLO', 'HeLLo', 'upper')
222 self
.checkequal('HELLO', 'HELLO', 'upper')
223 self
.checkraises(TypeError, 'hello', 'upper', 42)
225 def test_expandtabs(self
):
226 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
227 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
228 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
229 self
.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
230 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
231 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
232 self
.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
234 self
.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
236 def test_split(self
):
237 self
.checkequal(['this', 'is', 'the', 'split', 'function'],
238 'this is the split function', 'split')
241 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
242 self
.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
243 self
.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
244 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
245 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
246 self
.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
247 self
.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
250 self
.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
251 self
.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
252 self
.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
253 self
.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
254 self
.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
255 self
.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
256 self
.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
257 self
.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
258 self
.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
261 self
.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
262 self
.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
263 self
.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
264 self
.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
265 self
.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
266 self
.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
267 self
.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
268 self
.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
270 # mixed use of str and unicode
271 self
.checkequal([u
'a', u
'b', u
'c d'], 'a b c d', 'split', u
' ', 2)
274 self
.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
276 def test_rsplit(self
):
277 self
.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
278 'this is the rsplit function', 'rsplit')
281 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
282 self
.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
283 self
.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
284 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
285 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
286 self
.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
287 self
.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
290 self
.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
291 self
.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
292 self
.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
293 self
.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
294 self
.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
295 self
.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
296 self
.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
297 self
.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
298 self
.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
301 self
.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
302 self
.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
303 self
.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
304 self
.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
305 self
.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
306 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'], 'test begincase', 'rsplit', 'test')
310 # mixed use of str and unicode
311 self
.checkequal([u
'a b', u
'c', u
'd'], 'a b c d', 'rsplit', u
' ', 2)
314 self
.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
316 def test_strip(self
):
317 self
.checkequal('hello', ' hello ', 'strip')
318 self
.checkequal('hello ', ' hello ', 'lstrip')
319 self
.checkequal(' hello', ' hello ', 'rstrip')
320 self
.checkequal('hello', 'hello', 'strip')
322 # strip/lstrip/rstrip with None arg
323 self
.checkequal('hello', ' hello ', 'strip', None)
324 self
.checkequal('hello ', ' hello ', 'lstrip', None)
325 self
.checkequal(' hello', ' hello ', 'rstrip', None)
326 self
.checkequal('hello', 'hello', 'strip', None)
328 # strip/lstrip/rstrip with str arg
329 self
.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
330 self
.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
331 self
.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
332 self
.checkequal('hello', 'hello', 'strip', 'xyz')
334 # strip/lstrip/rstrip with unicode arg
335 if test_support
.have_unicode
:
336 self
.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
337 'strip', unicode('xyz', 'ascii'))
338 self
.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
339 'lstrip', unicode('xyz', 'ascii'))
340 self
.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
341 'rstrip', unicode('xyz', 'ascii'))
342 self
.checkequal(unicode('hello', 'ascii'), 'hello',
343 'strip', unicode('xyz', 'ascii'))
345 self
.checkraises(TypeError, 'hello', 'strip', 42, 42)
346 self
.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
347 self
.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
349 def test_ljust(self
):
350 self
.checkequal('abc ', 'abc', 'ljust', 10)
351 self
.checkequal('abc ', 'abc', 'ljust', 6)
352 self
.checkequal('abc', 'abc', 'ljust', 3)
353 self
.checkequal('abc', 'abc', 'ljust', 2)
354 self
.checkequal('abc*******', 'abc', 'ljust', 10, '*')
355 self
.checkraises(TypeError, 'abc', 'ljust')
357 def test_rjust(self
):
358 self
.checkequal(' abc', 'abc', 'rjust', 10)
359 self
.checkequal(' abc', 'abc', 'rjust', 6)
360 self
.checkequal('abc', 'abc', 'rjust', 3)
361 self
.checkequal('abc', 'abc', 'rjust', 2)
362 self
.checkequal('*******abc', 'abc', 'rjust', 10, '*')
363 self
.checkraises(TypeError, 'abc', 'rjust')
365 def test_center(self
):
366 self
.checkequal(' abc ', 'abc', 'center', 10)
367 self
.checkequal(' abc ', 'abc', 'center', 6)
368 self
.checkequal('abc', 'abc', 'center', 3)
369 self
.checkequal('abc', 'abc', 'center', 2)
370 self
.checkequal('***abc****', 'abc', 'center', 10, '*')
371 self
.checkraises(TypeError, 'abc', 'center')
373 def test_swapcase(self
):
374 self
.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
376 self
.checkraises(TypeError, 'hello', 'swapcase', 42)
378 def test_replace(self
):
379 self
.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
380 self
.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
381 self
.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
382 self
.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
383 self
.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
384 self
.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
385 self
.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
386 self
.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
387 self
.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
388 self
.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
389 self
.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
390 self
.checkequal('abc', 'abc', 'replace', '', '-', 0)
391 self
.checkequal('', '', 'replace', '', '')
392 self
.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
393 self
.checkequal('abc', 'abc', 'replace', 'xy', '--')
394 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
395 # MemoryError due to empty result (platform malloc issue when requesting
397 self
.checkequal('', '123', 'replace', '123', '')
398 self
.checkequal('', '123123', 'replace', '123', '')
399 self
.checkequal('x', '123x123', 'replace', '123', '')
401 self
.checkraises(TypeError, 'hello', 'replace')
402 self
.checkraises(TypeError, 'hello', 'replace', 42)
403 self
.checkraises(TypeError, 'hello', 'replace', 42, 'h')
404 self
.checkraises(TypeError, 'hello', 'replace', 'h', 42)
406 def test_zfill(self
):
407 self
.checkequal('123', '123', 'zfill', 2)
408 self
.checkequal('123', '123', 'zfill', 3)
409 self
.checkequal('0123', '123', 'zfill', 4)
410 self
.checkequal('+123', '+123', 'zfill', 3)
411 self
.checkequal('+123', '+123', 'zfill', 4)
412 self
.checkequal('+0123', '+123', 'zfill', 5)
413 self
.checkequal('-123', '-123', 'zfill', 3)
414 self
.checkequal('-123', '-123', 'zfill', 4)
415 self
.checkequal('-0123', '-123', 'zfill', 5)
416 self
.checkequal('000', '', 'zfill', 3)
417 self
.checkequal('34', '34', 'zfill', 1)
418 self
.checkequal('0034', '34', 'zfill', 4)
420 self
.checkraises(TypeError, '123', 'zfill')
422 class MixinStrUnicodeUserStringTest
:
423 # additional tests that only work for
424 # stringlike objects, i.e. str, unicode, UserString
425 # (but not the string module)
427 def test_islower(self
):
428 self
.checkequal(False, '', 'islower')
429 self
.checkequal(True, 'a', 'islower')
430 self
.checkequal(False, 'A', 'islower')
431 self
.checkequal(False, '\n', 'islower')
432 self
.checkequal(True, 'abc', 'islower')
433 self
.checkequal(False, 'aBc', 'islower')
434 self
.checkequal(True, 'abc\n', 'islower')
435 self
.checkraises(TypeError, 'abc', 'islower', 42)
437 def test_isupper(self
):
438 self
.checkequal(False, '', 'isupper')
439 self
.checkequal(False, 'a', 'isupper')
440 self
.checkequal(True, 'A', 'isupper')
441 self
.checkequal(False, '\n', 'isupper')
442 self
.checkequal(True, 'ABC', 'isupper')
443 self
.checkequal(False, 'AbC', 'isupper')
444 self
.checkequal(True, 'ABC\n', 'isupper')
445 self
.checkraises(TypeError, 'abc', 'isupper', 42)
447 def test_istitle(self
):
448 self
.checkequal(False, '', 'istitle')
449 self
.checkequal(False, 'a', 'istitle')
450 self
.checkequal(True, 'A', 'istitle')
451 self
.checkequal(False, '\n', 'istitle')
452 self
.checkequal(True, 'A Titlecased Line', 'istitle')
453 self
.checkequal(True, 'A\nTitlecased Line', 'istitle')
454 self
.checkequal(True, 'A Titlecased, Line', 'istitle')
455 self
.checkequal(False, 'Not a capitalized String', 'istitle')
456 self
.checkequal(False, 'Not\ta Titlecase String', 'istitle')
457 self
.checkequal(False, 'Not--a Titlecase String', 'istitle')
458 self
.checkequal(False, 'NOT', 'istitle')
459 self
.checkraises(TypeError, 'abc', 'istitle', 42)
461 def test_isspace(self
):
462 self
.checkequal(False, '', 'isspace')
463 self
.checkequal(False, 'a', 'isspace')
464 self
.checkequal(True, ' ', 'isspace')
465 self
.checkequal(True, '\t', 'isspace')
466 self
.checkequal(True, '\r', 'isspace')
467 self
.checkequal(True, '\n', 'isspace')
468 self
.checkequal(True, ' \t\r\n', 'isspace')
469 self
.checkequal(False, ' \t\r\na', 'isspace')
470 self
.checkraises(TypeError, 'abc', 'isspace', 42)
472 def test_isalpha(self
):
473 self
.checkequal(False, '', 'isalpha')
474 self
.checkequal(True, 'a', 'isalpha')
475 self
.checkequal(True, 'A', 'isalpha')
476 self
.checkequal(False, '\n', 'isalpha')
477 self
.checkequal(True, 'abc', 'isalpha')
478 self
.checkequal(False, 'aBc123', 'isalpha')
479 self
.checkequal(False, 'abc\n', 'isalpha')
480 self
.checkraises(TypeError, 'abc', 'isalpha', 42)
482 def test_isalnum(self
):
483 self
.checkequal(False, '', 'isalnum')
484 self
.checkequal(True, 'a', 'isalnum')
485 self
.checkequal(True, 'A', 'isalnum')
486 self
.checkequal(False, '\n', 'isalnum')
487 self
.checkequal(True, '123abc456', 'isalnum')
488 self
.checkequal(True, 'a1b3c', 'isalnum')
489 self
.checkequal(False, 'aBc000 ', 'isalnum')
490 self
.checkequal(False, 'abc\n', 'isalnum')
491 self
.checkraises(TypeError, 'abc', 'isalnum', 42)
493 def test_isdigit(self
):
494 self
.checkequal(False, '', 'isdigit')
495 self
.checkequal(False, 'a', 'isdigit')
496 self
.checkequal(True, '0', 'isdigit')
497 self
.checkequal(True, '0123456789', 'isdigit')
498 self
.checkequal(False, '0123456789a', 'isdigit')
500 self
.checkraises(TypeError, 'abc', 'isdigit', 42)
502 def test_title(self
):
503 self
.checkequal(' Hello ', ' hello ', 'title')
504 self
.checkequal('Hello ', 'hello ', 'title')
505 self
.checkequal('Hello ', 'Hello ', 'title')
506 self
.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
507 self
.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
508 self
.checkequal('Getint', "getInt", 'title')
509 self
.checkraises(TypeError, 'hello', 'title', 42)
511 def test_splitlines(self
):
512 self
.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
513 self
.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
514 self
.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
515 self
.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
516 self
.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
517 self
.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
518 self
.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
520 self
.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
522 def test_startswith(self
):
523 self
.checkequal(True, 'hello', 'startswith', 'he')
524 self
.checkequal(True, 'hello', 'startswith', 'hello')
525 self
.checkequal(False, 'hello', 'startswith', 'hello world')
526 self
.checkequal(True, 'hello', 'startswith', '')
527 self
.checkequal(False, 'hello', 'startswith', 'ello')
528 self
.checkequal(True, 'hello', 'startswith', 'ello', 1)
529 self
.checkequal(True, 'hello', 'startswith', 'o', 4)
530 self
.checkequal(False, 'hello', 'startswith', 'o', 5)
531 self
.checkequal(True, 'hello', 'startswith', '', 5)
532 self
.checkequal(False, 'hello', 'startswith', 'lo', 6)
533 self
.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
534 self
.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
535 self
.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
537 # test negative indices
538 self
.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
539 self
.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
540 self
.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
541 self
.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
542 self
.checkequal(False, 'hello', 'startswith', 'ello', -5)
543 self
.checkequal(True, 'hello', 'startswith', 'ello', -4)
544 self
.checkequal(False, 'hello', 'startswith', 'o', -2)
545 self
.checkequal(True, 'hello', 'startswith', 'o', -1)
546 self
.checkequal(True, 'hello', 'startswith', '', -3, -3)
547 self
.checkequal(False, 'hello', 'startswith', 'lo', -9)
549 self
.checkraises(TypeError, 'hello', 'startswith')
550 self
.checkraises(TypeError, 'hello', 'startswith', 42)
552 def test_endswith(self
):
553 self
.checkequal(True, 'hello', 'endswith', 'lo')
554 self
.checkequal(False, 'hello', 'endswith', 'he')
555 self
.checkequal(True, 'hello', 'endswith', '')
556 self
.checkequal(False, 'hello', 'endswith', 'hello world')
557 self
.checkequal(False, 'helloworld', 'endswith', 'worl')
558 self
.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
559 self
.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
560 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
561 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
562 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
563 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
564 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
565 self
.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
566 self
.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
568 # test negative indices
569 self
.checkequal(True, 'hello', 'endswith', 'lo', -2)
570 self
.checkequal(False, 'hello', 'endswith', 'he', -2)
571 self
.checkequal(True, 'hello', 'endswith', '', -3, -3)
572 self
.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
573 self
.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
574 self
.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
575 self
.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
576 self
.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
577 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
578 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
579 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
580 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
581 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
583 self
.checkraises(TypeError, 'hello', 'endswith')
584 self
.checkraises(TypeError, 'hello', 'endswith', 42)
586 def test___contains__(self
):
587 self
.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
588 self
.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
589 self
.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
590 self
.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
591 self
.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
592 self
.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
593 self
.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
594 self
.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
595 self
.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
597 def test_subscript(self
):
598 self
.checkequal(u
'a', 'abc', '__getitem__', 0)
599 self
.checkequal(u
'c', 'abc', '__getitem__', -1)
600 self
.checkequal(u
'a', 'abc', '__getitem__', 0L)
601 self
.checkequal(u
'abc', 'abc', '__getitem__', slice(0, 3))
602 self
.checkequal(u
'abc', 'abc', '__getitem__', slice(0, 1000))
603 self
.checkequal(u
'a', 'abc', '__getitem__', slice(0, 1))
604 self
.checkequal(u
'', 'abc', '__getitem__', slice(0, 0))
605 # FIXME What about negative indizes? This is handled differently by [] and __getitem__(slice)
607 self
.checkraises(TypeError, 'abc', '__getitem__', 'def')
609 def test_slice(self
):
610 self
.checkequal('abc', 'abc', '__getslice__', 0, 1000)
611 self
.checkequal('abc', 'abc', '__getslice__', 0, 3)
612 self
.checkequal('ab', 'abc', '__getslice__', 0, 2)
613 self
.checkequal('bc', 'abc', '__getslice__', 1, 3)
614 self
.checkequal('b', 'abc', '__getslice__', 1, 2)
615 self
.checkequal('', 'abc', '__getslice__', 2, 2)
616 self
.checkequal('', 'abc', '__getslice__', 1000, 1000)
617 self
.checkequal('', 'abc', '__getslice__', 2000, 1000)
618 self
.checkequal('', 'abc', '__getslice__', 2, 1)
619 # FIXME What about negative indizes? This is handled differently by [] and __getslice__
621 self
.checkraises(TypeError, 'abc', '__getslice__', 'def')
624 self
.checkequal('', 'abc', '__mul__', -1)
625 self
.checkequal('', 'abc', '__mul__', 0)
626 self
.checkequal('abc', 'abc', '__mul__', 1)
627 self
.checkequal('abcabcabc', 'abc', '__mul__', 3)
628 self
.checkraises(TypeError, 'abc', '__mul__')
629 self
.checkraises(TypeError, 'abc', '__mul__', '')
630 self
.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
633 # join now works with any sequence type
634 # moved here, because the argument order is
635 # different in string.join (see the test in
636 # test.test_string.StringTest.test_join)
637 self
.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
638 self
.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
639 self
.checkequal('w x y z', ' ', 'join', Sequence())
640 self
.checkequal('abc', 'a', 'join', ('abc',))
641 self
.checkequal('z', 'a', 'join', UserList(['z']))
642 if test_support
.have_unicode
:
643 self
.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
644 self
.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
645 self
.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
646 self
.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
647 self
.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
648 for i
in [5, 25, 125]:
649 self
.checkequal(((('a' * i
) + '-') * i
)[:-1], '-', 'join',
651 self
.checkequal(((('a' * i
) + '-') * i
)[:-1], '-', 'join',
654 self
.checkraises(TypeError, ' ', 'join', BadSeq1())
655 self
.checkequal('a b c', ' ', 'join', BadSeq2())
657 self
.checkraises(TypeError, ' ', 'join')
658 self
.checkraises(TypeError, ' ', 'join', 7)
659 self
.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
663 self
.fixtype(' ').join(f())
665 if '+' not in str(e
):
666 self
.fail('join() ate exception message')
668 self
.fail('exception not raised')
670 def test_formatting(self
):
671 self
.checkequal('+hello+', '+%s+', '__mod__', 'hello')
672 self
.checkequal('+10+', '+%d+', '__mod__', 10)
673 self
.checkequal('a', "%c", '__mod__', "a")
674 self
.checkequal('a', "%c", '__mod__', "a")
675 self
.checkequal('"', "%c", '__mod__', 34)
676 self
.checkequal('$', "%c", '__mod__', 36)
677 self
.checkequal('10', "%d", '__mod__', 10)
678 self
.checkequal('\x7f', "%c", '__mod__', 0x7f)
680 for ordinal
in (-100, 0x200000):
681 # unicode raises ValueError, str raises OverflowError
682 self
.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal
)
684 self
.checkequal(' 42', '%3ld', '__mod__', 42)
685 self
.checkequal('0042.00', '%07.2f', '__mod__', 42)
686 self
.checkequal('0042.00', '%07.2F', '__mod__', 42)
688 self
.checkraises(TypeError, 'abc', '__mod__')
689 self
.checkraises(TypeError, '%(foo)s', '__mod__', 42)
690 self
.checkraises(TypeError, '%s%s', '__mod__', (42,))
691 self
.checkraises(TypeError, '%c', '__mod__', (None,))
692 self
.checkraises(ValueError, '%(foo', '__mod__', {})
693 self
.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
695 # argument names with properly nested brackets are supported
696 self
.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
698 # 100 is a magic number in PyUnicode_Format, this forces a resize
699 self
.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
701 self
.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
702 self
.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
703 self
.checkraises(ValueError, '%10', '__mod__', (42,))
705 def test_floatformatting(self
):
707 for prec
in xrange(100):
708 format
= '%%.%if' % prec
711 value
= value
* 3.141592655 / 3.0 * 10.0
712 # The formatfloat() code in stringobject.c and
713 # unicodeobject.c uses a 120 byte buffer and switches from
714 # 'f' formatting to 'g' at precision 50, so we expect
715 # OverflowErrors for the ranges x < 50 and prec >= 67.
716 if x
< 50 and prec
>= 67:
717 self
.checkraises(OverflowError, format
, "__mod__", value
)
719 self
.checkcall(format
, "__mod__", value
)
722 class MixinStrStringUserStringTest
:
723 # Additional tests for 8bit strings, i.e. str, UserString and
726 def test_maketrans(self
):
728 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
729 string
.maketrans('abc', 'xyz')
731 self
.assertRaises(ValueError, string
.maketrans
, 'abc', 'xyzw')
733 def test_translate(self
):
734 table
= string
.maketrans('abc', 'xyz')
735 self
.checkequal('xyzxyz', 'xyzabcdef', 'translate', table
, 'def')
737 table
= string
.maketrans('a', 'A')
738 self
.checkequal('Abc', 'abc', 'translate', table
)
739 self
.checkequal('xyz', 'xyz', 'translate', table
)
740 self
.checkequal('yz', 'xyz', 'translate', table
, 'x')
741 self
.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
742 self
.checkraises(ValueError, 'xyz', 'translate', 'too short')
745 class MixinStrUserStringTest
:
746 # Additional tests that only work with
747 # 8bit compatible object, i.e. str and UserString
749 if test_support
.have_unicode
:
750 def test_encoding_decoding(self
):
751 codecs
= [('rot13', 'uryyb jbeyq'),
752 ('base64', 'aGVsbG8gd29ybGQ=\n'),
753 ('hex', '68656c6c6f20776f726c64'),
754 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
755 for encoding
, data
in codecs
:
756 self
.checkequal(data
, 'hello world', 'encode', encoding
)
757 self
.checkequal('hello world', data
, 'decode', encoding
)
758 # zlib is optional, so we make the test optional too...
764 data
= 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
765 self
.checkequal(data
, 'hello world', 'encode', 'zlib')
766 self
.checkequal('hello world', data
, 'decode', 'zlib')
768 self
.checkraises(TypeError, 'xyz', 'decode', 42)
769 self
.checkraises(TypeError, 'xyz', 'encode', 42)
772 class MixinStrUnicodeTest
:
773 # Additional tests that only work with str and unicode.
775 def test_bug1001011(self
):
776 # Make sure join returns a NEW object for single item sequences
777 # involving a subclass.
778 # Make sure that it is of the appropriate type.
779 # Check the optimisation still occurs for standard objects.
783 s1
= subclass("abcd")
785 self
.assert_(s1
is not s2
)
786 self
.assert_(type(s2
) is t
)
790 self
.assert_(s1
is s2
)
792 # Should also test mixed-type join.
794 s1
= subclass("abcd")
796 self
.assert_(s1
is not s2
)
797 self
.assert_(type(s2
) is t
)
801 self
.assert_(s1
is s2
)
804 s1
= subclass("abcd")
806 self
.assert_(s1
is not s2
)
807 self
.assert_(type(s2
) is unicode) # promotes!
811 self
.assert_(s1
is not s2
)
812 self
.assert_(type(s2
) is unicode) # promotes!
815 self
.fail("unexpected type for MixinStrUnicodeTest %r" % t
)