Back to index

python3.2  3.2.2
Public Member Functions
test.test_operator.OperatorTestCase Class Reference

List of all members.

Public Member Functions

def test_lt
def test_le
def test_eq
def test_ne
def test_ge
def test_gt
def test_abs
def test_add
def test_bitwise_and
def test_concat
def test_countOf
def test_delitem
def test_floordiv
def test_truediv
def test_getitem
def test_indexOf
def test_invert
def test_lshift
def test_mod
def test_mul
def test_neg
def test_bitwise_or
def test_pos
def test_pow
def test_rshift
def test_contains
def test_setitem
def test_sub
def test_truth
def test_bitwise_xor
def test_is
def test_is_not
def test_attrgetter
def test_itemgetter
def test_methodcaller
def test_inplace

Detailed Description

Definition at line 35 of file test_operator.py.


Member Function Documentation

Definition at line 102 of file test_operator.py.

00102 
00103     def test_abs(self):
00104         self.assertRaises(TypeError, operator.abs)
00105         self.assertRaises(TypeError, operator.abs, None)
00106         self.assertEqual(operator.abs(-1), 1)
00107         self.assertEqual(operator.abs(1), 1)

Here is the call graph for this function:

Definition at line 108 of file test_operator.py.

00108 
00109     def test_add(self):
00110         self.assertRaises(TypeError, operator.add)
00111         self.assertRaises(TypeError, operator.add, None, None)
00112         self.assertTrue(operator.add(3, 4) == 7)

Here is the call graph for this function:

Definition at line 269 of file test_operator.py.

00269 
00270     def test_attrgetter(self):
00271         class A:
00272             pass
00273         a = A()
00274         a.name = 'arthur'
00275         f = operator.attrgetter('name')
00276         self.assertEqual(f(a), 'arthur')
00277         f = operator.attrgetter('rank')
00278         self.assertRaises(AttributeError, f, a)
00279         self.assertRaises(TypeError, operator.attrgetter, 2)
00280         self.assertRaises(TypeError, operator.attrgetter)
00281 
00282         # multiple gets
00283         record = A()
00284         record.x = 'X'
00285         record.y = 'Y'
00286         record.z = 'Z'
00287         self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y'))
00288         self.assertRaises(TypeError, operator.attrgetter, ('x', (), 'y'))
00289 
00290         class C(object):
00291             def __getattr__(self, name):
00292                 raise SyntaxError
00293         self.assertRaises(SyntaxError, operator.attrgetter('foo'), C())
00294 
00295         # recursive gets
00296         a = A()
00297         a.name = 'arthur'
00298         a.child = A()
00299         a.child.name = 'thomas'
00300         f = operator.attrgetter('child.name')
00301         self.assertEqual(f(a), 'thomas')
00302         self.assertRaises(AttributeError, f, a.child)
00303         f = operator.attrgetter('name', 'child.name')
00304         self.assertEqual(f(a), ('arthur', 'thomas'))
00305         f = operator.attrgetter('name', 'child.name', 'child.child.name')
00306         self.assertRaises(AttributeError, f, a)
00307         f = operator.attrgetter('child.')
00308         self.assertRaises(AttributeError, f, a)
00309         f = operator.attrgetter('.child')
00310         self.assertRaises(AttributeError, f, a)
00311 
00312         a.child.child = A()
00313         a.child.child.name = 'johnson'
00314         f = operator.attrgetter('child.child.name')
00315         self.assertEqual(f(a), 'johnson')
00316         f = operator.attrgetter('name', 'child.name', 'child.child.name')
00317         self.assertEqual(f(a), ('arthur', 'thomas', 'johnson'))

Definition at line 113 of file test_operator.py.

00113 
00114     def test_bitwise_and(self):
00115         self.assertRaises(TypeError, operator.and_)
00116         self.assertRaises(TypeError, operator.and_, None, None)
00117         self.assertTrue(operator.and_(0xf, 0xa) == 0xa)

Here is the call graph for this function:

Definition at line 192 of file test_operator.py.

00192 
00193     def test_bitwise_or(self):
00194         self.assertRaises(TypeError, operator.or_)
00195         self.assertRaises(TypeError, operator.or_, None, None)
00196         self.assertTrue(operator.or_(0xa, 0x5) == 0xf)

Here is the call graph for this function:

