def testWeakSet(self): weak_set = WeakSet() s1 = _Stub() s2 = _Stub() weak_set.add(s1) assert isinstance(iter(weak_set).next(), _Stub) assert s1 in weak_set self.CustomAssertEqual(len(weak_set), 1) del s1 self.CustomAssertEqual(len(weak_set), 0)
def testFunction(self): weak_set = WeakSet() def function(): "Never called" # Adding twice, having one. weak_set.add(function) weak_set.add(function) self.CustomAssertEqual(len(weak_set), 1) # Removing function weak_set.remove(function) assert len(weak_set) == 0
def testRemove(self): weak_set = WeakSet() s1 = _Stub() self.CustomAssertEqual(len(weak_set), 0) # Trying remove, raises KeyError with pytest.raises(KeyError): weak_set.remove(s1) self.CustomAssertEqual(len(weak_set), 0) # Trying discard, no exception raised weak_set.discard(s1) self.CustomAssertEqual(len(weak_set), 0)
def testWithError(self): weak_set = WeakSet() # Not WITH, everything ok s1 = _Stub() weak_set.add(s1.Method) self.CustomAssertEqual(len(weak_set), 1) del s1 self.CustomAssertEqual(len(weak_set), 0) # Using WITH, s2 is not deleted from weak_set s2 = _Stub() with pytest.raises(KeyError): raise KeyError("key") self.CustomAssertEqual(len(weak_set), 0) weak_set.add(s2.Method) self.CustomAssertEqual(len(weak_set), 1) del s2 self.CustomAssertEqual(len(weak_set), 0)
def testWeakSet2(self): weak_set = WeakSet() # >>> Removing with DEL s1 = _Stub() weak_set.add(s1.Method) self.CustomAssertEqual(len(weak_set), 1) del s1 self.CustomAssertEqual(len(weak_set), 0) # >>> Removing with REMOVE s2 = _Stub() weak_set.add(s2.Method) self.CustomAssertEqual(len(weak_set), 1) weak_set.remove(s2.Method) self.CustomAssertEqual(len(weak_set), 0)
def testFunction(self): weak_set = WeakSet() def function(): 'Never called' # Adding twice, having one. weak_set.add(function) weak_set.add(function) self.CustomAssertEqual(len(weak_set), 1) # Removing function weak_set.remove(function) assert len(weak_set) == 0
def testWithError(self): weak_set = WeakSet() # Not WITH, everything ok s1 = _Stub() weak_set.add(s1.Method) self.CustomAssertEqual(len(weak_set), 1) del s1 self.CustomAssertEqual(len(weak_set), 0) # Using WITH, s2 is not deleted from weak_set s2 = _Stub() with pytest.raises(KeyError): raise KeyError('key') self.CustomAssertEqual(len(weak_set), 0) weak_set.add(s2.Method) self.CustomAssertEqual(len(weak_set), 1) del s2 self.CustomAssertEqual(len(weak_set), 0)