0001 """ 0002 Common tests shared by test_str, test_unicode, test_userstring and test_string. 0003 """ 0004 0005 import unittest, string, sys 0006 from test import test_support 0007 from UserList import UserList 0008 0009 class Sequence: 0010 def __init__(self, seq='wxyz'): self.seq = seq 0011 def __len__(self): return len(self.seq) 0012 def __getitem__(self, i): return self.seq[i] 0013 0014 class BadSeq1(Sequence): 0015 def __init__(self): self.seq = [7, 'hello', 123L] 0016 0017 class BadSeq2(Sequence): 0018 def __init__(self): self.seq = ['a', 'b', 'c'] 0019 def __len__(self): return 8 0020 0021 class CommonTest(unittest.TestCase): 0022 # This testcase contains test that can be used in all 0023 # stringlike classes. Currently this is str, unicode 0024 # UserString and the string module. 0025 0026 # The type to be tested 0027 # Change in subclasses to change the behaviour of fixtesttype() 0028 type2test = None 0029 0030 # All tests pass their arguments to the testing methods 0031 # as str objects. fixtesttype() can be used to propagate 0032 # these arguments to the appropriate type 0033 def fixtype(self, obj): 0034 if isinstance(obj, str): 0035 return self.__class__.type2test(obj) 0036 elif isinstance(obj, list): 0037 return [self.fixtype(x) for x in obj] 0038 elif isinstance(obj, tuple): 0039 return tuple([self.fixtype(x) for x in obj]) 0040 elif isinstance(obj, dict): 0041 return dict([ 0042 (self.fixtype(key), self.fixtype(value)) 0043 for (key, value) in obj.iteritems() 0044 ]) 0045 else: 0046 return obj 0047 0048 # check that object.method(*args) returns result 0049 def checkequal(self, result, object, methodname, *args): 0050 result = self.fixtype(result) 0051 object = self.fixtype(object) 0052 args = self.fixtype(args) 0053 realresult = getattr(object, methodname)(*args) 0054 self.assertEqual( 0055 result, 0056 realresult 0057 ) 0058 # if the original is returned make sure that 0059 # this doesn't happen with subclasses 0060 if object == realresult: 0061 class subtype(self.__class__.type2test): 0062 pass 0063 object = subtype(object) 0064 realresult = getattr(object, methodname)(*args) 0065 self.assert_(object is not realresult) 0066 0067 # check that object.method(*args) raises exc 0068 def checkraises(self, exc, object, methodname, *args): 0069 object = self.fixtype(object) 0070 args = self.fixtype(args) 0071 self.assertRaises( 0072 exc, 0073 getattr(object, methodname), 0074 *args 0075 ) 0076 0077 # call object.method(*args) without any checks 0078 def checkcall(self, object, methodname, *args): 0079 object = self.fixtype(object) 0080 args = self.fixtype(args) 0081 getattr(object, methodname)(*args) 0082 0083 def test_hash(self): 0084 # SF bug 1054139: += optimization was not invalidating cached hash value 0085 a = self.type2test('DNSSEC') 0086 b = self.type2test('') 0087 for c in a: 0088 b += c 0089 hash(b) 0090 self.assertEqual(hash(a), hash(b)) 0091 0092 def test_capitalize(self): 0093 self.checkequal(' hello ', ' hello ', 'capitalize') 0094 self.checkequal('Hello ', 'Hello ','capitalize') 0095 self.checkequal('Hello ', 'hello ','capitalize') 0096 self.checkequal('Aaaa', 'aaaa', 'capitalize') 0097 self.checkequal('Aaaa', 'AaAa', 'capitalize') 0098 0099 self.checkraises(TypeError, 'hello', 'capitalize', 42) 0100 0101 def test_count(self): 0102 self.checkequal(3, 'aaa', 'count', 'a') 0103 self.checkequal(0, 'aaa', 'count', 'b') 0104 self.checkequal(3, 'aaa', 'count', 'a') 0105 self.checkequal(0, 'aaa', 'count', 'b') 0106 self.checkequal(3, 'aaa', 'count', 'a') 0107 self.checkequal(0, 'aaa', 'count', 'b') 0108 self.checkequal(0, 'aaa', 'count', 'b') 0109 self.checkequal(1, 'aaa', 'count', 'a', -1) 0110 self.checkequal(3, 'aaa', 'count', 'a', -10) 0111 self.checkequal(2, 'aaa', 'count', 'a', 0, -1) 0112 self.checkequal(0, 'aaa', 'count', 'a', 0, -10) 0113 0114 self.checkraises(TypeError, 'hello', 'count') 0115 self.checkraises(TypeError, 'hello', 'count', 42) 0116 0117 def test_find(self): 0118 self.checkequal(0, 'abcdefghiabc', 'find', 'abc') 0119 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1) 0120 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4) 0121 0122 self.checkraises(TypeError, 'hello', 'find') 0123 self.checkraises(TypeError, 'hello', 'find', 42) 0124 0125 def test_rfind(self): 0126 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc') 0127 self.checkequal(12, 'abcdefghiabc', 'rfind', '') 0128 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd') 0129 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz') 0130 0131 self.checkraises(TypeError, 'hello', 'rfind') 0132 self.checkraises(TypeError, 'hello', 'rfind', 42) 0133 0134 def test_index(self): 0135 self.checkequal(0, 'abcdefghiabc', 'index', '') 0136 self.checkequal(3, 'abcdefghiabc', 'index', 'def') 0137 self.checkequal(0, 'abcdefghiabc', 'index', 'abc') 0138 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1) 0139 0140 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib') 0141 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1) 0142 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8) 0143 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1) 0144 0145 self.checkraises(TypeError, 'hello', 'index') 0146 self.checkraises(TypeError, 'hello', 'index', 42) 0147 0148 def test_rindex(self): 0149 self.checkequal(12, 'abcdefghiabc', 'rindex', '') 0150 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def') 0151 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc') 0152 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) 0153 0154 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib') 0155 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1) 0156 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1) 0157 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8) 0158 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1) 0159 0160 self.checkraises(TypeError, 'hello', 'rindex') 0161 self.checkraises(TypeError, 'hello', 'rindex', 42) 0162 0163 def test_lower(self): 0164 self.checkequal('hello', 'HeLLo', 'lower') 0165 self.checkequal('hello', 'hello', 'lower') 0166 self.checkraises(TypeError, 'hello', 'lower', 42) 0167 0168 def test_upper(self): 0169 self.checkequal('HELLO', 'HeLLo', 'upper') 0170 self.checkequal('HELLO', 'HELLO', 'upper') 0171 self.checkraises(TypeError, 'hello', 'upper', 42) 0172 0173 def test_expandtabs(self): 0174 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs') 0175 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) 0176 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4) 0177 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4) 0178 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs') 0179 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) 0180 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4) 0181 0182 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42) 0183 0184 def test_split(self): 0185 self.checkequal(['this', 'is', 'the', 'split', 'function'], 0186 'this is the split function', 'split') 0187 0188 # by whitespace 0189 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split') 0190 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1) 0191 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2) 0192 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3) 0193 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4) 0194 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0) 0195 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2) 0196 0197 # by a char 0198 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|') 0199 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1) 0200 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2) 0201 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3) 0202 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4) 0203 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0) 0204 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2) 0205 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|') 0206 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2) 0207 0208 # by string 0209 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') 0210 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1) 0211 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2) 0212 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3) 0213 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4) 0214 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0) 0215 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2) 0216 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test') 0217 0218 # mixed use of str and unicode 0219 self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2) 0220 0221 # argument type 0222 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42) 0223 0224 def test_rsplit(self): 0225 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'], 0226 'this is the rsplit function', 'rsplit') 0227 0228 # by whitespace 0229 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit') 0230 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1) 0231 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2) 0232 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3) 0233 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4) 0234 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0) 0235 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2) 0236 0237 # by a char 0238 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|') 0239 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1) 0240 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2) 0241 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3) 0242 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4) 0243 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0) 0244 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2) 0245 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|') 0246 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2) 0247 0248 # by string 0249 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//') 0250 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1) 0251 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2) 0252 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3) 0253 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4) 0254 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0) 0255 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2) 0256 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test') 0257 0258 # mixed use of str and unicode 0259 self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2) 0260 0261 # argument type 0262 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42) 0263 0264 def test_strip(self): 0265 self.checkequal('hello', ' hello ', 'strip') 0266 self.checkequal('hello ', ' hello ', 'lstrip') 0267 self.checkequal(' hello', ' hello ', 'rstrip') 0268 self.checkequal('hello', 'hello', 'strip') 0269 0270 # strip/lstrip/rstrip with None arg 0271 self.checkequal('hello', ' hello ', 'strip', None) 0272 self.checkequal('hello ', ' hello ', 'lstrip', None) 0273 self.checkequal(' hello', ' hello ', 'rstrip', None) 0274 self.checkequal('hello', 'hello', 'strip', None) 0275 0276 # strip/lstrip/rstrip with str arg 0277 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz') 0278 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz') 0279 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz') 0280 self.checkequal('hello', 'hello', 'strip', 'xyz') 0281 0282 # strip/lstrip/rstrip with unicode arg 0283 if test_support.have_unicode: 0284 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy', 0285 'strip', unicode('xyz', 'ascii')) 0286 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy', 0287 'lstrip', unicode('xyz', 'ascii')) 0288 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy', 0289 'rstrip', unicode('xyz', 'ascii')) 0290 self.checkequal(unicode('hello', 'ascii'), 'hello', 0291 'strip', unicode('xyz', 'ascii')) 0292 0293 self.checkraises(TypeError, 'hello', 'strip', 42, 42) 0294 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42) 0295 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42) 0296 0297 def test_ljust(self): 0298 self.checkequal('abc ', 'abc', 'ljust', 10) 0299 self.checkequal('abc ', 'abc', 'ljust', 6) 0300 self.checkequal('abc', 'abc', 'ljust', 3) 0301 self.checkequal('abc', 'abc', 'ljust', 2) 0302 self.checkequal('abc*******', 'abc', 'ljust', 10, '*') 0303 self.checkraises(TypeError, 'abc', 'ljust') 0304 0305 def test_rjust(self): 0306 self.checkequal(' abc', 'abc', 'rjust', 10) 0307 self.checkequal(' abc', 'abc', 'rjust', 6) 0308 self.checkequal('abc', 'abc', 'rjust', 3) 0309 self.checkequal('abc', 'abc', 'rjust', 2) 0310 self.checkequal('*******abc', 'abc', 'rjust', 10, '*') 0311 self.checkraises(TypeError, 'abc', 'rjust') 0312 0313 def test_center(self): 0314 self.checkequal(' abc ', 'abc', 'center', 10) 0315 self.checkequal(' abc ', 'abc', 'center', 6) 0316 self.checkequal('abc', 'abc', 'center', 3) 0317 self.checkequal('abc', 'abc', 'center', 2) 0318 self.checkequal('***abc****', 'abc', 'center', 10, '*') 0319 self.checkraises(TypeError, 'abc', 'center') 0320 0321 def test_swapcase(self): 0322 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase') 0323 0324 self.checkraises(TypeError, 'hello', 'swapcase', 42) 0325 0326 def test_replace(self): 0327 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) 0328 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '') 0329 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2) 0330 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3) 0331 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4) 0332 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0) 0333 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@') 0334 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@') 0335 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2) 0336 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-') 0337 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3) 0338 self.checkequal('abc', 'abc', 'replace', '', '-', 0) 0339 self.checkequal('', '', 'replace', '', '') 0340 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0) 0341 self.checkequal('abc', 'abc', 'replace', 'xy', '--') 0342 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with 0343 # MemoryError due to empty result (platform malloc issue when requesting 0344 # 0 bytes). 0345 self.checkequal('', '123', 'replace', '123', '') 0346 self.checkequal('', '123123', 'replace', '123', '') 0347 self.checkequal('x', '123x123', 'replace', '123', '') 0348 0349 self.checkraises(TypeError, 'hello', 'replace') 0350 self.checkraises(TypeError, 'hello', 'replace', 42) 0351 self.checkraises(TypeError, 'hello', 'replace', 42, 'h') 0352 self.checkraises(TypeError, 'hello', 'replace', 'h', 42) 0353 0354 def test_zfill(self): 0355 self.checkequal('123', '123', 'zfill', 2) 0356 self.checkequal('123', '123', 'zfill', 3) 0357 self.checkequal('0123', '123', 'zfill', 4) 0358 self.checkequal('+123', '+123', 'zfill', 3) 0359 self.checkequal('+123', '+123', 'zfill', 4) 0360 self.checkequal('+0123', '+123', 'zfill', 5) 0361 self.checkequal('-123', '-123', 'zfill', 3) 0362 self.checkequal('-123', '-123', 'zfill', 4) 0363 self.checkequal('-0123', '-123', 'zfill', 5) 0364 self.checkequal('000', '', 'zfill', 3) 0365 self.checkequal('34', '34', 'zfill', 1) 0366 self.checkequal('0034', '34', 'zfill', 4) 0367 0368 self.checkraises(TypeError, '123', 'zfill') 0369 0370 class MixinStrUnicodeUserStringTest: 0371 # additional tests that only work for 0372 # stringlike objects, i.e. str, unicode, UserString 0373 # (but not the string module) 0374 0375 def test_islower(self): 0376 self.checkequal(False, '', 'islower') 0377 self.checkequal(True, 'a', 'islower') 0378 self.checkequal(False, 'A', 'islower') 0379 self.checkequal(False, '\n', 'islower') 0380 self.checkequal(True, 'abc', 'islower') 0381 self.checkequal(False, 'aBc', 'islower') 0382 self.checkequal(True, 'abc\n', 'islower') 0383 self.checkraises(TypeError, 'abc', 'islower', 42) 0384 0385 def test_isupper(self): 0386 self.checkequal(False, '', 'isupper') 0387 self.checkequal(False, 'a', 'isupper') 0388 self.checkequal(True, 'A', 'isupper') 0389 self.checkequal(False, '\n', 'isupper') 0390 self.checkequal(True, 'ABC', 'isupper') 0391 self.checkequal(False, 'AbC', 'isupper') 0392 self.checkequal(True, 'ABC\n', 'isupper') 0393 self.checkraises(TypeError, 'abc', 'isupper', 42) 0394 0395 def test_istitle(self): 0396 self.checkequal(False, '', 'istitle') 0397 self.checkequal(False, 'a', 'istitle') 0398 self.checkequal(True, 'A', 'istitle') 0399 self.checkequal(False, '\n', 'istitle') 0400 self.checkequal(True, 'A Titlecased Line', 'istitle') 0401 self.checkequal(True, 'A\nTitlecased Line', 'istitle') 0402 self.checkequal(True, 'A Titlecased, Line', 'istitle') 0403 self.checkequal(False, 'Not a capitalized String', 'istitle') 0404 self.checkequal(False, 'Not\ta Titlecase String', 'istitle') 0405 self.checkequal(False, 'Not--a Titlecase String', 'istitle') 0406 self.checkequal(False, 'NOT', 'istitle') 0407 self.checkraises(TypeError, 'abc', 'istitle', 42) 0408 0409 def test_isspace(self): 0410 self.checkequal(False, '', 'isspace') 0411 self.checkequal(False, 'a', 'isspace') 0412 self.checkequal(True, ' ', 'isspace') 0413 self.checkequal(True, '\t', 'isspace') 0414 self.checkequal(True, '\r', 'isspace') 0415 self.checkequal(True, '\n', 'isspace') 0416 self.checkequal(True, ' \t\r\n', 'isspace') 0417 self.checkequal(False, ' \t\r\na', 'isspace') 0418 self.checkraises(TypeError, 'abc', 'isspace', 42) 0419 0420 def test_isalpha(self): 0421 self.checkequal(False, '', 'isalpha') 0422 self.checkequal(True, 'a', 'isalpha') 0423 self.checkequal(True, 'A', 'isalpha') 0424 self.checkequal(False, '\n', 'isalpha') 0425 self.checkequal(True, 'abc', 'isalpha') 0426 self.checkequal(False, 'aBc123', 'isalpha') 0427 self.checkequal(False, 'abc\n', 'isalpha') 0428 self.checkraises(TypeError, 'abc', 'isalpha', 42) 0429 0430 def test_isalnum(self): 0431 self.checkequal(False, '', 'isalnum') 0432 self.checkequal(True, 'a', 'isalnum') 0433 self.checkequal(True, 'A', 'isalnum') 0434 self.checkequal(False, '\n', 'isalnum') 0435 self.checkequal(True, '123abc456', 'isalnum') 0436 self.checkequal(True, 'a1b3c', 'isalnum') 0437 self.checkequal(False, 'aBc000 ', 'isalnum') 0438 self.checkequal(False, 'abc\n', 'isalnum') 0439 self.checkraises(TypeError, 'abc', 'isalnum', 42) 0440 0441 def test_isdigit(self): 0442 self.checkequal(False, '', 'isdigit') 0443 self.checkequal(False, 'a', 'isdigit') 0444 self.checkequal(True, '0', 'isdigit') 0445 self.checkequal(True, '0123456789', 'isdigit') 0446 self.checkequal(False, '0123456789a', 'isdigit') 0447 0448 self.checkraises(TypeError, 'abc', 'isdigit', 42) 0449 0450 def test_title(self): 0451 self.checkequal(' Hello ', ' hello ', 'title') 0452 self.checkequal('Hello ', 'hello ', 'title') 0453 self.checkequal('Hello ', 'Hello ', 'title') 0454 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title') 0455 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', ) 0456 self.checkequal('Getint', "getInt", 'title') 0457 self.checkraises(TypeError, 'hello', 'title', 42) 0458 0459 def test_splitlines(self): 0460 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines') 0461 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines') 0462 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines') 0463 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines') 0464 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines') 0465 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines') 0466 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1) 0467 0468 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) 0469 0470 def test_startswith(self): 0471 self.checkequal(True, 'hello', 'startswith', 'he') 0472 self.checkequal(True, 'hello', 'startswith', 'hello') 0473 self.checkequal(False, 'hello', 'startswith', 'hello world') 0474 self.checkequal(True, 'hello', 'startswith', '') 0475 self.checkequal(False, 'hello', 'startswith', 'ello') 0476 self.checkequal(True, 'hello', 'startswith', 'ello', 1) 0477 self.checkequal(True, 'hello', 'startswith', 'o', 4) 0478 self.checkequal(False, 'hello', 'startswith', 'o', 5) 0479 self.checkequal(True, 'hello', 'startswith', '', 5) 0480 self.checkequal(False, 'hello', 'startswith', 'lo', 6) 0481 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3) 0482 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7) 0483 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6) 0484 0485 # test negative indices 0486 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1) 0487 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1) 0488 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1) 0489 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10) 0490 self.checkequal(False, 'hello', 'startswith', 'ello', -5) 0491 self.checkequal(True, 'hello', 'startswith', 'ello', -4) 0492 self.checkequal(False, 'hello', 'startswith', 'o', -2) 0493 self.checkequal(True, 'hello', 'startswith', 'o', -1) 0494 self.checkequal(True, 'hello', 'startswith', '', -3, -3) 0495 self.checkequal(False, 'hello', 'startswith', 'lo', -9) 0496 0497 self.checkraises(TypeError, 'hello', 'startswith') 0498 self.checkraises(TypeError, 'hello', 'startswith', 42) 0499 0500 def test_endswith(self): 0501 self.checkequal(True, 'hello', 'endswith', 'lo') 0502 self.checkequal(False, 'hello', 'endswith', 'he') 0503 self.checkequal(True, 'hello', 'endswith', '') 0504 self.checkequal(False, 'hello', 'endswith', 'hello world') 0505 self.checkequal(False, 'helloworld', 'endswith', 'worl') 0506 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9) 0507 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12) 0508 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7) 0509 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7) 0510 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7) 0511 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7) 0512 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8) 0513 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1) 0514 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0) 0515 0516 # test negative indices 0517 self.checkequal(True, 'hello', 'endswith', 'lo', -2) 0518 self.checkequal(False, 'hello', 'endswith', 'he', -2) 0519 self.checkequal(True, 'hello', 'endswith', '', -3, -3) 0520 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2) 0521 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6) 0522 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1) 0523 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9) 0524 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12) 0525 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3) 0526 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3) 0527 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3) 0528 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4) 0529 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2) 0530 0531 self.checkraises(TypeError, 'hello', 'endswith') 0532 self.checkraises(TypeError, 'hello', 'endswith', 42) 0533 0534 def test___contains__(self): 0535 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True) 0536 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True) 0537 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False) 0538 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True) 0539 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True) 0540 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True) 0541 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True) 0542 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False) 0543 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False) 0544 0545 def test_subscript(self): 0546 self.checkequal(u'a', 'abc', '__getitem__', 0) 0547 self.checkequal(u'c', 'abc', '__getitem__', -1) 0548 self.checkequal(u'a', 'abc', '__getitem__', 0L) 0549 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3)) 0550 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000)) 0551 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1)) 0552 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0)) 0553 # FIXME What about negative indizes? This is handled differently by [] and __getitem__(slice) 0554 0555 self.checkraises(TypeError, 'abc', '__getitem__', 'def') 0556 0557 def test_slice(self): 0558 self.checkequal('abc', 'abc', '__getslice__', 0, 1000) 0559 self.checkequal('abc', 'abc', '__getslice__', 0, 3) 0560 self.checkequal('ab', 'abc', '__getslice__', 0, 2) 0561 self.checkequal('bc', 'abc', '__getslice__', 1, 3) 0562 self.checkequal('b', 'abc', '__getslice__', 1, 2) 0563 self.checkequal('', 'abc', '__getslice__', 2, 2) 0564 self.checkequal('', 'abc', '__getslice__', 1000, 1000) 0565 self.checkequal('', 'abc', '__getslice__', 2000, 1000) 0566 self.checkequal('', 'abc', '__getslice__', 2, 1) 0567 # FIXME What about negative indizes? This is handled differently by [] and __getslice__ 0568 0569 self.checkraises(TypeError, 'abc', '__getslice__', 'def') 0570 0571 def test_mul(self): 0572 self.checkequal('', 'abc', '__mul__', -1) 0573 self.checkequal('', 'abc', '__mul__', 0) 0574 self.checkequal('abc', 'abc', '__mul__', 1) 0575 self.checkequal('abcabcabc', 'abc', '__mul__', 3) 0576 self.checkraises(TypeError, 'abc', '__mul__') 0577 self.checkraises(TypeError, 'abc', '__mul__', '') 0578 self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000) 0579 0580 def test_join(self): 0581 # join now works with any sequence type 0582 # moved here, because the argument order is 0583 # different in string.join (see the test in 0584 # test.test_string.StringTest.test_join) 0585 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) 0586 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd')) 0587 self.checkequal('w x y z', ' ', 'join', Sequence()) 0588 self.checkequal('abc', 'a', 'join', ('abc',)) 0589 self.checkequal('z', 'a', 'join', UserList(['z'])) 0590 if test_support.have_unicode: 0591 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c']) 0592 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c']) 0593 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c']) 0594 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')]) 0595 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3]) 0596 for i in [5, 25, 125]: 0597 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', 0598 ['a' * i] * i) 0599 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', 0600 ('a' * i,) * i) 0601 0602 self.checkraises(TypeError, ' ', 'join', BadSeq1()) 0603 self.checkequal('a b c', ' ', 'join', BadSeq2()) 0604 0605 self.checkraises(TypeError, ' ', 'join') 0606 self.checkraises(TypeError, ' ', 'join', 7) 0607 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L])) 0608 0609 def test_formatting(self): 0610 self.checkequal('+hello+', '+%s+', '__mod__', 'hello') 0611 self.checkequal('+10+', '+%d+', '__mod__', 10) 0612 self.checkequal('a', "%c", '__mod__', "a") 0613 self.checkequal('a', "%c", '__mod__', "a") 0614 self.checkequal('"', "%c", '__mod__', 34) 0615 self.checkequal('$', "%c", '__mod__', 36) 0616 self.checkequal('10', "%d", '__mod__', 10) 0617 self.checkequal('\x7f', "%c", '__mod__', 0x7f) 0618 0619 for ordinal in (-100, 0x200000): 0620 # unicode raises ValueError, str raises OverflowError 0621 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal) 0622 0623 self.checkequal(' 42', '%3ld', '__mod__', 42) 0624 self.checkequal('0042.00', '%07.2f', '__mod__', 42) 0625 self.checkequal('0042.00', '%07.2F', '__mod__', 42) 0626 0627 self.checkraises(TypeError, 'abc', '__mod__') 0628 self.checkraises(TypeError, '%(foo)s', '__mod__', 42) 0629 self.checkraises(TypeError, '%s%s', '__mod__', (42,)) 0630 self.checkraises(TypeError, '%c', '__mod__', (None,)) 0631 self.checkraises(ValueError, '%(foo', '__mod__', {}) 0632 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42)) 0633 0634 # argument names with properly nested brackets are supported 0635 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'}) 0636 0637 # 100 is a magic number in PyUnicode_Format, this forces a resize 0638 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a') 0639 0640 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar')) 0641 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.)) 0642 self.checkraises(ValueError, '%10', '__mod__', (42,)) 0643 0644 def test_floatformatting(self): 0645 # float formatting 0646 for prec in xrange(100): 0647 format = '%%.%if' % prec 0648 value = 0.01 0649 for x in xrange(60): 0650 value = value * 3.141592655 / 3.0 * 10.0 0651 # The formatfloat() code in stringobject.c and 0652 # unicodeobject.c uses a 120 byte buffer and switches from 0653 # 'f' formatting to 'g' at precision 50, so we expect 0654 # OverflowErrors for the ranges x < 50 and prec >= 67. 0655 if x < 50 and prec >= 67: 0656 self.checkraises(OverflowError, format, "__mod__", value) 0657 else: 0658 self.checkcall(format, "__mod__", value) 0659 0660 0661 class MixinStrStringUserStringTest: 0662 # Additional tests for 8bit strings, i.e. str, UserString and 0663 # the string module 0664 0665 def test_maketrans(self): 0666 self.assertEqual( 0667 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'), 0668 string.maketrans('abc', 'xyz') 0669 ) 0670 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw') 0671 0672 def test_translate(self): 0673 table = string.maketrans('abc', 'xyz') 0674 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def') 0675 0676 table = string.maketrans('a', 'A') 0677 self.checkequal('Abc', 'abc', 'translate', table) 0678 self.checkequal('xyz', 'xyz', 'translate', table) 0679 self.checkequal('yz', 'xyz', 'translate', table, 'x') 0680 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip') 0681 self.checkraises(ValueError, 'xyz', 'translate', 'too short') 0682 0683 0684 class MixinStrUserStringTest: 0685 # Additional tests that only work with 0686 # 8bit compatible object, i.e. str and UserString 0687 0688 def test_encoding_decoding(self): 0689 codecs = [('rot13', 'uryyb jbeyq'), 0690 ('base64', 'aGVsbG8gd29ybGQ=\n'), 0691 ('hex', '68656c6c6f20776f726c64'), 0692 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')] 0693 for encoding, data in codecs: 0694 self.checkequal(data, 'hello world', 'encode', encoding) 0695 self.checkequal('hello world', data, 'decode', encoding) 0696 # zlib is optional, so we make the test optional too... 0697 try: 0698 import zlib 0699 except ImportError: 0700 pass 0701 else: 0702 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]' 0703 self.checkequal(data, 'hello world', 'encode', 'zlib') 0704 self.checkequal('hello world', data, 'decode', 'zlib') 0705 0706 self.checkraises(TypeError, 'xyz', 'decode', 42) 0707 self.checkraises(TypeError, 'xyz', 'encode', 42) 0708 0709 0710 class MixinStrUnicodeTest: 0711 # Additional tests that only work with str and unicode. 0712 0713 def test_bug1001011(self): 0714 # Make sure join returns a NEW object for single item sequences 0715 # involving a subclass. 0716 # Make sure that it is of the appropriate type. 0717 # Check the optimisation still occurs for standard objects. 0718 t = self.type2test 0719 class subclass(t): 0720 pass 0721 s1 = subclass("abcd") 0722 s2 = t().join([s1]) 0723 self.assert_(s1 is not s2) 0724 self.assert_(type(s2) is t) 0725 0726 s1 = t("abcd") 0727 s2 = t().join([s1]) 0728 self.assert_(s1 is s2) 0729 0730 # Should also test mixed-type join. 0731 if t is unicode: 0732 s1 = subclass("abcd") 0733 s2 = "".join([s1]) 0734 self.assert_(s1 is not s2) 0735 self.assert_(type(s2) is t) 0736 0737 s1 = t("abcd") 0738 s2 = "".join([s1]) 0739 self.assert_(s1 is s2) 0740 0741 elif t is str: 0742 s1 = subclass("abcd") 0743 s2 = u"".join([s1]) 0744 self.assert_(s1 is not s2) 0745 self.assert_(type(s2) is unicode) # promotes! 0746 0747 s1 = t("abcd") 0748 s2 = u"".join([s1]) 0749 self.assert_(s1 is not s2) 0750 self.assert_(type(s2) is unicode) # promotes! 0751 0752 else: 0753 self.fail("unexpected type for MixinStrUnicodeTest %r" % t) 0754
Generated by PyXR 0.9.4