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
)
83 def test_capitalize(self
):
84 self
.checkequal(' hello ', ' hello ', 'capitalize')
85 self
.checkequal('Hello ', 'Hello ','capitalize')
86 self
.checkequal('Hello ', 'hello ','capitalize')
87 self
.checkequal('Aaaa', 'aaaa', 'capitalize')
88 self
.checkequal('Aaaa', 'AaAa', 'capitalize')
90 self
.checkraises(TypeError, 'hello', 'capitalize', 42)
93 self
.checkequal(3, 'aaa', 'count', 'a')
94 self
.checkequal(0, 'aaa', 'count', 'b')
95 self
.checkequal(3, 'aaa', 'count', 'a')
96 self
.checkequal(0, 'aaa', 'count', 'b')
97 self
.checkequal(3, 'aaa', 'count', 'a')
98 self
.checkequal(0, 'aaa', 'count', 'b')
99 self
.checkequal(0, 'aaa', 'count', 'b')
100 self
.checkequal(1, 'aaa', 'count', 'a', -1)
101 self
.checkequal(3, 'aaa', 'count', 'a', -10)
102 self
.checkequal(2, 'aaa', 'count', 'a', 0, -1)
103 self
.checkequal(0, 'aaa', 'count', 'a', 0, -10)
105 self
.checkraises(TypeError, 'hello', 'count')
106 self
.checkraises(TypeError, 'hello', 'count', 42)
109 self
.checkequal(0, 'abcdefghiabc', 'find', 'abc')
110 self
.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
111 self
.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
113 self
.checkraises(TypeError, 'hello', 'find')
114 self
.checkraises(TypeError, 'hello', 'find', 42)
116 def test_rfind(self
):
117 self
.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
118 self
.checkequal(12, 'abcdefghiabc', 'rfind', '')
119 self
.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
120 self
.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
122 self
.checkraises(TypeError, 'hello', 'rfind')
123 self
.checkraises(TypeError, 'hello', 'rfind', 42)
125 def test_index(self
):
126 self
.checkequal(0, 'abcdefghiabc', 'index', '')
127 self
.checkequal(3, 'abcdefghiabc', 'index', 'def')
128 self
.checkequal(0, 'abcdefghiabc', 'index', 'abc')
129 self
.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
131 self
.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
132 self
.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
133 self
.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
134 self
.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
136 self
.checkraises(TypeError, 'hello', 'index')
137 self
.checkraises(TypeError, 'hello', 'index', 42)
139 def test_rindex(self
):
140 self
.checkequal(12, 'abcdefghiabc', 'rindex', '')
141 self
.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
142 self
.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
143 self
.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
145 self
.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
146 self
.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
147 self
.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
148 self
.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
149 self
.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
151 self
.checkraises(TypeError, 'hello', 'rindex')
152 self
.checkraises(TypeError, 'hello', 'rindex', 42)
154 def test_lower(self
):
155 self
.checkequal('hello', 'HeLLo', 'lower')
156 self
.checkequal('hello', 'hello', 'lower')
157 self
.checkraises(TypeError, 'hello', 'lower', 42)
159 def test_upper(self
):
160 self
.checkequal('HELLO', 'HeLLo', 'upper')
161 self
.checkequal('HELLO', 'HELLO', 'upper')
162 self
.checkraises(TypeError, 'hello', 'upper', 42)
164 def test_expandtabs(self
):
165 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
166 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
167 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
168 self
.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
169 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
170 self
.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
171 self
.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
173 self
.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
175 def test_split(self
):
176 self
.checkequal(['this', 'is', 'the', 'split', 'function'],
177 'this is the split function', 'split')
178 self
.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
179 self
.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
180 self
.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
181 self
.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
182 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
183 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
184 self
.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
185 self
.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
186 self
.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
187 self
.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
188 self
.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
190 self
.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
192 def test_strip(self
):
193 self
.checkequal('hello', ' hello ', 'strip')
194 self
.checkequal('hello ', ' hello ', 'lstrip')
195 self
.checkequal(' hello', ' hello ', 'rstrip')
196 self
.checkequal('hello', 'hello', 'strip')
198 # strip/lstrip/rstrip with None arg
199 self
.checkequal('hello', ' hello ', 'strip', None)
200 self
.checkequal('hello ', ' hello ', 'lstrip', None)
201 self
.checkequal(' hello', ' hello ', 'rstrip', None)
202 self
.checkequal('hello', 'hello', 'strip', None)
204 # strip/lstrip/rstrip with str arg
205 self
.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
206 self
.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
207 self
.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
208 self
.checkequal('hello', 'hello', 'strip', 'xyz')
210 # strip/lstrip/rstrip with unicode arg
211 if test_support
.have_unicode
:
212 self
.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
213 'strip', unicode('xyz', 'ascii'))
214 self
.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
215 'lstrip', unicode('xyz', 'ascii'))
216 self
.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
217 'rstrip', unicode('xyz', 'ascii'))
218 self
.checkequal(unicode('hello', 'ascii'), 'hello',
219 'strip', unicode('xyz', 'ascii'))
221 self
.checkraises(TypeError, 'hello', 'strip', 42, 42)
222 self
.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
223 self
.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
225 def test_ljust(self
):
226 self
.checkequal('abc ', 'abc', 'ljust', 10)
227 self
.checkequal('abc ', 'abc', 'ljust', 6)
228 self
.checkequal('abc', 'abc', 'ljust', 3)
229 self
.checkequal('abc', 'abc', 'ljust', 2)
231 self
.checkraises(TypeError, 'abc', 'ljust')
233 def test_rjust(self
):
234 self
.checkequal(' abc', 'abc', 'rjust', 10)
235 self
.checkequal(' abc', 'abc', 'rjust', 6)
236 self
.checkequal('abc', 'abc', 'rjust', 3)
237 self
.checkequal('abc', 'abc', 'rjust', 2)
239 self
.checkraises(TypeError, 'abc', 'rjust')
241 def test_center(self
):
242 self
.checkequal(' abc ', 'abc', 'center', 10)
243 self
.checkequal(' abc ', 'abc', 'center', 6)
244 self
.checkequal('abc', 'abc', 'center', 3)
245 self
.checkequal('abc', 'abc', 'center', 2)
247 self
.checkraises(TypeError, 'abc', 'center')
249 def test_swapcase(self
):
250 self
.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
252 self
.checkraises(TypeError, 'hello', 'swapcase', 42)
254 def test_replace(self
):
255 self
.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
256 self
.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
257 self
.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
258 self
.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
259 self
.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
260 self
.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
261 self
.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
262 self
.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
263 self
.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
264 self
.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
265 self
.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
266 self
.checkequal('abc', 'abc', 'replace', '', '-', 0)
267 self
.checkequal('', '', 'replace', '', '')
268 self
.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
269 self
.checkequal('abc', 'abc', 'replace', 'xy', '--')
270 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
271 # MemoryError due to empty result (platform malloc issue when requesting
273 self
.checkequal('', '123', 'replace', '123', '')
274 self
.checkequal('', '123123', 'replace', '123', '')
275 self
.checkequal('x', '123x123', 'replace', '123', '')
277 self
.checkraises(TypeError, 'hello', 'replace')
278 self
.checkraises(TypeError, 'hello', 'replace', 42)
279 self
.checkraises(TypeError, 'hello', 'replace', 42, 'h')
280 self
.checkraises(TypeError, 'hello', 'replace', 'h', 42)
282 def test_zfill(self
):
283 self
.checkequal('123', '123', 'zfill', 2)
284 self
.checkequal('123', '123', 'zfill', 3)
285 self
.checkequal('0123', '123', 'zfill', 4)
286 self
.checkequal('+123', '+123', 'zfill', 3)
287 self
.checkequal('+123', '+123', 'zfill', 4)
288 self
.checkequal('+0123', '+123', 'zfill', 5)
289 self
.checkequal('-123', '-123', 'zfill', 3)
290 self
.checkequal('-123', '-123', 'zfill', 4)
291 self
.checkequal('-0123', '-123', 'zfill', 5)
292 self
.checkequal('000', '', 'zfill', 3)
293 self
.checkequal('34', '34', 'zfill', 1)
294 self
.checkequal('0034', '34', 'zfill', 4)
296 self
.checkraises(TypeError, '123', 'zfill')
298 class MixinStrUnicodeUserStringTest
:
299 # additional tests that only work for
300 # stringlike objects, i.e. str, unicode, UserString
301 # (but not the string module)
303 def test_islower(self
):
304 self
.checkequal(False, '', 'islower')
305 self
.checkequal(True, 'a', 'islower')
306 self
.checkequal(False, 'A', 'islower')
307 self
.checkequal(False, '\n', 'islower')
308 self
.checkequal(True, 'abc', 'islower')
309 self
.checkequal(False, 'aBc', 'islower')
310 self
.checkequal(True, 'abc\n', 'islower')
311 self
.checkraises(TypeError, 'abc', 'islower', 42)
313 def test_isupper(self
):
314 self
.checkequal(False, '', 'isupper')
315 self
.checkequal(False, 'a', 'isupper')
316 self
.checkequal(True, 'A', 'isupper')
317 self
.checkequal(False, '\n', 'isupper')
318 self
.checkequal(True, 'ABC', 'isupper')
319 self
.checkequal(False, 'AbC', 'isupper')
320 self
.checkequal(True, 'ABC\n', 'isupper')
321 self
.checkraises(TypeError, 'abc', 'isupper', 42)
323 def test_istitle(self
):
324 self
.checkequal(False, '', 'istitle')
325 self
.checkequal(False, 'a', 'istitle')
326 self
.checkequal(True, 'A', 'istitle')
327 self
.checkequal(False, '\n', 'istitle')
328 self
.checkequal(True, 'A Titlecased Line', 'istitle')
329 self
.checkequal(True, 'A\nTitlecased Line', 'istitle')
330 self
.checkequal(True, 'A Titlecased, Line', 'istitle')
331 self
.checkequal(False, 'Not a capitalized String', 'istitle')
332 self
.checkequal(False, 'Not\ta Titlecase String', 'istitle')
333 self
.checkequal(False, 'Not--a Titlecase String', 'istitle')
334 self
.checkequal(False, 'NOT', 'istitle')
335 self
.checkraises(TypeError, 'abc', 'istitle', 42)
337 def test_isspace(self
):
338 self
.checkequal(False, '', 'isspace')
339 self
.checkequal(False, 'a', 'isspace')
340 self
.checkequal(True, ' ', 'isspace')
341 self
.checkequal(True, '\t', 'isspace')
342 self
.checkequal(True, '\r', 'isspace')
343 self
.checkequal(True, '\n', 'isspace')
344 self
.checkequal(True, ' \t\r\n', 'isspace')
345 self
.checkequal(False, ' \t\r\na', 'isspace')
346 self
.checkraises(TypeError, 'abc', 'isspace', 42)
348 def test_isalpha(self
):
349 self
.checkequal(False, '', 'isalpha')
350 self
.checkequal(True, 'a', 'isalpha')
351 self
.checkequal(True, 'A', 'isalpha')
352 self
.checkequal(False, '\n', 'isalpha')
353 self
.checkequal(True, 'abc', 'isalpha')
354 self
.checkequal(False, 'aBc123', 'isalpha')
355 self
.checkequal(False, 'abc\n', 'isalpha')
356 self
.checkraises(TypeError, 'abc', 'isalpha', 42)
358 def test_isalnum(self
):
359 self
.checkequal(False, '', 'isalnum')
360 self
.checkequal(True, 'a', 'isalnum')
361 self
.checkequal(True, 'A', 'isalnum')
362 self
.checkequal(False, '\n', 'isalnum')
363 self
.checkequal(True, '123abc456', 'isalnum')
364 self
.checkequal(True, 'a1b3c', 'isalnum')
365 self
.checkequal(False, 'aBc000 ', 'isalnum')
366 self
.checkequal(False, 'abc\n', 'isalnum')
367 self
.checkraises(TypeError, 'abc', 'isalnum', 42)
369 def test_isdigit(self
):
370 self
.checkequal(False, '', 'isdigit')
371 self
.checkequal(False, 'a', 'isdigit')
372 self
.checkequal(True, '0', 'isdigit')
373 self
.checkequal(True, '0123456789', 'isdigit')
374 self
.checkequal(False, '0123456789a', 'isdigit')
376 self
.checkraises(TypeError, 'abc', 'isdigit', 42)
378 def test_title(self
):
379 self
.checkequal(' Hello ', ' hello ', 'title')
380 self
.checkequal('Hello ', 'hello ', 'title')
381 self
.checkequal('Hello ', 'Hello ', 'title')
382 self
.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
383 self
.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
384 self
.checkequal('Getint', "getInt", 'title')
385 self
.checkraises(TypeError, 'hello', 'title', 42)
387 def test_splitlines(self
):
388 self
.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
389 self
.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
390 self
.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
391 self
.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
392 self
.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
393 self
.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
394 self
.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
396 self
.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
398 def test_startswith(self
):
399 self
.checkequal(True, 'hello', 'startswith', 'he')
400 self
.checkequal(True, 'hello', 'startswith', 'hello')
401 self
.checkequal(False, 'hello', 'startswith', 'hello world')
402 self
.checkequal(True, 'hello', 'startswith', '')
403 self
.checkequal(False, 'hello', 'startswith', 'ello')
404 self
.checkequal(True, 'hello', 'startswith', 'ello', 1)
405 self
.checkequal(True, 'hello', 'startswith', 'o', 4)
406 self
.checkequal(False, 'hello', 'startswith', 'o', 5)
407 self
.checkequal(True, 'hello', 'startswith', '', 5)
408 self
.checkequal(False, 'hello', 'startswith', 'lo', 6)
409 self
.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
410 self
.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
411 self
.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
413 # test negative indices
414 self
.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
415 self
.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
416 self
.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
417 self
.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
418 self
.checkequal(False, 'hello', 'startswith', 'ello', -5)
419 self
.checkequal(True, 'hello', 'startswith', 'ello', -4)
420 self
.checkequal(False, 'hello', 'startswith', 'o', -2)
421 self
.checkequal(True, 'hello', 'startswith', 'o', -1)
422 self
.checkequal(True, 'hello', 'startswith', '', -3, -3)
423 self
.checkequal(False, 'hello', 'startswith', 'lo', -9)
425 self
.checkraises(TypeError, 'hello', 'startswith')
426 self
.checkraises(TypeError, 'hello', 'startswith', 42)
428 def test_endswith(self
):
429 self
.checkequal(True, 'hello', 'endswith', 'lo')
430 self
.checkequal(False, 'hello', 'endswith', 'he')
431 self
.checkequal(True, 'hello', 'endswith', '')
432 self
.checkequal(False, 'hello', 'endswith', 'hello world')
433 self
.checkequal(False, 'helloworld', 'endswith', 'worl')
434 self
.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
435 self
.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
436 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
437 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
438 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
439 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
440 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
441 self
.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
442 self
.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
444 # test negative indices
445 self
.checkequal(True, 'hello', 'endswith', 'lo', -2)
446 self
.checkequal(False, 'hello', 'endswith', 'he', -2)
447 self
.checkequal(True, 'hello', 'endswith', '', -3, -3)
448 self
.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
449 self
.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
450 self
.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
451 self
.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
452 self
.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
453 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
454 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
455 self
.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
456 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
457 self
.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
459 self
.checkraises(TypeError, 'hello', 'endswith')
460 self
.checkraises(TypeError, 'hello', 'endswith', 42)
462 def test___contains__(self
):
463 self
.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
464 self
.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
465 self
.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
466 self
.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
467 self
.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
468 self
.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
469 self
.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
470 self
.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
471 self
.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
473 def test_subscript(self
):
474 self
.checkequal(u
'a', 'abc', '__getitem__', 0)
475 self
.checkequal(u
'c', 'abc', '__getitem__', -1)
476 self
.checkequal(u
'a', 'abc', '__getitem__', 0L)
477 self
.checkequal(u
'abc', 'abc', '__getitem__', slice(0, 3))
478 self
.checkequal(u
'abc', 'abc', '__getitem__', slice(0, 1000))
479 self
.checkequal(u
'a', 'abc', '__getitem__', slice(0, 1))
480 self
.checkequal(u
'', 'abc', '__getitem__', slice(0, 0))
481 # FIXME What about negative indizes? This is handled differently by [] and __getitem__(slice)
483 self
.checkraises(TypeError, 'abc', '__getitem__', 'def')
485 def test_slice(self
):
486 self
.checkequal('abc', 'abc', '__getslice__', 0, 1000)
487 self
.checkequal('abc', 'abc', '__getslice__', 0, 3)
488 self
.checkequal('ab', 'abc', '__getslice__', 0, 2)
489 self
.checkequal('bc', 'abc', '__getslice__', 1, 3)
490 self
.checkequal('b', 'abc', '__getslice__', 1, 2)
491 self
.checkequal('', 'abc', '__getslice__', 2, 2)
492 self
.checkequal('', 'abc', '__getslice__', 1000, 1000)
493 self
.checkequal('', 'abc', '__getslice__', 2000, 1000)
494 self
.checkequal('', 'abc', '__getslice__', 2, 1)
495 # FIXME What about negative indizes? This is handled differently by [] and __getslice__
497 self
.checkraises(TypeError, 'abc', '__getslice__', 'def')
500 self
.checkequal('', 'abc', '__mul__', -1)
501 self
.checkequal('', 'abc', '__mul__', 0)
502 self
.checkequal('abc', 'abc', '__mul__', 1)
503 self
.checkequal('abcabcabc', 'abc', '__mul__', 3)
504 self
.checkraises(TypeError, 'abc', '__mul__')
505 self
.checkraises(TypeError, 'abc', '__mul__', '')
506 self
.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
509 # join now works with any sequence type
510 # moved here, because the argument order is
511 # different in string.join (see the test in
512 # test.test_string.StringTest.test_join)
513 self
.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
514 self
.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
515 self
.checkequal('w x y z', ' ', 'join', Sequence())
516 self
.checkequal('abc', 'a', 'join', ('abc',))
517 self
.checkequal('z', 'a', 'join', UserList(['z']))
518 if test_support
.have_unicode
:
519 self
.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
520 self
.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
521 self
.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
522 self
.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
523 self
.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
524 for i
in [5, 25, 125]:
525 self
.checkequal(((('a' * i
) + '-') * i
)[:-1], '-', 'join',
527 self
.checkequal(((('a' * i
) + '-') * i
)[:-1], '-', 'join',
530 self
.checkraises(TypeError, ' ', 'join', BadSeq1())
531 self
.checkequal('a b c', ' ', 'join', BadSeq2())
533 self
.checkraises(TypeError, ' ', 'join')
534 self
.checkraises(TypeError, ' ', 'join', 7)
535 self
.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
537 def test_formatting(self
):
538 self
.checkequal('+hello+', '+%s+', '__mod__', 'hello')
539 self
.checkequal('+10+', '+%d+', '__mod__', 10)
540 self
.checkequal('a', "%c", '__mod__', "a")
541 self
.checkequal('a', "%c", '__mod__', "a")
542 self
.checkequal('"', "%c", '__mod__', 34)
543 self
.checkequal('$', "%c", '__mod__', 36)
544 self
.checkequal('10', "%d", '__mod__', 10)
545 self
.checkequal('\x7f', "%c", '__mod__', 0x7f)
547 for ordinal
in (-100, 0x200000):
548 # unicode raises ValueError, str raises OverflowError
549 self
.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal
)
551 self
.checkequal(' 42', '%3ld', '__mod__', 42)
552 self
.checkequal('0042.00', '%07.2f', '__mod__', 42)
553 self
.checkequal('0042.00', '%07.2F', '__mod__', 42)
555 self
.checkraises(TypeError, 'abc', '__mod__')
556 self
.checkraises(TypeError, '%(foo)s', '__mod__', 42)
557 self
.checkraises(TypeError, '%s%s', '__mod__', (42,))
558 self
.checkraises(TypeError, '%c', '__mod__', (None,))
559 self
.checkraises(ValueError, '%(foo', '__mod__', {})
560 self
.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
562 # argument names with properly nested brackets are supported
563 self
.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
565 # 100 is a magic number in PyUnicode_Format, this forces a resize
566 self
.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
568 self
.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
569 self
.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
570 self
.checkraises(ValueError, '%10', '__mod__', (42,))
572 def test_floatformatting(self
):
574 for prec
in xrange(100):
575 format
= '%%.%if' % prec
578 value
= value
* 3.141592655 / 3.0 * 10.0
579 # The formatfloat() code in stringobject.c and
580 # unicodeobject.c uses a 120 byte buffer and switches from
581 # 'f' formatting to 'g' at precision 50, so we expect
582 # OverflowErrors for the ranges x < 50 and prec >= 67.
583 if x
< 50 and prec
>= 67:
584 self
.checkraises(OverflowError, format
, "__mod__", value
)
586 self
.checkcall(format
, "__mod__", value
)
588 class MixinStrStringUserStringTest
:
589 # Additional tests for 8bit strings, i.e. str, UserString and
592 def test_maketrans(self
):
594 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
595 string
.maketrans('abc', 'xyz')
597 self
.assertRaises(ValueError, string
.maketrans
, 'abc', 'xyzw')
599 def test_translate(self
):
600 table
= string
.maketrans('abc', 'xyz')
601 self
.checkequal('xyzxyz', 'xyzabcdef', 'translate', table
, 'def')
603 table
= string
.maketrans('a', 'A')
604 self
.checkequal('Abc', 'abc', 'translate', table
)
605 self
.checkequal('xyz', 'xyz', 'translate', table
)
606 self
.checkequal('yz', 'xyz', 'translate', table
, 'x')
607 self
.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
608 self
.checkraises(ValueError, 'xyz', 'translate', 'too short')
611 class MixinStrUserStringTest
:
612 # Additional tests that only work with
613 # 8bit compatible object, i.e. str and UserString
615 def test_encoding_decoding(self
):
616 codecs
= [('rot13', 'uryyb jbeyq'),
617 ('base64', 'aGVsbG8gd29ybGQ=\n'),
618 ('hex', '68656c6c6f20776f726c64'),
619 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
620 for encoding
, data
in codecs
:
621 self
.checkequal(data
, 'hello world', 'encode', encoding
)
622 self
.checkequal('hello world', data
, 'decode', encoding
)
623 # zlib is optional, so we make the test optional too...
629 data
= 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
630 self
.checkequal(data
, 'hello world', 'encode', 'zlib')
631 self
.checkequal('hello world', data
, 'decode', 'zlib')
633 self
.checkraises(TypeError, 'xyz', 'decode', 42)
634 self
.checkraises(TypeError, 'xyz', 'encode', 42)