| 1 |
import operator |
| 2 |
import unittest |
| 3 |
|
| 4 |
from test import test_support |
| 5 |
|
| 6 |
class Seq1: |
| 7 |
def __init__(self, lst): |
| 8 |
self.lst = lst |
| 9 |
def __len__(self): |
| 10 |
return len(self.lst) |
| 11 |
def __getitem__(self, i): |
| 12 |
return self.lst[i] |
| 13 |
def __add__(self, other): |
| 14 |
return self.lst + other.lst |
| 15 |
def __mul__(self, other): |
| 16 |
return self.lst * other |
| 17 |
def __rmul__(self, other): |
| 18 |
return other * self.lst |
| 19 |
|
| 20 |
class Seq2(object): |
| 21 |
def __init__(self, lst): |
| 22 |
self.lst = lst |
| 23 |
def __len__(self): |
| 24 |
return len(self.lst) |
| 25 |
def __getitem__(self, i): |
| 26 |
return self.lst[i] |
| 27 |
def __add__(self, other): |
| 28 |
return self.lst + other.lst |
| 29 |
def __mul__(self, other): |
| 30 |
return self.lst * other |
| 31 |
def __rmul__(self, other): |
| 32 |
return other * self.lst |
| 33 |
|
| 34 |
|
| 35 |
class OperatorTestCase(unittest.TestCase): |
| 36 |
def test_lt(self): |
| 37 |
self.assertRaises(TypeError, operator.lt) |
| 38 |
self.assertRaises(TypeError, operator.lt, 1j, 2j) |
| 39 |
self.assertFalse(operator.lt(1, 0)) |
| 40 |
self.assertFalse(operator.lt(1, 0.0)) |
| 41 |
self.assertFalse(operator.lt(1, 1)) |
| 42 |
self.assertFalse(operator.lt(1, 1.0)) |
| 43 |
self.assertTrue(operator.lt(1, 2)) |
| 44 |
self.assertTrue(operator.lt(1, 2.0)) |
| 45 |
|
| 46 |
def test_le(self): |
| 47 |
self.assertRaises(TypeError, operator.le) |
| 48 |
self.assertRaises(TypeError, operator.le, 1j, 2j) |
| 49 |
self.assertFalse(operator.le(1, 0)) |
| 50 |
self.assertFalse(operator.le(1, 0.0)) |
| 51 |
self.assertTrue(operator.le(1, 1)) |
| 52 |
self.assertTrue(operator.le(1, 1.0)) |
| 53 |
self.assertTrue(operator.le(1, 2)) |
| 54 |
self.assertTrue(operator.le(1, 2.0)) |
| 55 |
|
| 56 |
def test_eq(self): |
| 57 |
class C(object): |
| 58 |
def __eq__(self, other): |
| 59 |
raise SyntaxError |
| 60 |
__hash__ = None |
| 61 |
self.assertRaises(TypeError, operator.eq) |
| 62 |
self.assertRaises(SyntaxError, operator.eq, C(), C()) |
| 63 |
self.assertFalse(operator.eq(1, 0)) |
| 64 |
self.assertFalse(operator.eq(1, 0.0)) |
| 65 |
self.assertTrue(operator.eq(1, 1)) |
| 66 |
self.assertTrue(operator.eq(1, 1.0)) |
| 67 |
self.assertFalse(operator.eq(1, 2)) |
| 68 |
self.assertFalse(operator.eq(1, 2.0)) |
| 69 |
|
| 70 |
def test_ne(self): |
| 71 |
class C(object): |
| 72 |
def __ne__(self, other): |
| 73 |
raise SyntaxError |
| 74 |
self.assertRaises(TypeError, operator.ne) |
| 75 |
self.assertRaises(SyntaxError, operator.ne, C(), C()) |
| 76 |
self.assertTrue(operator.ne(1, 0)) |
| 77 |
self.assertTrue(operator.ne(1, 0.0)) |
| 78 |
self.assertFalse(operator.ne(1, 1)) |
| 79 |
self.assertFalse(operator.ne(1, 1.0)) |
| 80 |
self.assertTrue(operator.ne(1, 2)) |
| 81 |
self.assertTrue(operator.ne(1, 2.0)) |
| 82 |
|
| 83 |
def test_ge(self): |
| 84 |
self.assertRaises(TypeError, operator.ge) |
| 85 |
self.assertRaises(TypeError, operator.ge, 1j, 2j) |
| 86 |
self.assertTrue(operator.ge(1, 0)) |
| 87 |
self.assertTrue(operator.ge(1, 0.0)) |
| 88 |
self.assertTrue(operator.ge(1, 1)) |
| 89 |
self.assertTrue(operator.ge(1, 1.0)) |
| 90 |
self.assertFalse(operator.ge(1, 2)) |
| 91 |
self.assertFalse(operator.ge(1, 2.0)) |
| 92 |
|
| 93 |
def test_gt(self): |
| 94 |
self.assertRaises(TypeError, operator.gt) |
| 95 |
self.assertRaises(TypeError, operator.gt, 1j, 2j) |
| 96 |
self.assertTrue(operator.gt(1, 0)) |
| 97 |
self.assertTrue(operator.gt(1, 0.0)) |
| 98 |
self.assertFalse(operator.gt(1, 1)) |
| 99 |
self.assertFalse(operator.gt(1, 1.0)) |
| 100 |
self.assertFalse(operator.gt(1, 2)) |
| 101 |
self.assertFalse(operator.gt(1, 2.0)) |
| 102 |
|
| 103 |
def test_abs(self): |
| 104 |
self.assertRaises(TypeError, operator.abs) |
| 105 |
self.assertRaises(TypeError, operator.abs, None) |
| 106 |
self.assertTrue(operator.abs(-1) == 1) |
| 107 |
self.assertTrue(operator.abs(1) == 1) |
| 108 |
|
| 109 |
def test_add(self): |
| 110 |
self.assertRaises(TypeError, operator.add) |
| 111 |
self.assertRaises(TypeError, operator.add, None, None) |
| 112 |
self.assertTrue(operator.add(3, 4) == 7) |
| 113 |
|
| 114 |
def test_bitwise_and(self): |
| 115 |
self.assertRaises(TypeError, operator.and_) |
| 116 |
self.assertRaises(TypeError, operator.and_, None, None) |
| 117 |
self.assertTrue(operator.and_(0xf, 0xa) == 0xa) |
| 118 |
|
| 119 |
def test_concat(self): |
| 120 |
self.assertRaises(TypeError, operator.concat) |
| 121 |
self.assertRaises(TypeError, operator.concat, None, None) |
| 122 |
self.assertTrue(operator.concat('py', 'thon') == 'python') |
| 123 |
self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4]) |
| 124 |
self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7]) |
| 125 |
self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7]) |
| 126 |
self.assertRaises(TypeError, operator.concat, 13, 29) |
| 127 |
|
| 128 |
def test_countOf(self): |
| 129 |
self.assertRaises(TypeError, operator.countOf) |
| 130 |
self.assertRaises(TypeError, operator.countOf, None, None) |
| 131 |
self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1) |
| 132 |
self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0) |
| 133 |
|
| 134 |
def test_delitem(self): |
| 135 |
a = [4, 3, 2, 1] |
| 136 |
self.assertRaises(TypeError, operator.delitem, a) |
| 137 |
self.assertRaises(TypeError, operator.delitem, a, None) |
| 138 |
self.assertTrue(operator.delitem(a, 1) is None) |
| 139 |
self.assertTrue(a == [4, 2, 1]) |
| 140 |
|
| 141 |
def test_delslice(self): |
| 142 |
a = range(10) |
| 143 |
self.assertRaises(TypeError, operator.delslice, a) |
| 144 |
self.assertRaises(TypeError, operator.delslice, a, None, None) |
| 145 |
self.assertTrue(operator.delslice(a, 2, 8) is None) |
| 146 |
self.assertTrue(a == [0, 1, 8, 9]) |
| 147 |
operator.delslice(a, 0, test_support.MAX_Py_ssize_t) |
| 148 |
self.assertTrue(a == []) |
| 149 |
|
| 150 |
def test_div(self): |
| 151 |
self.assertRaises(TypeError, operator.div, 5) |
| 152 |
self.assertRaises(TypeError, operator.div, None, None) |
| 153 |
self.assertTrue(operator.floordiv(5, 2) == 2) |
| 154 |
|
| 155 |
def test_floordiv(self): |
| 156 |
self.assertRaises(TypeError, operator.floordiv, 5) |
| 157 |
self.assertRaises(TypeError, operator.floordiv, None, None) |
| 158 |
self.assertTrue(operator.floordiv(5, 2) == 2) |
| 159 |
|
| 160 |
def test_truediv(self): |
| 161 |
self.assertRaises(TypeError, operator.truediv, 5) |
| 162 |
self.assertRaises(TypeError, operator.truediv, None, None) |
| 163 |
self.assertTrue(operator.truediv(5, 2) == 2.5) |
| 164 |
|
| 165 |
def test_getitem(self): |
| 166 |
a = range(10) |
| 167 |
self.assertRaises(TypeError, operator.getitem) |
| 168 |
self.assertRaises(TypeError, operator.getitem, a, None) |
| 169 |
self.assertTrue(operator.getitem(a, 2) == 2) |
| 170 |
|
| 171 |
def test_getslice(self): |
| 172 |
a = range(10) |
| 173 |
self.assertRaises(TypeError, operator.getslice) |
| 174 |
self.assertRaises(TypeError, operator.getslice, a, None, None) |
| 175 |
self.assertTrue(operator.getslice(a, 4, 6) == [4, 5]) |
| 176 |
b = operator.getslice(a, 0, test_support.MAX_Py_ssize_t) |
| 177 |
self.assertTrue(b == a) |
| 178 |
|
| 179 |
def test_indexOf(self): |
| 180 |
self.assertRaises(TypeError, operator.indexOf) |
| 181 |
self.assertRaises(TypeError, operator.indexOf, None, None) |
| 182 |
self.assertTrue(operator.indexOf([4, 3, 2, 1], 3) == 1) |
| 183 |
self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0) |
| 184 |
|
| 185 |
def test_invert(self): |
| 186 |
self.assertRaises(TypeError, operator.invert) |
| 187 |
self.assertRaises(TypeError, operator.invert, None) |
| 188 |
self.assertTrue(operator.inv(4) == -5) |
| 189 |
|
| 190 |
def test_isCallable(self): |
| 191 |
self.assertRaises(TypeError, operator.isCallable) |
| 192 |
class C: |
| 193 |
pass |
| 194 |
def check(self, o, v): |
| 195 |
with test_support.check_py3k_warnings(): |
| 196 |
self.assertEqual(operator.isCallable(o), v) |
| 197 |
self.assertEqual(callable(o), v) |
| 198 |
check(self, 4, 0) |
| 199 |
check(self, operator.isCallable, 1) |
| 200 |
check(self, C, 1) |
| 201 |
check(self, C(), 0) |
| 202 |
|
| 203 |
def test_isMappingType(self): |
| 204 |
self.assertRaises(TypeError, operator.isMappingType) |
| 205 |
self.assertFalse(operator.isMappingType(1)) |
| 206 |
self.assertFalse(operator.isMappingType(operator.isMappingType)) |
| 207 |
self.assertTrue(operator.isMappingType(operator.__dict__)) |
| 208 |
self.assertTrue(operator.isMappingType({})) |
| 209 |
|
| 210 |
def test_isNumberType(self): |
| 211 |
self.assertRaises(TypeError, operator.isNumberType) |
| 212 |
self.assertTrue(operator.isNumberType(8)) |
| 213 |
self.assertTrue(operator.isNumberType(8j)) |
| 214 |
self.assertTrue(operator.isNumberType(8L)) |
| 215 |
self.assertTrue(operator.isNumberType(8.3)) |
| 216 |
self.assertFalse(operator.isNumberType(dir())) |
| 217 |
|
| 218 |
def test_isSequenceType(self): |
| 219 |
self.assertRaises(TypeError, operator.isSequenceType) |
| 220 |
self.assertTrue(operator.isSequenceType(dir())) |
| 221 |
self.assertTrue(operator.isSequenceType(())) |
| 222 |
self.assertTrue(operator.isSequenceType(xrange(10))) |
| 223 |
self.assertTrue(operator.isSequenceType('yeahbuddy')) |
| 224 |
self.assertFalse(operator.isSequenceType(3)) |
| 225 |
class Dict(dict): pass |
| 226 |
self.assertFalse(operator.isSequenceType(Dict())) |
| 227 |
|
| 228 |
def test_lshift(self): |
| 229 |
self.assertRaises(TypeError, operator.lshift) |
| 230 |
self.assertRaises(TypeError, operator.lshift, None, 42) |
| 231 |
self.assertTrue(operator.lshift(5, 1) == 10) |
| 232 |
self.assertTrue(operator.lshift(5, 0) == 5) |
| 233 |
self.assertRaises(ValueError, operator.lshift, 2, -1) |
| 234 |
|
| 235 |
def test_mod(self): |
| 236 |
self.assertRaises(TypeError, operator.mod) |
| 237 |
self.assertRaises(TypeError, operator.mod, None, 42) |
| 238 |
self.assertTrue(operator.mod(5, 2) == 1) |
| 239 |
|
| 240 |
def test_mul(self): |
| 241 |
self.assertRaises(TypeError, operator.mul) |
| 242 |
self.assertRaises(TypeError, operator.mul, None, None) |
| 243 |
self.assertTrue(operator.mul(5, 2) == 10) |
| 244 |
|
| 245 |
def test_neg(self): |
| 246 |
self.assertRaises(TypeError, operator.neg) |
| 247 |
self.assertRaises(TypeError, operator.neg, None) |
| 248 |
self.assertTrue(operator.neg(5) == -5) |
| 249 |
self.assertTrue(operator.neg(-5) == 5) |
| 250 |
self.assertTrue(operator.neg(0) == 0) |
| 251 |
self.assertTrue(operator.neg(-0) == 0) |
| 252 |
|
| 253 |
def test_bitwise_or(self): |
| 254 |
self.assertRaises(TypeError, operator.or_) |
| 255 |
self.assertRaises(TypeError, operator.or_, None, None) |
| 256 |
self.assertTrue(operator.or_(0xa, 0x5) == 0xf) |
| 257 |
|
| 258 |
def test_pos(self): |
| 259 |
self.assertRaises(TypeError, operator.pos) |
| 260 |
self.assertRaises(TypeError, operator.pos, None) |
| 261 |
self.assertTrue(operator.pos(5) == 5) |
| 262 |
self.assertTrue(operator.pos(-5) == -5) |
| 263 |
self.assertTrue(operator.pos(0) == 0) |
| 264 |
self.assertTrue(operator.pos(-0) == 0) |
| 265 |
|
| 266 |
def test_pow(self): |
| 267 |
self.assertRaises(TypeError, operator.pow) |
| 268 |
self.assertRaises(TypeError, operator.pow, None, None) |
| 269 |
self.assertTrue(operator.pow(3,5) == 3**5) |
| 270 |
self.assertTrue(operator.__pow__(3,5) == 3**5) |
| 271 |
self.assertRaises(TypeError, operator.pow, 1) |
| 272 |
self.assertRaises(TypeError, operator.pow, 1, 2, 3) |
| 273 |
|
| 274 |
def test_repeat(self): |
| 275 |
a = range(3) |
| 276 |
self.assertRaises(TypeError, operator.repeat) |
| 277 |
self.assertRaises(TypeError, operator.repeat, a, None) |
| 278 |
self.assertTrue(operator.repeat(a, 2) == a+a) |
| 279 |
self.assertTrue(operator.repeat(a, 1) == a) |
| 280 |
self.assertTrue(operator.repeat(a, 0) == []) |
| 281 |
a = (1, 2, 3) |
| 282 |
self.assertTrue(operator.repeat(a, 2) == a+a) |
| 283 |
self.assertTrue(operator.repeat(a, 1) == a) |
| 284 |
self.assertTrue(operator.repeat(a, 0) == ()) |
| 285 |
a = '123' |
| 286 |
self.assertTrue(operator.repeat(a, 2) == a+a) |
| 287 |
self.assertTrue(operator.repeat(a, 1) == a) |
| 288 |
self.assertTrue(operator.repeat(a, 0) == '') |
| 289 |
a = Seq1([4, 5, 6]) |
| 290 |
self.assertTrue(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]) |
| 291 |
self.assertTrue(operator.repeat(a, 1) == [4, 5, 6]) |
| 292 |
self.assertTrue(operator.repeat(a, 0) == []) |
| 293 |
a = Seq2([4, 5, 6]) |
| 294 |
self.assertTrue(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]) |
| 295 |
self.assertTrue(operator.repeat(a, 1) == [4, 5, 6]) |
| 296 |
self.assertTrue(operator.repeat(a, 0) == []) |
| 297 |
self.assertRaises(TypeError, operator.repeat, 6, 7) |
| 298 |
|
| 299 |
def test_rshift(self): |
| 300 |
self.assertRaises(TypeError, operator.rshift) |
| 301 |
self.assertRaises(TypeError, operator.rshift, None, 42) |
| 302 |
self.assertTrue(operator.rshift(5, 1) == 2) |
| 303 |
self.assertTrue(operator.rshift(5, 0) == 5) |
| 304 |
self.assertRaises(ValueError, operator.rshift, 2, -1) |
| 305 |
|
| 306 |
def test_contains(self): |
| 307 |
self.assertRaises(TypeError, operator.contains) |
| 308 |
self.assertRaises(TypeError, operator.contains, None, None) |
| 309 |
self.assertTrue(operator.contains(range(4), 2)) |
| 310 |
self.assertFalse(operator.contains(range(4), 5)) |
| 311 |
with test_support.check_py3k_warnings(): |
| 312 |
self.assertTrue(operator.sequenceIncludes(range(4), 2)) |
| 313 |
self.assertFalse(operator.sequenceIncludes(range(4), 5)) |
| 314 |
|
| 315 |
def test_setitem(self): |
| 316 |
a = range(3) |
| 317 |
self.assertRaises(TypeError, operator.setitem, a) |
| 318 |
self.assertRaises(TypeError, operator.setitem, a, None, None) |
| 319 |
self.assertTrue(operator.setitem(a, 0, 2) is None) |
| 320 |
self.assertTrue(a == [2, 1, 2]) |
| 321 |
self.assertRaises(IndexError, operator.setitem, a, 4, 2) |
| 322 |
|
| 323 |
def test_setslice(self): |
| 324 |
a = range(4) |
| 325 |
self.assertRaises(TypeError, operator.setslice, a) |
| 326 |
self.assertRaises(TypeError, operator.setslice, a, None, None, None) |
| 327 |
self.assertTrue(operator.setslice(a, 1, 3, [2, 1]) is None) |
| 328 |
self.assertTrue(a == [0, 2, 1, 3]) |
| 329 |
operator.setslice(a, 0, test_support.MAX_Py_ssize_t, []) |
| 330 |
self.assertTrue(a == []) |
| 331 |
|
| 332 |
def test_sub(self): |
| 333 |
self.assertRaises(TypeError, operator.sub) |
| 334 |
self.assertRaises(TypeError, operator.sub, None, None) |
| 335 |
self.assertTrue(operator.sub(5, 2) == 3) |
| 336 |
|
| 337 |
def test_truth(self): |
| 338 |
class C(object): |
| 339 |
def __nonzero__(self): |
| 340 |
raise SyntaxError |
| 341 |
self.assertRaises(TypeError, operator.truth) |
| 342 |
self.assertRaises(SyntaxError, operator.truth, C()) |
| 343 |
self.assertTrue(operator.truth(5)) |
| 344 |
self.assertTrue(operator.truth([0])) |
| 345 |
self.assertFalse(operator.truth(0)) |
| 346 |
self.assertFalse(operator.truth([])) |
| 347 |
|
| 348 |
def test_bitwise_xor(self): |
| 349 |
self.assertRaises(TypeError, operator.xor) |
| 350 |
self.assertRaises(TypeError, operator.xor, None, None) |
| 351 |
self.assertTrue(operator.xor(0xb, 0xc) == 0x7) |
| 352 |
|
| 353 |
def test_is(self): |
| 354 |
a = b = 'xyzpdq' |
| 355 |
c = a[:3] + b[3:] |
| 356 |
self.assertRaises(TypeError, operator.is_) |
| 357 |
self.assertTrue(operator.is_(a, b)) |
| 358 |
self.assertFalse(operator.is_(a,c)) |
| 359 |
|
| 360 |
def test_is_not(self): |
| 361 |
a = b = 'xyzpdq' |
| 362 |
c = a[:3] + b[3:] |
| 363 |
self.assertRaises(TypeError, operator.is_not) |
| 364 |
self.assertFalse(operator.is_not(a, b)) |
| 365 |
self.assertTrue(operator.is_not(a,c)) |
| 366 |
|
| 367 |
def test_attrgetter(self): |
| 368 |
class A: |
| 369 |
pass |
| 370 |
a = A() |
| 371 |
a.name = 'arthur' |
| 372 |
f = operator.attrgetter('name') |
| 373 |
self.assertEqual(f(a), 'arthur') |
| 374 |
self.assertRaises(TypeError, f) |
| 375 |
self.assertRaises(TypeError, f, a, 'dent') |
| 376 |
self.assertRaises(TypeError, f, a, surname='dent') |
| 377 |
f = operator.attrgetter('rank') |
| 378 |
self.assertRaises(AttributeError, f, a) |
| 379 |
f = operator.attrgetter(2) |
| 380 |
self.assertRaises(TypeError, f, a) |
| 381 |
self.assertRaises(TypeError, operator.attrgetter) |
| 382 |
|
| 383 |
|
| 384 |
record = A() |
| 385 |
record.x = 'X' |
| 386 |
record.y = 'Y' |
| 387 |
record.z = 'Z' |
| 388 |
self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y')) |
| 389 |
self.assertRaises(TypeError, operator.attrgetter('x', (), 'y'), record) |
| 390 |
|
| 391 |
class C(object): |
| 392 |
def __getattr__(self, name): |
| 393 |
raise SyntaxError |
| 394 |
self.assertRaises(SyntaxError, operator.attrgetter('foo'), C()) |
| 395 |
|
| 396 |
|
| 397 |
a = A() |
| 398 |
a.name = 'arthur' |
| 399 |
a.child = A() |
| 400 |
a.child.name = 'thomas' |
| 401 |
f = operator.attrgetter('child.name') |
| 402 |
self.assertEqual(f(a), 'thomas') |
| 403 |
self.assertRaises(AttributeError, f, a.child) |
| 404 |
f = operator.attrgetter('name', 'child.name') |
| 405 |
self.assertEqual(f(a), ('arthur', 'thomas')) |
| 406 |
f = operator.attrgetter('name', 'child.name', 'child.child.name') |
| 407 |
self.assertRaises(AttributeError, f, a) |
| 408 |
|
| 409 |
a.child.child = A() |
| 410 |
a.child.child.name = 'johnson' |
| 411 |
f = operator.attrgetter('child.child.name') |
| 412 |
self.assertEqual(f(a), 'johnson') |
| 413 |
f = operator.attrgetter('name', 'child.name', 'child.child.name') |
| 414 |
self.assertEqual(f(a), ('arthur', 'thomas', 'johnson')) |
| 415 |
|
| 416 |
def test_itemgetter(self): |
| 417 |
a = 'ABCDE' |
| 418 |
f = operator.itemgetter(2) |
| 419 |
self.assertEqual(f(a), 'C') |
| 420 |
self.assertRaises(TypeError, f) |
| 421 |
self.assertRaises(TypeError, f, a, 3) |
| 422 |
self.assertRaises(TypeError, f, a, size=3) |
| 423 |
f = operator.itemgetter(10) |
| 424 |
self.assertRaises(IndexError, f, a) |
| 425 |
|
| 426 |
class C(object): |
| 427 |
def __getitem__(self, name): |
| 428 |
raise SyntaxError |
| 429 |
self.assertRaises(SyntaxError, operator.itemgetter(42), C()) |
| 430 |
|
| 431 |
f = operator.itemgetter('name') |
| 432 |
self.assertRaises(TypeError, f, a) |
| 433 |
self.assertRaises(TypeError, operator.itemgetter) |
| 434 |
|
| 435 |
d = dict(key='val') |
| 436 |
f = operator.itemgetter('key') |
| 437 |
self.assertEqual(f(d), 'val') |
| 438 |
f = operator.itemgetter('nonkey') |
| 439 |
self.assertRaises(KeyError, f, d) |
| 440 |
|
| 441 |
|
| 442 |
inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
| 443 |
getcount = operator.itemgetter(1) |
| 444 |
self.assertEqual(map(getcount, inventory), [3, 2, 5, 1]) |
| 445 |
self.assertEqual(sorted(inventory, key=getcount), |
| 446 |
[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]) |
| 447 |
|
| 448 |
|
| 449 |
data = map(str, range(20)) |
| 450 |
self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5')) |
| 451 |
self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data) |
| 452 |
|
| 453 |
def test_methodcaller(self): |
| 454 |
self.assertRaises(TypeError, operator.methodcaller) |
| 455 |
class A: |
| 456 |
def foo(self, *args, **kwds): |
| 457 |
return args[0] + args[1] |
| 458 |
def bar(self, f=42): |
| 459 |
return f |
| 460 |
a = A() |
| 461 |
f = operator.methodcaller('foo') |
| 462 |
self.assertRaises(IndexError, f, a) |
| 463 |
f = operator.methodcaller('foo', 1, 2) |
| 464 |
self.assertEqual(f(a), 3) |
| 465 |
self.assertRaises(TypeError, f) |
| 466 |
self.assertRaises(TypeError, f, a, 3) |
| 467 |
self.assertRaises(TypeError, f, a, spam=3) |
| 468 |
f = operator.methodcaller('bar') |
| 469 |
self.assertEqual(f(a), 42) |
| 470 |
self.assertRaises(TypeError, f, a, a) |
| 471 |
f = operator.methodcaller('bar', f=5) |
| 472 |
self.assertEqual(f(a), 5) |
| 473 |
|
| 474 |
def test_inplace(self): |
| 475 |
class C(object): |
| 476 |
def __iadd__ (self, other): return "iadd" |
| 477 |
def __iand__ (self, other): return "iand" |
| 478 |
def __idiv__ (self, other): return "idiv" |
| 479 |
def __ifloordiv__(self, other): return "ifloordiv" |
| 480 |
def __ilshift__ (self, other): return "ilshift" |
| 481 |
def __imod__ (self, other): return "imod" |
| 482 |
def __imul__ (self, other): return "imul" |
| 483 |
def __ior__ (self, other): return "ior" |
| 484 |
def __ipow__ (self, other): return "ipow" |
| 485 |
def __irshift__ (self, other): return "irshift" |
| 486 |
def __isub__ (self, other): return "isub" |
| 487 |
def __itruediv__ (self, other): return "itruediv" |
| 488 |
def __ixor__ (self, other): return "ixor" |
| 489 |
def __getitem__(self, other): return 5 |
| 490 |
c = C() |
| 491 |
self.assertEqual(operator.iadd (c, 5), "iadd") |
| 492 |
self.assertEqual(operator.iand (c, 5), "iand") |
| 493 |
self.assertEqual(operator.idiv (c, 5), "idiv") |
| 494 |
self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") |
| 495 |
self.assertEqual(operator.ilshift (c, 5), "ilshift") |
| 496 |
self.assertEqual(operator.imod (c, 5), "imod") |
| 497 |
self.assertEqual(operator.imul (c, 5), "imul") |
| 498 |
self.assertEqual(operator.ior (c, 5), "ior") |
| 499 |
self.assertEqual(operator.ipow (c, 5), "ipow") |
| 500 |
self.assertEqual(operator.irshift (c, 5), "irshift") |
| 501 |
self.assertEqual(operator.isub (c, 5), "isub") |
| 502 |
self.assertEqual(operator.itruediv (c, 5), "itruediv") |
| 503 |
self.assertEqual(operator.ixor (c, 5), "ixor") |
| 504 |
self.assertEqual(operator.iconcat (c, c), "iadd") |
| 505 |
self.assertEqual(operator.irepeat (c, 5), "imul") |
| 506 |
self.assertEqual(operator.__iadd__ (c, 5), "iadd") |
| 507 |
self.assertEqual(operator.__iand__ (c, 5), "iand") |
| 508 |
self.assertEqual(operator.__idiv__ (c, 5), "idiv") |
| 509 |
self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv") |
| 510 |
self.assertEqual(operator.__ilshift__ (c, 5), "ilshift") |
| 511 |
self.assertEqual(operator.__imod__ (c, 5), "imod") |
| 512 |
self.assertEqual(operator.__imul__ (c, 5), "imul") |
| 513 |
self.assertEqual(operator.__ior__ (c, 5), "ior") |
| 514 |
self.assertEqual(operator.__ipow__ (c, 5), "ipow") |
| 515 |
self.assertEqual(operator.__irshift__ (c, 5), "irshift") |
| 516 |
self.assertEqual(operator.__isub__ (c, 5), "isub") |
| 517 |
self.assertEqual(operator.__itruediv__ (c, 5), "itruediv") |
| 518 |
self.assertEqual(operator.__ixor__ (c, 5), "ixor") |
| 519 |
self.assertEqual(operator.__iconcat__ (c, c), "iadd") |
| 520 |
self.assertEqual(operator.__irepeat__ (c, 5), "imul") |
| 521 |
|
| 522 |
def test_main(verbose=None): |
| 523 |
import sys |
| 524 |
test_classes = ( |
| 525 |
OperatorTestCase, |
| 526 |
) |
| 527 |
|
| 528 |
test_support.run_unittest(*test_classes) |
| 529 |
|
| 530 |
|
| 531 |
if verbose and hasattr(sys, "gettotalrefcount"): |
| 532 |
import gc |
| 533 |
counts = [None] * 5 |
| 534 |
for i in xrange(len(counts)): |
| 535 |
test_support.run_unittest(*test_classes) |
| 536 |
gc.collect() |
| 537 |
counts[i] = sys.gettotalrefcount() |
| 538 |
print counts |
| 539 |
|
| 540 |
if __name__ == "__main__": |
| 541 |
test_main(verbose=True) |