Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1 | """ |
| 2 | Common tests shared by test_str, test_unicode, test_userstring and test_string. |
| 3 | """ |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 4 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 5 | import unittest, string, sys |
| 6 | from test import test_support |
Jeremy Hylton | 20f41b6 | 2000-07-11 03:31:55 +0000 | [diff] [blame] | 7 | from UserList import UserList |
| 8 | |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 9 | class Sequence: |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 10 | def __init__(self, seq='wxyz'): self.seq = seq |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 11 | def __len__(self): return len(self.seq) |
| 12 | def __getitem__(self, i): return self.seq[i] |
| 13 | |
| 14 | class BadSeq1(Sequence): |
| 15 | def __init__(self): self.seq = [7, 'hello', 123L] |
| 16 | |
| 17 | class BadSeq2(Sequence): |
| 18 | def __init__(self): self.seq = ['a', 'b', 'c'] |
| 19 | def __len__(self): return 8 |
| 20 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 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. |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 25 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 26 | # The type to be tested |
| 27 | # Change in subclasses to change the behaviour of fixtesttype() |
| 28 | type2test = None |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 29 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 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): |
| 41 | return dict([ |
| 42 | (self.fixtype(key), self.fixtype(value)) |
| 43 | for (key, value) in obj.iteritems() |
| 44 | ]) |
| 45 | else: |
| 46 | return obj |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 47 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 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) |
| 54 | self.assertEqual( |
| 55 | result, |
| 56 | realresult |
| 57 | ) |
| 58 | # if the original is returned make sure that |
| 59 | # this doesn't happen with subclasses |
| 60 | if object == realresult: |
| 61 | class subtype(self.__class__.type2test): |
| 62 | pass |
| 63 | object = subtype(object) |
| 64 | realresult = getattr(object, methodname)(*args) |
| 65 | self.assert_(object is not realresult) |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 66 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 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) |
| 71 | self.assertRaises( |
| 72 | exc, |
| 73 | getattr(object, methodname), |
| 74 | *args |
| 75 | ) |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 76 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 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) |
| 82 | |
| 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') |
| 89 | |
| 90 | self.checkraises(TypeError, 'hello', 'capitalize', 42) |
| 91 | |
| 92 | def test_count(self): |
| 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) |
| 104 | |
| 105 | self.checkraises(TypeError, 'hello', 'count') |
| 106 | self.checkraises(TypeError, 'hello', 'count', 42) |
| 107 | |
| 108 | def test_find(self): |
| 109 | self.checkequal(0, 'abcdefghiabc', 'find', 'abc') |
| 110 | self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1) |
| 111 | self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4) |
| 112 | |
| 113 | self.checkraises(TypeError, 'hello', 'find') |
| 114 | self.checkraises(TypeError, 'hello', 'find', 42) |
| 115 | |
| 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') |
| 121 | |
| 122 | self.checkraises(TypeError, 'hello', 'rfind') |
| 123 | self.checkraises(TypeError, 'hello', 'rfind', 42) |
| 124 | |
| 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) |
| 130 | |
| 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) |
| 135 | |
| 136 | self.checkraises(TypeError, 'hello', 'index') |
| 137 | self.checkraises(TypeError, 'hello', 'index', 42) |
| 138 | |
| 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) |
| 144 | |
| 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) |
| 150 | |
| 151 | self.checkraises(TypeError, 'hello', 'rindex') |
| 152 | self.checkraises(TypeError, 'hello', 'rindex', 42) |
| 153 | |
| 154 | def test_lower(self): |
| 155 | self.checkequal('hello', 'HeLLo', 'lower') |
| 156 | self.checkequal('hello', 'hello', 'lower') |
| 157 | self.checkraises(TypeError, 'hello', 'lower', 42) |
| 158 | |
| 159 | def test_upper(self): |
| 160 | self.checkequal('HELLO', 'HeLLo', 'upper') |
| 161 | self.checkequal('HELLO', 'HELLO', 'upper') |
| 162 | self.checkraises(TypeError, 'hello', 'upper', 42) |
| 163 | |
| 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) |
| 172 | |
| 173 | self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42) |
| 174 | |
| 175 | def test_split(self): |
| 176 | self.checkequal(['this', 'is', 'the', 'split', 'function'], |
| 177 | 'this is the split function', 'split') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 178 | |
| 179 | # by whitespace |
| 180 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 181 | self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1) |
| 182 | self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2) |
| 183 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3) |
| 184 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4) |
| 185 | self.checkequal(['a b c d'], 'a b c d', 'split', None, 0) |
| 186 | self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 187 | |
| 188 | # by a char |
| 189 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|') |
| 190 | self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1) |
| 191 | self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2) |
| 192 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3) |
| 193 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4) |
| 194 | self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0) |
| 195 | self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2) |
| 196 | self.checkequal(['endcase ', ''], 'endcase |', 'split', '|') |
| 197 | self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2) |
| 198 | |
| 199 | # by string |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 200 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 201 | self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1) |
| 202 | self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2) |
| 203 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3) |
| 204 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4) |
| 205 | self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0) |
| 206 | self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 207 | self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test') |
| 208 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 209 | # mixed use of str and unicode |
| 210 | self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2) |
| 211 | |
| 212 | # argument type |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 213 | self.checkraises(TypeError, 'hello', 'split', 42, 42, 42) |
| 214 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 215 | def test_rsplit(self): |
| 216 | self.checkequal(['this', 'is', 'the', 'rsplit', 'function'], |
| 217 | 'this is the rsplit function', 'rsplit') |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 218 | |
| 219 | # by whitespace |
| 220 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit') |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 221 | self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1) |
| 222 | self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2) |
| 223 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3) |
| 224 | self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4) |
| 225 | self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 226 | self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 227 | |
| 228 | # by a char |
| 229 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|') |
| 230 | self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1) |
| 231 | self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2) |
| 232 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3) |
| 233 | self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4) |
| 234 | self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0) |
| 235 | self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2) |
| 236 | self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|') |
| 237 | self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2) |
| 238 | |
| 239 | # by string |
| 240 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//') |
| 241 | self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1) |
| 242 | self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2) |
| 243 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3) |
| 244 | self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4) |
| 245 | self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0) |
| 246 | self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2) |
| 247 | self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test') |
| 248 | |
| 249 | # mixed use of str and unicode |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 250 | self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 251 | |
| 252 | # argument type |
| 253 | self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 254 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 255 | def test_strip(self): |
| 256 | self.checkequal('hello', ' hello ', 'strip') |
| 257 | self.checkequal('hello ', ' hello ', 'lstrip') |
| 258 | self.checkequal(' hello', ' hello ', 'rstrip') |
| 259 | self.checkequal('hello', 'hello', 'strip') |
| 260 | |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 261 | # strip/lstrip/rstrip with None arg |
| 262 | self.checkequal('hello', ' hello ', 'strip', None) |
| 263 | self.checkequal('hello ', ' hello ', 'lstrip', None) |
| 264 | self.checkequal(' hello', ' hello ', 'rstrip', None) |
| 265 | self.checkequal('hello', 'hello', 'strip', None) |
| 266 | |
| 267 | # strip/lstrip/rstrip with str arg |
| 268 | self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz') |
| 269 | self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz') |
| 270 | self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz') |
| 271 | self.checkequal('hello', 'hello', 'strip', 'xyz') |
| 272 | |
| 273 | # strip/lstrip/rstrip with unicode arg |
| 274 | if test_support.have_unicode: |
| 275 | self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy', |
| 276 | 'strip', unicode('xyz', 'ascii')) |
| 277 | self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy', |
| 278 | 'lstrip', unicode('xyz', 'ascii')) |
| 279 | self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy', |
| 280 | 'rstrip', unicode('xyz', 'ascii')) |
| 281 | self.checkequal(unicode('hello', 'ascii'), 'hello', |
| 282 | 'strip', unicode('xyz', 'ascii')) |
| 283 | |
| 284 | self.checkraises(TypeError, 'hello', 'strip', 42, 42) |
| 285 | self.checkraises(TypeError, 'hello', 'lstrip', 42, 42) |
| 286 | self.checkraises(TypeError, 'hello', 'rstrip', 42, 42) |
| 287 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 288 | def test_ljust(self): |
| 289 | self.checkequal('abc ', 'abc', 'ljust', 10) |
| 290 | self.checkequal('abc ', 'abc', 'ljust', 6) |
| 291 | self.checkequal('abc', 'abc', 'ljust', 3) |
| 292 | self.checkequal('abc', 'abc', 'ljust', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 293 | self.checkequal('abc*******', 'abc', 'ljust', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 294 | self.checkraises(TypeError, 'abc', 'ljust') |
| 295 | |
| 296 | def test_rjust(self): |
| 297 | self.checkequal(' abc', 'abc', 'rjust', 10) |
| 298 | self.checkequal(' abc', 'abc', 'rjust', 6) |
| 299 | self.checkequal('abc', 'abc', 'rjust', 3) |
| 300 | self.checkequal('abc', 'abc', 'rjust', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 301 | self.checkequal('*******abc', 'abc', 'rjust', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 302 | self.checkraises(TypeError, 'abc', 'rjust') |
| 303 | |
| 304 | def test_center(self): |
| 305 | self.checkequal(' abc ', 'abc', 'center', 10) |
| 306 | self.checkequal(' abc ', 'abc', 'center', 6) |
| 307 | self.checkequal('abc', 'abc', 'center', 3) |
| 308 | self.checkequal('abc', 'abc', 'center', 2) |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 309 | self.checkequal('***abc****', 'abc', 'center', 10, '*') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 310 | self.checkraises(TypeError, 'abc', 'center') |
| 311 | |
| 312 | def test_swapcase(self): |
| 313 | self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase') |
| 314 | |
| 315 | self.checkraises(TypeError, 'hello', 'swapcase', 42) |
| 316 | |
| 317 | def test_replace(self): |
| 318 | self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) |
| 319 | self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '') |
| 320 | self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2) |
| 321 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3) |
| 322 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4) |
| 323 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0) |
| 324 | self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@') |
| 325 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@') |
| 326 | self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2) |
| 327 | self.checkequal('-a-b-c-', 'abc', 'replace', '', '-') |
| 328 | self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3) |
| 329 | self.checkequal('abc', 'abc', 'replace', '', '-', 0) |
| 330 | self.checkequal('', '', 'replace', '', '') |
| 331 | self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0) |
| 332 | self.checkequal('abc', 'abc', 'replace', 'xy', '--') |
| 333 | # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with |
| 334 | # MemoryError due to empty result (platform malloc issue when requesting |
| 335 | # 0 bytes). |
| 336 | self.checkequal('', '123', 'replace', '123', '') |
| 337 | self.checkequal('', '123123', 'replace', '123', '') |
| 338 | self.checkequal('x', '123x123', 'replace', '123', '') |
| 339 | |
| 340 | self.checkraises(TypeError, 'hello', 'replace') |
| 341 | self.checkraises(TypeError, 'hello', 'replace', 42) |
| 342 | self.checkraises(TypeError, 'hello', 'replace', 42, 'h') |
| 343 | self.checkraises(TypeError, 'hello', 'replace', 'h', 42) |
| 344 | |
| 345 | def test_zfill(self): |
| 346 | self.checkequal('123', '123', 'zfill', 2) |
| 347 | self.checkequal('123', '123', 'zfill', 3) |
| 348 | self.checkequal('0123', '123', 'zfill', 4) |
| 349 | self.checkequal('+123', '+123', 'zfill', 3) |
| 350 | self.checkequal('+123', '+123', 'zfill', 4) |
| 351 | self.checkequal('+0123', '+123', 'zfill', 5) |
| 352 | self.checkequal('-123', '-123', 'zfill', 3) |
| 353 | self.checkequal('-123', '-123', 'zfill', 4) |
| 354 | self.checkequal('-0123', '-123', 'zfill', 5) |
| 355 | self.checkequal('000', '', 'zfill', 3) |
| 356 | self.checkequal('34', '34', 'zfill', 1) |
| 357 | self.checkequal('0034', '34', 'zfill', 4) |
| 358 | |
| 359 | self.checkraises(TypeError, '123', 'zfill') |
| 360 | |
| 361 | class MixinStrUnicodeUserStringTest: |
| 362 | # additional tests that only work for |
| 363 | # stringlike objects, i.e. str, unicode, UserString |
| 364 | # (but not the string module) |
| 365 | |
| 366 | def test_islower(self): |
| 367 | self.checkequal(False, '', 'islower') |
| 368 | self.checkequal(True, 'a', 'islower') |
| 369 | self.checkequal(False, 'A', 'islower') |
| 370 | self.checkequal(False, '\n', 'islower') |
| 371 | self.checkequal(True, 'abc', 'islower') |
| 372 | self.checkequal(False, 'aBc', 'islower') |
| 373 | self.checkequal(True, 'abc\n', 'islower') |
| 374 | self.checkraises(TypeError, 'abc', 'islower', 42) |
| 375 | |
| 376 | def test_isupper(self): |
| 377 | self.checkequal(False, '', 'isupper') |
| 378 | self.checkequal(False, 'a', 'isupper') |
| 379 | self.checkequal(True, 'A', 'isupper') |
| 380 | self.checkequal(False, '\n', 'isupper') |
| 381 | self.checkequal(True, 'ABC', 'isupper') |
| 382 | self.checkequal(False, 'AbC', 'isupper') |
| 383 | self.checkequal(True, 'ABC\n', 'isupper') |
| 384 | self.checkraises(TypeError, 'abc', 'isupper', 42) |
| 385 | |
| 386 | def test_istitle(self): |
| 387 | self.checkequal(False, '', 'istitle') |
| 388 | self.checkequal(False, 'a', 'istitle') |
| 389 | self.checkequal(True, 'A', 'istitle') |
| 390 | self.checkequal(False, '\n', 'istitle') |
| 391 | self.checkequal(True, 'A Titlecased Line', 'istitle') |
| 392 | self.checkequal(True, 'A\nTitlecased Line', 'istitle') |
| 393 | self.checkequal(True, 'A Titlecased, Line', 'istitle') |
| 394 | self.checkequal(False, 'Not a capitalized String', 'istitle') |
| 395 | self.checkequal(False, 'Not\ta Titlecase String', 'istitle') |
| 396 | self.checkequal(False, 'Not--a Titlecase String', 'istitle') |
| 397 | self.checkequal(False, 'NOT', 'istitle') |
| 398 | self.checkraises(TypeError, 'abc', 'istitle', 42) |
| 399 | |
| 400 | def test_isspace(self): |
| 401 | self.checkequal(False, '', 'isspace') |
| 402 | self.checkequal(False, 'a', 'isspace') |
| 403 | self.checkequal(True, ' ', 'isspace') |
| 404 | self.checkequal(True, '\t', 'isspace') |
| 405 | self.checkequal(True, '\r', 'isspace') |
| 406 | self.checkequal(True, '\n', 'isspace') |
| 407 | self.checkequal(True, ' \t\r\n', 'isspace') |
| 408 | self.checkequal(False, ' \t\r\na', 'isspace') |
| 409 | self.checkraises(TypeError, 'abc', 'isspace', 42) |
| 410 | |
| 411 | def test_isalpha(self): |
| 412 | self.checkequal(False, '', 'isalpha') |
| 413 | self.checkequal(True, 'a', 'isalpha') |
| 414 | self.checkequal(True, 'A', 'isalpha') |
| 415 | self.checkequal(False, '\n', 'isalpha') |
| 416 | self.checkequal(True, 'abc', 'isalpha') |
| 417 | self.checkequal(False, 'aBc123', 'isalpha') |
| 418 | self.checkequal(False, 'abc\n', 'isalpha') |
| 419 | self.checkraises(TypeError, 'abc', 'isalpha', 42) |
| 420 | |
| 421 | def test_isalnum(self): |
| 422 | self.checkequal(False, '', 'isalnum') |
| 423 | self.checkequal(True, 'a', 'isalnum') |
| 424 | self.checkequal(True, 'A', 'isalnum') |
| 425 | self.checkequal(False, '\n', 'isalnum') |
| 426 | self.checkequal(True, '123abc456', 'isalnum') |
| 427 | self.checkequal(True, 'a1b3c', 'isalnum') |
| 428 | self.checkequal(False, 'aBc000 ', 'isalnum') |
| 429 | self.checkequal(False, 'abc\n', 'isalnum') |
| 430 | self.checkraises(TypeError, 'abc', 'isalnum', 42) |
| 431 | |
| 432 | def test_isdigit(self): |
| 433 | self.checkequal(False, '', 'isdigit') |
| 434 | self.checkequal(False, 'a', 'isdigit') |
| 435 | self.checkequal(True, '0', 'isdigit') |
| 436 | self.checkequal(True, '0123456789', 'isdigit') |
| 437 | self.checkequal(False, '0123456789a', 'isdigit') |
| 438 | |
| 439 | self.checkraises(TypeError, 'abc', 'isdigit', 42) |
| 440 | |
| 441 | def test_title(self): |
| 442 | self.checkequal(' Hello ', ' hello ', 'title') |
| 443 | self.checkequal('Hello ', 'hello ', 'title') |
| 444 | self.checkequal('Hello ', 'Hello ', 'title') |
| 445 | self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title') |
| 446 | self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', ) |
| 447 | self.checkequal('Getint', "getInt", 'title') |
| 448 | self.checkraises(TypeError, 'hello', 'title', 42) |
| 449 | |
| 450 | def test_splitlines(self): |
| 451 | self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines') |
| 452 | self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines') |
| 453 | self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines') |
| 454 | self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines') |
| 455 | self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines') |
| 456 | self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines') |
| 457 | self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1) |
| 458 | |
| 459 | self.checkraises(TypeError, 'abc', 'splitlines', 42, 42) |
| 460 | |
| 461 | def test_startswith(self): |
| 462 | self.checkequal(True, 'hello', 'startswith', 'he') |
| 463 | self.checkequal(True, 'hello', 'startswith', 'hello') |
| 464 | self.checkequal(False, 'hello', 'startswith', 'hello world') |
| 465 | self.checkequal(True, 'hello', 'startswith', '') |
| 466 | self.checkequal(False, 'hello', 'startswith', 'ello') |
| 467 | self.checkequal(True, 'hello', 'startswith', 'ello', 1) |
| 468 | self.checkequal(True, 'hello', 'startswith', 'o', 4) |
| 469 | self.checkequal(False, 'hello', 'startswith', 'o', 5) |
| 470 | self.checkequal(True, 'hello', 'startswith', '', 5) |
| 471 | self.checkequal(False, 'hello', 'startswith', 'lo', 6) |
| 472 | self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3) |
| 473 | self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7) |
| 474 | self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6) |
| 475 | |
| 476 | # test negative indices |
| 477 | self.checkequal(True, 'hello', 'startswith', 'he', 0, -1) |
| 478 | self.checkequal(True, 'hello', 'startswith', 'he', -53, -1) |
| 479 | self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1) |
| 480 | self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10) |
| 481 | self.checkequal(False, 'hello', 'startswith', 'ello', -5) |
| 482 | self.checkequal(True, 'hello', 'startswith', 'ello', -4) |
| 483 | self.checkequal(False, 'hello', 'startswith', 'o', -2) |
| 484 | self.checkequal(True, 'hello', 'startswith', 'o', -1) |
| 485 | self.checkequal(True, 'hello', 'startswith', '', -3, -3) |
| 486 | self.checkequal(False, 'hello', 'startswith', 'lo', -9) |
| 487 | |
| 488 | self.checkraises(TypeError, 'hello', 'startswith') |
| 489 | self.checkraises(TypeError, 'hello', 'startswith', 42) |
| 490 | |
| 491 | def test_endswith(self): |
| 492 | self.checkequal(True, 'hello', 'endswith', 'lo') |
| 493 | self.checkequal(False, 'hello', 'endswith', 'he') |
| 494 | self.checkequal(True, 'hello', 'endswith', '') |
| 495 | self.checkequal(False, 'hello', 'endswith', 'hello world') |
| 496 | self.checkequal(False, 'helloworld', 'endswith', 'worl') |
| 497 | self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9) |
| 498 | self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12) |
| 499 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7) |
| 500 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7) |
| 501 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7) |
| 502 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7) |
| 503 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8) |
| 504 | self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1) |
| 505 | self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0) |
| 506 | |
| 507 | # test negative indices |
| 508 | self.checkequal(True, 'hello', 'endswith', 'lo', -2) |
| 509 | self.checkequal(False, 'hello', 'endswith', 'he', -2) |
| 510 | self.checkequal(True, 'hello', 'endswith', '', -3, -3) |
| 511 | self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2) |
| 512 | self.checkequal(False, 'helloworld', 'endswith', 'worl', -6) |
| 513 | self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1) |
| 514 | self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9) |
| 515 | self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12) |
| 516 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3) |
| 517 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3) |
| 518 | self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3) |
| 519 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4) |
| 520 | self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2) |
| 521 | |
| 522 | self.checkraises(TypeError, 'hello', 'endswith') |
| 523 | self.checkraises(TypeError, 'hello', 'endswith', 42) |
| 524 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 525 | def test___contains__(self): |
| 526 | self.checkequal(True, '', '__contains__', '') # vereq('' in '', True) |
| 527 | self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True) |
| 528 | self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False) |
| 529 | self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True) |
| 530 | self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True) |
| 531 | self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True) |
| 532 | self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True) |
| 533 | self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False) |
| 534 | self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False) |
| 535 | |
| 536 | def test_subscript(self): |
| 537 | self.checkequal(u'a', 'abc', '__getitem__', 0) |
| 538 | self.checkequal(u'c', 'abc', '__getitem__', -1) |
| 539 | self.checkequal(u'a', 'abc', '__getitem__', 0L) |
| 540 | self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3)) |
| 541 | self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000)) |
| 542 | self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1)) |
| 543 | self.checkequal(u'', 'abc', '__getitem__', slice(0, 0)) |
| 544 | # FIXME What about negative indizes? This is handled differently by [] and __getitem__(slice) |
| 545 | |
| 546 | self.checkraises(TypeError, 'abc', '__getitem__', 'def') |
| 547 | |
| 548 | def test_slice(self): |
| 549 | self.checkequal('abc', 'abc', '__getslice__', 0, 1000) |
| 550 | self.checkequal('abc', 'abc', '__getslice__', 0, 3) |
| 551 | self.checkequal('ab', 'abc', '__getslice__', 0, 2) |
| 552 | self.checkequal('bc', 'abc', '__getslice__', 1, 3) |
| 553 | self.checkequal('b', 'abc', '__getslice__', 1, 2) |
| 554 | self.checkequal('', 'abc', '__getslice__', 2, 2) |
| 555 | self.checkequal('', 'abc', '__getslice__', 1000, 1000) |
| 556 | self.checkequal('', 'abc', '__getslice__', 2000, 1000) |
| 557 | self.checkequal('', 'abc', '__getslice__', 2, 1) |
| 558 | # FIXME What about negative indizes? This is handled differently by [] and __getslice__ |
| 559 | |
| 560 | self.checkraises(TypeError, 'abc', '__getslice__', 'def') |
| 561 | |
| 562 | def test_mul(self): |
| 563 | self.checkequal('', 'abc', '__mul__', -1) |
| 564 | self.checkequal('', 'abc', '__mul__', 0) |
| 565 | self.checkequal('abc', 'abc', '__mul__', 1) |
| 566 | self.checkequal('abcabcabc', 'abc', '__mul__', 3) |
| 567 | self.checkraises(TypeError, 'abc', '__mul__') |
| 568 | self.checkraises(TypeError, 'abc', '__mul__', '') |
Neal Norwitz | 15ff0e9 | 2003-02-23 23:15:26 +0000 | [diff] [blame] | 569 | self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 570 | |
| 571 | def test_join(self): |
| 572 | # join now works with any sequence type |
| 573 | # moved here, because the argument order is |
| 574 | # different in string.join (see the test in |
| 575 | # test.test_string.StringTest.test_join) |
| 576 | self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 577 | self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 578 | self.checkequal('w x y z', ' ', 'join', Sequence()) |
| 579 | self.checkequal('abc', 'a', 'join', ('abc',)) |
| 580 | self.checkequal('z', 'a', 'join', UserList(['z'])) |
| 581 | if test_support.have_unicode: |
| 582 | self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c']) |
| 583 | self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c']) |
| 584 | self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c']) |
| 585 | self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')]) |
| 586 | self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3]) |
| 587 | for i in [5, 25, 125]: |
| 588 | self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', |
| 589 | ['a' * i] * i) |
| 590 | self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join', |
| 591 | ('a' * i,) * i) |
| 592 | |
| 593 | self.checkraises(TypeError, ' ', 'join', BadSeq1()) |
| 594 | self.checkequal('a b c', ' ', 'join', BadSeq2()) |
| 595 | |
| 596 | self.checkraises(TypeError, ' ', 'join') |
| 597 | self.checkraises(TypeError, ' ', 'join', 7) |
| 598 | self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L])) |
| 599 | |
| 600 | def test_formatting(self): |
| 601 | self.checkequal('+hello+', '+%s+', '__mod__', 'hello') |
| 602 | self.checkequal('+10+', '+%d+', '__mod__', 10) |
| 603 | self.checkequal('a', "%c", '__mod__', "a") |
| 604 | self.checkequal('a', "%c", '__mod__', "a") |
| 605 | self.checkequal('"', "%c", '__mod__', 34) |
| 606 | self.checkequal('$', "%c", '__mod__', 36) |
| 607 | self.checkequal('10', "%d", '__mod__', 10) |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 608 | self.checkequal('\x7f', "%c", '__mod__', 0x7f) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 609 | |
| 610 | for ordinal in (-100, 0x200000): |
| 611 | # unicode raises ValueError, str raises OverflowError |
| 612 | self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal) |
| 613 | |
| 614 | self.checkequal(' 42', '%3ld', '__mod__', 42) |
| 615 | self.checkequal('0042.00', '%07.2f', '__mod__', 42) |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 616 | self.checkequal('0042.00', '%07.2F', '__mod__', 42) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 617 | |
| 618 | self.checkraises(TypeError, 'abc', '__mod__') |
| 619 | self.checkraises(TypeError, '%(foo)s', '__mod__', 42) |
| 620 | self.checkraises(TypeError, '%s%s', '__mod__', (42,)) |
| 621 | self.checkraises(TypeError, '%c', '__mod__', (None,)) |
| 622 | self.checkraises(ValueError, '%(foo', '__mod__', {}) |
| 623 | self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42)) |
| 624 | |
| 625 | # argument names with properly nested brackets are supported |
| 626 | self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'}) |
| 627 | |
| 628 | # 100 is a magic number in PyUnicode_Format, this forces a resize |
| 629 | self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a') |
| 630 | |
| 631 | self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar')) |
| 632 | self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.)) |
| 633 | self.checkraises(ValueError, '%10', '__mod__', (42,)) |
| 634 | |
| 635 | def test_floatformatting(self): |
| 636 | # float formatting |
| 637 | for prec in xrange(100): |
| 638 | format = '%%.%if' % prec |
| 639 | value = 0.01 |
| 640 | for x in xrange(60): |
| 641 | value = value * 3.141592655 / 3.0 * 10.0 |
| 642 | # The formatfloat() code in stringobject.c and |
| 643 | # unicodeobject.c uses a 120 byte buffer and switches from |
| 644 | # 'f' formatting to 'g' at precision 50, so we expect |
| 645 | # OverflowErrors for the ranges x < 50 and prec >= 67. |
| 646 | if x < 50 and prec >= 67: |
| 647 | self.checkraises(OverflowError, format, "__mod__", value) |
| 648 | else: |
| 649 | self.checkcall(format, "__mod__", value) |
| 650 | |
| 651 | class MixinStrStringUserStringTest: |
| 652 | # Additional tests for 8bit strings, i.e. str, UserString and |
| 653 | # the string module |
| 654 | |
| 655 | def test_maketrans(self): |
| 656 | self.assertEqual( |
| 657 | ''.join(map(chr, xrange(256))).replace('abc', 'xyz'), |
| 658 | string.maketrans('abc', 'xyz') |
| 659 | ) |
| 660 | self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw') |
| 661 | |
| 662 | def test_translate(self): |
| 663 | table = string.maketrans('abc', 'xyz') |
| 664 | self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def') |
| 665 | |
| 666 | table = string.maketrans('a', 'A') |
| 667 | self.checkequal('Abc', 'abc', 'translate', table) |
| 668 | self.checkequal('xyz', 'xyz', 'translate', table) |
| 669 | self.checkequal('yz', 'xyz', 'translate', table, 'x') |
| 670 | self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip') |
| 671 | self.checkraises(ValueError, 'xyz', 'translate', 'too short') |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 672 | |
| 673 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 674 | class MixinStrUserStringTest: |
| 675 | # Additional tests that only work with |
| 676 | # 8bit compatible object, i.e. str and UserString |
Jeremy Hylton | f82b04e | 2000-07-10 17:08:42 +0000 | [diff] [blame] | 677 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 678 | def test_encoding_decoding(self): |
| 679 | codecs = [('rot13', 'uryyb jbeyq'), |
| 680 | ('base64', 'aGVsbG8gd29ybGQ=\n'), |
| 681 | ('hex', '68656c6c6f20776f726c64'), |
| 682 | ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')] |
| 683 | for encoding, data in codecs: |
| 684 | self.checkequal(data, 'hello world', 'encode', encoding) |
| 685 | self.checkequal('hello world', data, 'decode', encoding) |
| 686 | # zlib is optional, so we make the test optional too... |
| 687 | try: |
| 688 | import zlib |
| 689 | except ImportError: |
| 690 | pass |
| 691 | else: |
| 692 | data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]' |
| 693 | self.checkequal(data, 'hello world', 'encode', 'zlib') |
| 694 | self.checkequal('hello world', data, 'decode', 'zlib') |
Walter Dörwald | 97951de | 2003-03-26 14:31:25 +0000 | [diff] [blame] | 695 | |
| 696 | self.checkraises(TypeError, 'xyz', 'decode', 42) |
| 697 | self.checkraises(TypeError, 'xyz', 'encode', 42) |