Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_class.ClassTests Class Reference

List of all members.

Public Member Functions

def setUp
def assertCallStack
def testInit
def testBinaryOps
def testListAndDictOps
def testUnaryOps
def testMisc
def testGetSetAndDel
def testDel
def testBadTypeReturned
def testHashStuff
def testSFBug532646
def testForExceptionsRaisedInInstanceGetattr2
def testHashComparisonOfMethods

Public Attributes

 x

Detailed Description

Definition at line 134 of file test_class.py.


Member Function Documentation

def test.test_class.ClassTests.assertCallStack (   self,
  expected_calls 
)

Definition at line 138 of file test_class.py.

00138 
00139     def assertCallStack(self, expected_calls):
00140         actualCallList = callLst[:]  # need to copy because the comparison below will add
00141                                      # additional calls to callLst
00142         if expected_calls != actualCallList:
00143             self.fail("Expected call list:\n  %s\ndoes not match actual call list\n  %s" %
00144                       (expected_calls, actualCallList))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 135 of file test_class.py.

00135 
00136     def setUp(self):
00137         callLst[:] = []

Here is the caller graph for this function:

Definition at line 441 of file test_class.py.

00441 
00442     def testBadTypeReturned(self):
00443         # return values of some method are type-checked
00444         class BadTypeClass:
00445             def __int__(self):
00446                 return None
00447             __float__ = __int__
00448             __str__ = __int__
00449             __repr__ = __int__
00450             __oct__ = __int__
00451             __hex__ = __int__
00452 
00453         for f in [int, float, str, repr, oct, hex]:
00454             self.assertRaises(TypeError, f, BadTypeClass())

Here is the call graph for this function:

Definition at line 149 of file test_class.py.

00149 
00150     def testBinaryOps(self):
00151         testme = AllTests()
00152         # Binary operations
00153 
00154         callLst[:] = []
00155         testme + 1
00156         self.assertCallStack([("__add__", (testme, 1))])
00157 
00158         callLst[:] = []
00159         1 + testme
00160         self.assertCallStack([("__radd__", (testme, 1))])
00161 
00162         callLst[:] = []
00163         testme - 1
00164         self.assertCallStack([("__sub__", (testme, 1))])
00165 
00166         callLst[:] = []
00167         1 - testme
00168         self.assertCallStack([("__rsub__", (testme, 1))])
00169 
00170         callLst[:] = []
00171         testme * 1
00172         self.assertCallStack([("__mul__", (testme, 1))])
00173 
00174         callLst[:] = []
00175         1 * testme
00176         self.assertCallStack([("__rmul__", (testme, 1))])
00177 
00178         if 1/2 == 0:
00179             callLst[:] = []
00180             testme / 1
00181             self.assertCallStack([("__div__", (testme, 1))])
00182 
00183 
00184             callLst[:] = []
00185             1 / testme
00186             self.assertCallStack([("__rdiv__", (testme, 1))])
00187 
00188         callLst[:] = []
00189         testme % 1
00190         self.assertCallStack([("__mod__", (testme, 1))])
00191 
00192         callLst[:] = []
00193         1 % testme
00194         self.assertCallStack([("__rmod__", (testme, 1))])
00195 
00196 
00197         callLst[:] = []
00198         divmod(testme,1)
00199         self.assertCallStack([("__divmod__", (testme, 1))])
00200 
00201         callLst[:] = []
00202         divmod(1, testme)
00203         self.assertCallStack([("__rdivmod__", (testme, 1))])
00204 
00205         callLst[:] = []
00206         testme ** 1
00207         self.assertCallStack([("__pow__", (testme, 1))])
00208 
00209         callLst[:] = []
00210         1 ** testme
00211         self.assertCallStack([("__rpow__", (testme, 1))])
00212 
00213         callLst[:] = []
00214         testme >> 1
00215         self.assertCallStack([("__rshift__", (testme, 1))])
00216 
00217         callLst[:] = []
00218         1 >> testme
00219         self.assertCallStack([("__rrshift__", (testme, 1))])
00220 
00221         callLst[:] = []
00222         testme << 1
00223         self.assertCallStack([("__lshift__", (testme, 1))])
00224 
00225         callLst[:] = []
00226         1 << testme
00227         self.assertCallStack([("__rlshift__", (testme, 1))])
00228 
00229         callLst[:] = []
00230         testme & 1
00231         self.assertCallStack([("__and__", (testme, 1))])
00232 
00233         callLst[:] = []
00234         1 & testme
00235         self.assertCallStack([("__rand__", (testme, 1))])
00236 
00237         callLst[:] = []
00238         testme | 1
00239         self.assertCallStack([("__or__", (testme, 1))])
00240 
00241         callLst[:] = []
00242         1 | testme
00243         self.assertCallStack([("__ror__", (testme, 1))])
00244 
00245         callLst[:] = []
00246         testme ^ 1
00247         self.assertCallStack([("__xor__", (testme, 1))])
00248 
00249         callLst[:] = []
00250         1 ^ testme
00251         self.assertCallStack([("__rxor__", (testme, 1))])

Here is the call graph for this function:

