def test_setIDFunction(self): """ L{util.setIDFunction} returns the last value passed to it. """ value = object() previous = util.setIDFunction(value) result = util.setIDFunction(previous) self.assertIdentical(value, result)
def test_unsignedID(self): """ L{util.unsignedID} uses the function passed to L{util.setIDFunction} to determine the unique integer id of an object and then adjusts it to be positive if necessary. """ foo = object() bar = object() # A fake object identity mapping objects = {foo: 17, bar: -73} def fakeId(obj): return objects[obj] util.setIDFunction(fakeId) self.assertEquals(util.unsignedID(foo), 17) self.assertEquals(util.unsignedID(bar), (sys.maxint + 1) * 2 - 73)
def test_unsignedID(self): """ L{util.unsignedID} uses the function passed to L{util.setIDFunction} to determine the unique integer id of an object and then adjusts it to be positive if necessary. """ foo = object() bar = object() # A fake object identity mapping objects = {foo: 17, bar: -73} def fakeId(obj): return objects[obj] util.setIDFunction(fakeId) self.assertEqual(util.unsignedID(foo), 17) self.assertEqual(util.unsignedID(bar), (sys.maxsize + 1) * 2 - 73)
def test_str(self): """ The string representation of a L{DelayedCall} instance, as returned by C{str}, includes the unsigned id of the instance, as well as its state, the function to be called, and the function arguments. """ dc = DelayedCall(12, nothing, (3, ), {"A": 5}, None, None, lambda: 1.5) ids = {dc: 200} def fakeID(obj): try: return ids[obj] except (TypeError, KeyError): return id(obj) self.addCleanup(setIDFunction, setIDFunction(fakeID)) self.assertEqual( str(dc), "<DelayedCall 0xc8 [10.5s] called=0 cancelled=0 nothing(3, A=5)>")
def test_unsignedID(self): """ L{unsignedID} is used to print ID of the object in case of error, not standard ID value which can be negative. """ class X(BTBase): breakRepr = True ids = {X: 100} def fakeID(obj): try: return ids[obj] except (TypeError, KeyError): return id(obj) self.addCleanup(util.setIDFunction, util.setIDFunction(fakeID)) xRepr = reflect.safe_repr(X) self.assertIn("0x64", xRepr)
def test_str(self): """ The string representation of a L{DelayedCall} instance, as returned by C{str}, includes the unsigned id of the instance, as well as its state, the function to be called, and the function arguments. """ def nothing(): pass dc = DelayedCall(12, nothing, (3, ), {"A": 5}, None, None, lambda: 1.5) ids = {dc: 200} def fakeID(obj): try: return ids[obj] except (TypeError, KeyError): return id(obj) self.addCleanup(setIDFunction, setIDFunction(fakeID)) self.assertEqual( str(dc), "<DelayedCall 0xc8 [10.5s] called=0 cancelled=0 nothing(3, A=5)>")