Definition at line 250 of file test_operator.py.

00250 
00251     def test_bitwise_xor(self):
00252         self.assertRaises(TypeError, operator.xor)
00253         self.assertRaises(TypeError, operator.xor, None, None)
00254         self.assertTrue(operator.xor(0xb, 0xc) == 0x7)

Here is the call graph for this function:

Definition at line 118 of file test_operator.py.

00118 
00119     def test_concat(self):
00120         self.assertRaises(TypeError, operator.concat)
00121         self.assertRaises(TypeError, operator.concat, None, None)
00122         self.assertTrue(operator.concat('py', 'thon') == 'python')
00123         self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
00124         self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
00125         self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
00126         self.assertRaises(TypeError, operator.concat, 13, 29)

Here is the call graph for this function:

Definition at line 220 of file test_operator.py.

00220 
00221     def test_contains(self):
00222         self.assertRaises(TypeError, operator.contains)
00223         self.assertRaises(TypeError, operator.contains, None, None)
00224         self.assertTrue(operator.contains(range(4), 2))
00225         self.assertFalse(operator.contains(range(4), 5))

Here is the call graph for this function:

Definition at line 127 of file test_operator.py.

00127 
00128     def test_countOf(self):
00129         self.assertRaises(TypeError, operator.countOf)
00130         self.assertRaises(TypeError, operator.countOf, None, None)
00131         self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)
00132         self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)

Here is the call graph for this function:

Definition at line 133 of file test_operator.py.

00133 
00134     def test_delitem(self):
00135         a = [4, 3, 2, 1]
00136         self.assertRaises(TypeError, operator.delitem, a)
00137         self.assertRaises(TypeError, operator.delitem, a, None)
00138         self.assertTrue(operator.delitem(a, 1) is None)
00139         self.assertTrue(a == [4, 2, 1])

Here is the call graph for this function:

Definition at line 56 of file test_operator.py.

00056 
00057     def test_eq(self):
00058         class C(object):
00059             def __eq__(self, other):
00060                 raise SyntaxError
00061         self.assertRaises(TypeError, operator.eq)
00062         self.assertRaises(SyntaxError, operator.eq, C(), C())
00063         self.assertFalse(operator.eq(1, 0))
00064         self.assertFalse(operator.eq(1, 0.0))
00065         self.assertTrue(operator.eq(1, 1))
00066         self.assertTrue(operator.eq(1, 1.0))
00067         self.assertFalse(operator.eq(1, 2))
00068         self.assertFalse(operator.eq(1, 2.0))

Here is the call graph for this function:

Definition at line 140 of file test_operator.py.

00140 
00141     def test_floordiv(self):
00142         self.assertRaises(TypeError, operator.floordiv, 5)
00143         self.assertRaises(TypeError, operator.floordiv, None, None)
00144         self.assertTrue(operator.floordiv(5, 2) == 2)

Here is the call graph for this function:

Definition at line 82 of file test_operator.py.

00082 
00083     def test_ge(self):
00084         self.assertRaises(TypeError, operator.ge)
00085         self.assertRaises(TypeError, operator.ge, 1j, 2j)
00086         self.assertTrue(operator.ge(1, 0))
00087         self.assertTrue(operator.ge(1, 0.0))
00088         self.assertTrue(operator.ge(1, 1))
00089         self.assertTrue(operator.ge(1, 1.0))
00090         self.assertFalse(operator.ge(1, 2))
00091         self.assertFalse(operator.ge(1, 2.0))

Here is the call graph for this function:

Definition at line 150 of file test_operator.py.

00150 
00151     def test_getitem(self):
00152         a = range(10)
00153         self.assertRaises(TypeError, operator.getitem)
00154         self.assertRaises(TypeError, operator.getitem, a, None)
00155         self.assertTrue(operator.getitem(a, 2) == 2)

Here is the call graph for this function:

Definition at line 92 of file test_operator.py.

00092 
00093     def test_gt(self):
00094         self.assertRaises(TypeError, operator.gt)
00095         self.assertRaises(TypeError, operator.gt, 1j, 2j)
00096         self.assertTrue(operator.gt(1, 0))
00097         self.assertTrue(operator.gt(1, 0.0))
00098         self.assertFalse(operator.gt(1, 1))
00099         self.assertFalse(operator.gt(1, 1.0))
00100         self.assertFalse(operator.gt(1, 2))
00101         self.assertFalse(operator.gt(1, 2.0))