Definition at line 429 of file test_class.py.

00429 
00430     def testDel(self):
00431         x = []
00432 
00433         class DelTest:
00434             def __del__(self):
00435                 x.append("crab people, crab people")
00436         testme = DelTest()
00437         del testme
00438         import gc
00439         gc.collect()
00440         self.assertEqual(["crab people, crab people"], x)

Here is the call graph for this function:

Definition at line 485 of file test_class.py.

00485 
00486     def testForExceptionsRaisedInInstanceGetattr2(self):
00487         # Tests for exceptions raised in instance_getattr2().
00488 
00489         def booh(self):
00490             raise AttributeError("booh")
00491 
00492         class A:
00493             a = property(booh)
00494         try:
00495             A().a # Raised AttributeError: A instance has no attribute 'a'
00496         except AttributeError as x:
00497             if str(x) != "booh":
00498                 self.fail("attribute error for A().a got masked: %s" % x)
00499 
00500         class E:
00501             __eq__ = property(booh)
00502         E() == E() # In debug mode, caused a C-level assert() to fail
00503 
00504         class I:
00505             __init__ = property(booh)
00506         try:
00507             # In debug mode, printed XXX undetected error and
00508             #  raises AttributeError
00509             I()
00510         except AttributeError as x:
00511             pass
00512         else:
00513             self.fail("attribute error for I.__init__ got masked")

Here is the call graph for this function:

Definition at line 399 of file test_class.py.

00399 
00400     def testGetSetAndDel(self):
00401         # Interfering tests
00402         class ExtraTests(AllTests):
00403             @trackCall
00404             def __getattr__(self, *args):
00405                 return "SomeVal"
00406 
00407             @trackCall
00408             def __setattr__(self, *args):
00409                 pass
00410 
00411             @trackCall
00412             def __delattr__(self, *args):
00413                 pass
00414 
00415         testme = ExtraTests()
00416 
00417         callLst[:] = []
00418         testme.spam
00419         self.assertCallStack([('__getattr__', (testme, "spam"))])
00420 
00421         callLst[:] = []
00422         testme.eggs = "spam, spam, spam and ham"
00423         self.assertCallStack([('__setattr__', (testme, "eggs",
00424                                                "spam, spam, spam and ham"))])
00425 
00426         callLst[:] = []
00427         del testme.cardinal
00428         self.assertCallStack([('__delattr__', (testme, "cardinal"))])

Here is the call graph for this function:

Definition at line 514 of file test_class.py.

00514 
00515     def testHashComparisonOfMethods(self):
00516         # Test comparison and hash of methods
00517         class A:
00518             def __init__(self, x):
00519                 self.x = x
00520             def f(self):
00521                 pass
00522             def g(self):
00523                 pass
00524             def __eq__(self, other):
00525                 return self.x == other.x
00526             def __hash__(self):
00527                 return self.x
00528         class B(A):
00529             pass
00530 
00531         a1 = A(1)
00532         a2 = A(2)
00533         self.assertEqual(a1.f, a1.f)
00534         self.assertNotEqual(a1.f, a2.f)
00535         self.assertNotEqual(a1.f, a1.g)
00536         self.assertEqual(a1.f, A(1).f)
00537         self.assertEqual(hash(a1.f), hash(a1.f))
00538         self.assertEqual(hash(a1.f), hash(A(1).f))
00539 
00540         self.assertNotEqual(A.f, a1.f)
00541         self.assertNotEqual(A.f, A.g)
00542         self.assertEqual(B.f, A.f)
00543         self.assertEqual(hash(B.f), hash(A.f))
00544 
00545         # the following triggers a SystemError in 2.4
00546         a = A(hash(A.f)^(-1))
00547         hash(a.f)

Here is the call graph for this function:

Definition at line 455 of file test_class.py.

00455 
00456     def testHashStuff(self):
00457         # Test correct errors from hash() on objects with comparisons but
00458         #  no __hash__
00459 
00460         class C0:
00461             pass
00462 
00463         hash(C0()) # This should work; the next two should raise TypeError
00464 
00465         class C2:
00466             def __eq__(self, other): return 1
00467 
00468         self.assertRaises(TypeError, hash, C2())
00469 

Definition at line 145 of file test_class.py.

00145 
00146     def testInit(self):
00147         foo = AllTests()
00148         self.assertCallStack([("__init__", (foo,))])

Here is the call graph for this function:

Definition at line 252 of file test_class.py.