Here is the call graph for this function:

Definition at line 156 of file test_operator.py.

00156 
00157     def test_indexOf(self):
00158         self.assertRaises(TypeError, operator.indexOf)
00159         self.assertRaises(TypeError, operator.indexOf, None, None)
00160         self.assertTrue(operator.indexOf([4, 3, 2, 1], 3) == 1)
00161         self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)

Here is the call graph for this function:

Definition at line 370 of file test_operator.py.

00370 
00371     def test_inplace(self):
00372         class C(object):
00373             def __iadd__     (self, other): return "iadd"
00374             def __iand__     (self, other): return "iand"
00375             def __ifloordiv__(self, other): return "ifloordiv"
00376             def __ilshift__  (self, other): return "ilshift"
00377             def __imod__     (self, other): return "imod"
00378             def __imul__     (self, other): return "imul"
00379             def __ior__      (self, other): return "ior"
00380             def __ipow__     (self, other): return "ipow"
00381             def __irshift__  (self, other): return "irshift"
00382             def __isub__     (self, other): return "isub"
00383             def __itruediv__ (self, other): return "itruediv"
00384             def __ixor__     (self, other): return "ixor"
00385             def __getitem__(self, other): return 5  # so that C is a sequence
00386         c = C()
00387         self.assertEqual(operator.iadd     (c, 5), "iadd")
00388         self.assertEqual(operator.iand     (c, 5), "iand")
00389         self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
00390         self.assertEqual(operator.ilshift  (c, 5), "ilshift")
00391         self.assertEqual(operator.imod     (c, 5), "imod")
00392         self.assertEqual(operator.imul     (c, 5), "imul")
00393         self.assertEqual(operator.ior      (c, 5), "ior")
00394         self.assertEqual(operator.ipow     (c, 5), "ipow")
00395         self.assertEqual(operator.irshift  (c, 5), "irshift")
00396         self.assertEqual(operator.isub     (c, 5), "isub")
00397         self.assertEqual(operator.itruediv (c, 5), "itruediv")
00398         self.assertEqual(operator.ixor     (c, 5), "ixor")
00399         self.assertEqual(operator.iconcat  (c, c), "iadd")
00400         self.assertEqual(operator.__iadd__     (c, 5), "iadd")
00401         self.assertEqual(operator.__iand__     (c, 5), "iand")
00402         self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
00403         self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
00404         self.assertEqual(operator.__imod__     (c, 5), "imod")
00405         self.assertEqual(operator.__imul__     (c, 5), "imul")
00406         self.assertEqual(operator.__ior__      (c, 5), "ior")
00407         self.assertEqual(operator.__ipow__     (c, 5), "ipow")
00408         self.assertEqual(operator.__irshift__  (c, 5), "irshift")
00409         self.assertEqual(operator.__isub__     (c, 5), "isub")
00410         self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
00411         self.assertEqual(operator.__ixor__     (c, 5), "ixor")
00412         self.assertEqual(operator.__iconcat__  (c, c), "iadd")

Here is the call graph for this function:

Definition at line 162 of file test_operator.py.

00162 
00163     def test_invert(self):
00164         self.assertRaises(TypeError, operator.invert)
00165         self.assertRaises(TypeError, operator.invert, None)
00166         self.assertEqual(operator.inv(4), -5)

Here is the call graph for this function:

Definition at line 255 of file test_operator.py.

00255 
00256     def test_is(self):
00257         a = b = 'xyzpdq'
00258         c = a[:3] + b[3:]
00259         self.assertRaises(TypeError, operator.is_)
00260         self.assertTrue(operator.is_(a, b))
00261         self.assertFalse(operator.is_(a,c))

Here is the call graph for this function:

Definition at line 262 of file test_operator.py.

00262 
00263     def test_is_not(self):
00264         a = b = 'xyzpdq'
00265         c = a[:3] + b[3:]
00266         self.assertRaises(TypeError, operator.is_not)
00267         self.assertFalse(operator.is_not(a, b))
00268         self.assertTrue(operator.is_not(a,c))

Here is the call graph for this function:

Definition at line 318 of file test_operator.py.

00318 
00319     def test_itemgetter(self):
00320         a = 'ABCDE'
00321         f = operator.itemgetter(2)
00322         self.assertEqual(f(a), 'C')
00323         f = operator.itemgetter(10)
00324         self.assertRaises(IndexError, f, a)
00325 
00326         class C(object):
00327             def __getitem__(self, name):
00328                 raise SyntaxError
00329         self.assertRaises(SyntaxError, operator.itemgetter(42), C())
00330 
00331         f = operator.itemgetter('name')
00332         self.assertRaises(TypeError, f, a)
00333         self.assertRaises(TypeError, operator.itemgetter)
00334 
00335         d = dict(key='val')
00336         f = operator.itemgetter('key')
00337         self.assertEqual(f(d), 'val')
00338         f = operator.itemgetter('nonkey')
00339         self.assertRaises(KeyError, f, d)
00340 
00341         # example used in the docs
00342         inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
00343         getcount = operator.itemgetter(1)
00344         self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1])
00345         self.assertEqual(sorted(inventory, key=getcount),
00346             [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)])
00347 
00348         # multiple gets
00349         data = list(map(str, range(20)))
00350         self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5'))
00351         self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data)

Here is the call graph for this function:

Definition at line 46 of file test_operator.py.

00046 
00047     def test_le(self):
00048         self.assertRaises(TypeError, operator.le)
00049         self.assertRaises(TypeError, operator.le, 1j, 2j)
00050         self.assertFalse(operator.le(1, 0))
00051         self.assertFalse(operator.le(1, 0.0))
00052         self.assertTrue(operator.le(1, 1))
00053         self.assertTrue(operator.le(1, 1.0))
00054         self.assertTrue(operator.le(1, 2))
00055         self.assertTrue(operator.le(1, 2.0))

Here is the call graph for this function:

Definition at line 167 of file test_operator.py.

00167 
00168     def test_lshift(self):
00169         self.assertRaises(TypeError, operator.lshift)
00170         self.assertRaises(TypeError, operator.lshift, None, 42)
00171         self.assertTrue(operator.lshift(5, 1) == 10)
00172         self.assertTrue(operator.lshift(5, 0) == 5)
00173         self.assertRaises(ValueError, operator.lshift, 2, -1)

Here is the call graph for this function:

Definition at line 36 of file test_operator.py.

00036 
00037     def test_lt(self):
00038         self.assertRaises(TypeError, operator.lt)
00039         self.assertRaises(TypeError, operator.lt, 1j, 2j)
00040         self.assertFalse(operator.lt(1, 0))
00041         self.assertFalse(operator.lt(1, 0.0))
00042         self.assertFalse(operator.lt(1, 1))
00043         self.assertFalse(operator.lt(1, 1.0))
00044         self.assertTrue(operator.lt(1, 2))
00045         self.assertTrue(operator.lt(1, 2.0))

Here is the call graph for this function:

Definition at line 352 of file test_operator.py.

00352 
00353     def test_methodcaller(self):
00354         self.assertRaises(TypeError, operator.methodcaller)
00355         class A:
00356             def foo(self, *args, **kwds):
00357                 return args[0] + args[1]
00358             def bar(self, f=42):
00359                 return f
00360         a = A()
00361         f = operator.methodcaller('foo')
00362         self.assertRaises(IndexError, f, a)
00363         f = operator.methodcaller('foo', 1, 2)
00364         self.assertEqual(f(a), 3)
00365         f = operator.methodcaller('bar')
00366         self.assertEqual(f(a), 42)
00367         self.assertRaises(TypeError, f, a, a)
00368         f = operator.methodcaller('bar', f=5)
00369         self.assertEqual(f(a), 5)

Here is the call graph for this function:

Definition at line 174 of file test_operator.py.

00174 
00175     def test_mod(self):
00176         self.assertRaises(TypeError, operator.mod)
00177         self.assertRaises(TypeError, operator.mod, None, 42)
00178         self.assertTrue(operator.mod(5, 2) == 1)

Here is the call graph for this function:

Definition at line 179 of file test_operator.py.

00179 
00180     def test_mul(self):
00181         self.assertRaises(TypeError, operator.mul)
00182         self.assertRaises(TypeError, operator.mul, None, None)
00183         self.assertTrue(operator.mul(5, 2) == 10)

Here is the call graph for this function:

Definition at line 69 of file test_operator.py.