00252 
00253     def testListAndDictOps(self):
00254         testme = AllTests()
00255 
00256         # List/dict operations
00257 
00258         class Empty: pass
00259 
00260         try:
00261             1 in Empty()
00262             self.fail('failed, should have raised TypeError')
00263         except TypeError:
00264             pass
00265 
00266         callLst[:] = []
00267         1 in testme
00268         self.assertCallStack([('__contains__', (testme, 1))])
00269 
00270         callLst[:] = []
00271         testme[1]
00272         self.assertCallStack([('__getitem__', (testme, 1))])
00273 
00274         callLst[:] = []
00275         testme[1] = 1
00276         self.assertCallStack([('__setitem__', (testme, 1, 1))])
00277 
00278         callLst[:] = []
00279         del testme[1]
00280         self.assertCallStack([('__delitem__', (testme, 1))])
00281 
00282         callLst[:] = []
00283         testme[:42]
00284         self.assertCallStack([('__getitem__', (testme, slice(None, 42)))])
00285 
00286         callLst[:] = []
00287         testme[:42] = "The Answer"
00288         self.assertCallStack([('__setitem__', (testme, slice(None, 42),
00289                                                "The Answer"))])
00290 
00291         callLst[:] = []
00292         del testme[:42]
00293         self.assertCallStack([('__delitem__', (testme, slice(None, 42)))])
00294 
00295         callLst[:] = []
00296         testme[2:1024:10]
00297         self.assertCallStack([('__getitem__', (testme, slice(2, 1024, 10)))])
00298 
00299         callLst[:] = []
00300         testme[2:1024:10] = "A lot"
00301         self.assertCallStack([('__setitem__', (testme, slice(2, 1024, 10),
00302                                                                     "A lot"))])
00303         callLst[:] = []
00304         del testme[2:1024:10]
00305         self.assertCallStack([('__delitem__', (testme, slice(2, 1024, 10)))])
00306 
00307         callLst[:] = []
00308         testme[:42, ..., :24:, 24, 100]
00309         self.assertCallStack([('__getitem__', (testme, (slice(None, 42, None),
00310                                                         Ellipsis,
00311                                                         slice(None, 24, None),
00312                                                         24, 100)))])
00313         callLst[:] = []
00314         testme[:42, ..., :24:, 24, 100] = "Strange"
00315         self.assertCallStack([('__setitem__', (testme, (slice(None, 42, None),
00316                                                         Ellipsis,
00317                                                         slice(None, 24, None),
00318                                                         24, 100), "Strange"))])
00319         callLst[:] = []
00320         del testme[:42, ..., :24:, 24, 100]
00321         self.assertCallStack([('__delitem__', (testme, (slice(None, 42, None),
00322                                                         Ellipsis,
00323                                                         slice(None, 24, None),
00324                                                         24, 100)))])

Here is the call graph for this function:

Definition at line 351 of file test_class.py.

00351 
00352     def testMisc(self):
00353         testme = AllTests()
00354 
00355         callLst[:] = []
00356         hash(testme)
00357         self.assertCallStack([('__hash__', (testme,))])
00358 
00359         callLst[:] = []
00360         repr(testme)
00361         self.assertCallStack([('__repr__', (testme,))])
00362 
00363         callLst[:] = []
00364         str(testme)
00365         self.assertCallStack([('__str__', (testme,))])
00366 
00367         callLst[:] = []
00368         testme == 1
00369         self.assertCallStack([('__eq__', (testme, 1))])
00370 
00371         callLst[:] = []
00372         testme < 1
00373         self.assertCallStack([('__lt__', (testme, 1))])
00374 
00375         callLst[:] = []
00376         testme > 1
00377         self.assertCallStack([('__gt__', (testme, 1))])
00378 
00379         callLst[:] = []
00380         testme != 1
00381         self.assertCallStack([('__ne__', (testme, 1))])
00382 
00383         callLst[:] = []
00384         1 == testme
00385         self.assertCallStack([('__eq__', (1, testme))])
00386 
00387         callLst[:] = []
00388         1 < testme
00389         self.assertCallStack([('__gt__', (1, testme))])
00390 
00391         callLst[:] = []
00392         1 > testme
00393         self.assertCallStack([('__lt__', (1, testme))])
00394 
00395         callLst[:] = []
00396         1 != testme
00397         self.assertCallStack([('__ne__', (1, testme))])
00398 

Here is the call graph for this function:

Definition at line 470 of file test_class.py.

00470 
00471     def testSFBug532646(self):
00472         # Test for SF bug 532646
00473 
00474         class A:
00475             pass
00476         A.__call__ = A()
00477         a = A()
00478 
00479         try:
00480             a() # This should not segfault
00481         except RuntimeError:
00482             pass
00483         else:
00484             self.fail("Failed to raise RuntimeError")

Definition at line 325 of file test_class.py.

00325 
00326     def testUnaryOps(self):
00327         testme = AllTests()
00328 
00329         callLst[:] = []
00330         -testme
00331         self.assertCallStack([('__neg__', (testme,))])
00332         callLst[:] = []
00333         +testme
00334         self.assertCallStack([('__pos__', (testme,))])
00335         callLst[:] = []
00336         abs(testme)
00337         self.assertCallStack([('__abs__', (testme,))])
00338         callLst[:] = []
00339         int(testme)
00340         self.assertCallStack([('__int__', (testme,))])
00341         callLst[:] = []
00342         float(testme)
00343         self.assertCallStack([('__float__', (testme,))])
00344         callLst[:] = []
00345         oct(testme)
00346         self.assertCallStack([('__index__', (testme,))])
00347         callLst[:] = []
00348         hex(testme)
00349         self.assertCallStack([('__index__', (testme,))])
00350 

Here is the call graph for this function:


Member Data Documentation

Definition at line 518 of file test_class.py.


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