00069 
00070     def test_ne(self):
00071         class C(object):
00072             def __ne__(self, other):
00073                 raise SyntaxError
00074         self.assertRaises(TypeError, operator.ne)
00075         self.assertRaises(SyntaxError, operator.ne, C(), C())
00076         self.assertTrue(operator.ne(1, 0))
00077         self.assertTrue(operator.ne(1, 0.0))
00078         self.assertFalse(operator.ne(1, 1))
00079         self.assertFalse(operator.ne(1, 1.0))
00080         self.assertTrue(operator.ne(1, 2))
00081         self.assertTrue(operator.ne(1, 2.0))

Here is the call graph for this function:

Definition at line 184 of file test_operator.py.

00184 
00185     def test_neg(self):
00186         self.assertRaises(TypeError, operator.neg)
00187         self.assertRaises(TypeError, operator.neg, None)
00188         self.assertEqual(operator.neg(5), -5)
00189         self.assertEqual(operator.neg(-5), 5)
00190         self.assertEqual(operator.neg(0), 0)
00191         self.assertEqual(operator.neg(-0), 0)

Here is the call graph for this function:

Definition at line 197 of file test_operator.py.

00197 
00198     def test_pos(self):
00199         self.assertRaises(TypeError, operator.pos)
00200         self.assertRaises(TypeError, operator.pos, None)
00201         self.assertEqual(operator.pos(5), 5)
00202         self.assertEqual(operator.pos(-5), -5)
00203         self.assertEqual(operator.pos(0), 0)
00204         self.assertEqual(operator.pos(-0), 0)

Here is the call graph for this function:

Definition at line 205 of file test_operator.py.

00205 
00206     def test_pow(self):
00207         self.assertRaises(TypeError, operator.pow)
00208         self.assertRaises(TypeError, operator.pow, None, None)
00209         self.assertEqual(operator.pow(3,5), 3**5)
00210         self.assertEqual(operator.__pow__(3,5), 3**5)
00211         self.assertRaises(TypeError, operator.pow, 1)
00212         self.assertRaises(TypeError, operator.pow, 1, 2, 3)

Here is the call graph for this function:

Definition at line 213 of file test_operator.py.

00213 
00214     def test_rshift(self):
00215         self.assertRaises(TypeError, operator.rshift)
00216         self.assertRaises(TypeError, operator.rshift, None, 42)
00217         self.assertTrue(operator.rshift(5, 1) == 2)
00218         self.assertTrue(operator.rshift(5, 0) == 5)
00219         self.assertRaises(ValueError, operator.rshift, 2, -1)

Here is the call graph for this function:

Definition at line 226 of file test_operator.py.

00226 
00227     def test_setitem(self):
00228         a = list(range(3))
00229         self.assertRaises(TypeError, operator.setitem, a)
00230         self.assertRaises(TypeError, operator.setitem, a, None, None)
00231         self.assertTrue(operator.setitem(a, 0, 2) is None)
00232         self.assertTrue(a == [2, 1, 2])
00233         self.assertRaises(IndexError, operator.setitem, a, 4, 2)

Here is the call graph for this function:

Definition at line 234 of file test_operator.py.

00234 
00235     def test_sub(self):
00236         self.assertRaises(TypeError, operator.sub)
00237         self.assertRaises(TypeError, operator.sub, None, None)
00238         self.assertTrue(operator.sub(5, 2) == 3)

Here is the call graph for this function:

Definition at line 145 of file test_operator.py.

00145 
00146     def test_truediv(self):
00147         self.assertRaises(TypeError, operator.truediv, 5)
00148         self.assertRaises(TypeError, operator.truediv, None, None)
00149         self.assertTrue(operator.truediv(5, 2) == 2.5)

Here is the call graph for this function:

Definition at line 239 of file test_operator.py.

00239 
00240     def test_truth(self):
00241         class C(object):
00242             def __bool__(self):
00243                 raise SyntaxError
00244         self.assertRaises(TypeError, operator.truth)
00245         self.assertRaises(SyntaxError, operator.truth, C())
00246         self.assertTrue(operator.truth(5))
00247         self.assertTrue(operator.truth([0]))
00248         self.assertFalse(operator.truth(0))
00249         self.assertFalse(operator.truth([]))

Here is the call graph for this function:


The documentation for this class was generated from the following file: