def testRecord(self): """Verify recording of a new action""" c = Controller() action = BaseAction('x') c.record(action) self.failUnless(c.actions.contains(action)) self.failUnless(c.actionUnderConstruction == action)
def testSettingClassMethod(self): c = Controller() class MethodTarget(object): def m(self): pass mt = MethodTarget() c.override(mt, 'm', lambda(x): x)
def testPlayModeAndSwitches(self): """Verify that the replay switch works""" c = Controller() self.failUnless(c.isRecording) self.failIf(c.isPlayingBack) c.replay() self.failIf(c.isRecording) self.failUnless(c.isPlayingBack)
def testSetCount(self): """Verify that settting the count works""" c = Controller() a = BaseAction('x') c.record(a) self.failUnless(a.playbackPolicy.remaining == 1) c.setCount(2) self.failUnless(a.playbackPolicy.remaining == 2)
def testUnusedAttributeAssignmentCanBeVerified(self): """Ensure that unused calls cause verification failure""" c = Controller() x = c.mock(KlassBeingMocked) x.w = 5 c.replay() self.failUnlessRaises(RecordedCallsWereNotReplayedCorrectly, c.verify)
def testPlayback(self): """Ensure that the playback operates correctly""" c = Controller() action = BaseAction('x') c.actions.append(action) c.playback(action) self.failIf(c.actions.contains(action)) self.failUnless(action.playbackPolicy.hasBeenPlayedBack) self.failUnless(action.playbackPolicy.isReadyForRemoval)
def testOverrideOfOneItem(self): """Verify override stores and replays""" c = Controller() x = KlassBeingMocked() x.f = 38 c.override(x, 'f', 5) self.failUnless(x.f == 5) c.restore() self.failUnless(x.f == 38)
def testAttributeAssignmentCanBePlayedBack(self): """Attibute assignment should be intercepted""" c = Controller() x = c.mock(KlassBeingMocked) x.w = 5 x.w = 6 c.replay() x.w = 5
def testUndefinedPlaybackRaisesException(self): """Playback failure should raise an exception""" c = Controller() x = c.mock(KlassBeingMocked) c.replay() try: x.w = 5 self.fail() except PlaybackFailure, e: pass
def testSetAttributeRecordsField(self): """Ensure setting an attribute is correctly recorded""" c = Controller() x = c.mock(KlassBeingMocked) x.w = 5 self.failUnless(len(c.actions) == 1) action = c.actions.get(BaseAction((x,'w'))) self.failUnless(action.field == 'w') self.failUnless(action.value == 5)
def testFunctionCallWithMismatchedArguments(self): """Function call with mismatched arguments""" c = Controller() x = c.mock(KlassBeingMocked) x.g(3, 4) x.h() x.i(8, v1=4, v2=3) c.replay() self.failUnlessRaises(PlaybackFailure, x.g, 5, 3) self.failUnlessRaises(PlaybackFailure, x.h, 2) self.failUnlessRaises(PlaybackFailure, x.i, 8, v1=4, v2=5)
def testSetCountOneOrMoreRaisesErrorOnNextRecord(self): """Setting an unlimited count causes subsequent records to fail""" c = Controller() x = c.mock(KlassBeingMocked) x.w = 5 c.oneOrMore() try: x.w = 6 self.fail() except IllegalPlaybackRecorded, e: pass
def testGetattrWithNoReturnValueSpecified(self): """Getattr with no return value specified""" c = Controller() x = c.mock(KlassBeingMocked) x.g c.replay() self.failIf(x.g == None) try: x.g self.fail() except PlaybackFailure, e: pass
def testAttributeAssignmentIsIntercepted(self): """Attibute assignment should be intercepted""" c = Controller() x = c.mock(KlassBeingMocked) x.w = 5 c.replay() try: x.w self.fail() except PlaybackFailure, e: pass
def testPlaybackFailsWithIncorrectValues(self): """Ensure playing back incorrect values results in an error""" c = Controller() x = c.mock(KlassBeingMocked) x.w = 5 c.replay() try: x.w = 4 self.fail() except PlaybackFailure, e: pass
def testExplicitGeneratorExecptionUsage(self): """Check exception raising with explicit generators using discrete settings""" c = Controller() x = c.mock() x.g(8, 9) c.generator() c.setReturn(10) c.setException(Exception("bogus")) c.replay() g = x.g(8, 9) self.failUnless(g.next() == 10) self.failUnlessRaises(Exception, g.next)
def testOverrideOfTwoItems(self): """Verify override stores and replays for multiple items""" c = Controller() x = KlassBeingMocked() y = KlassBeingMocked() x.f = 38 y.g = 39 c.override(x, 'f', 5) c.override(y, 'g', 6) self.failUnless(x.f == 5) self.failUnless(y.g == 6) c.restore() self.failUnless(x.f == 38) self.failUnless(y.g == 39)
def testObjectWithProxiedClass(self): c = Controller() class ClassToClone(object): def m(self): pass x = ClassToClone() y = ClassToClone() c.attachObjectToProxyClass(y) y.__class__.f = lambda x: None try: x.f() self.fail() except AttributeError, e: pass
def testProxyClassForObject(self): c = Controller() class ClassToClone(object): def m(self): pass x = ClassToClone() ClonedClass = c.proxyClassForObject(x) ClonedClass.f = ClonedClass.m y = ClonedClass() x.m() try: x.f() self.fail() except AttributeError, e: pass
def testExpectAndReturn(self): """Check expect and return""" c = Controller() x = c.mock() c.expectAndReturn(x.g(8, 9), 5) c.replay() self.failUnless(x.g(8, 9) == 5) c.verify()
def testGetItem(self): """Ensure that __getitem__ works""" c = Controller() x = c.mock(KlassBeingMocked) x.g[5] c.setReturn(6) c.replay() self.failUnless(x.g[5] == 6) c.verify()
def testLen(self): """Ensure that __len__ works""" c = Controller() x = c.mock(KlassBeingMocked) k = len(x) c.setReturn(5) c.replay() self.failUnless(len(x) == 5) c.verify()
def testOverrideWithImplicitMock(self): """Verify override stores and replays""" c = Controller() x = KlassBeingMocked() x.f = 38 c.override(x, 'f') x.f(35) c.replay() self.failUnlessRaises(Exception, c.verify) x.f(35) c.verify() c.restore() self.failUnless(x.f == 38)
def testExplicitGeneratorUsage(self): """Check operation of explicit generators using discrete settings""" c = Controller() x = c.mock() x.g(8, 9) c.generator() c.setReturn(10) c.setReturn(11) c.replay() self.failUnless([k for k in x.g(8, 9)] == [10, 11])
def testPlaybackWithMultipleCounts(self): """Perform playback with multiple values""" c = Controller() action = BaseAction('x') c.record(action) c.setCount(2) self.failIf(action.playbackPolicy.hasBeenPlayedBack) self.failIf(action.playbackPolicy.isReadyForRemoval) c.playback(action) self.failIf(action.playbackPolicy.hasBeenPlayedBack) self.failIf(action.playbackPolicy.isReadyForRemoval) c.playback(action) self.failUnless(action.playbackPolicy.hasBeenPlayedBack) self.failUnless(action.playbackPolicy.isReadyForRemoval)
def testIdentifierSequenceCreation(self): """Verify that identifiers are correctly created""" c = Controller() m = c.mock(KlassBeingMocked) expectedKeys = Set() m.g = 2 expectedKeys.add((m, 'g')) self.failUnless(expectedKeys == self.actions(c)) m.g.h expectedKeys.add((m, 'g', 'h')) self.failUnless(expectedKeys == self.actions(c)) m.g() expectedKeys.add((m, 'g', '()')) self.failUnless(expectedKeys == self.actions(c)) m.g().i() expectedKeys.add((m, 'g', '()', 'i')) expectedKeys.add((m, 'g', '()', 'i', '()')) self.failUnless(expectedKeys == self.actions(c))
def test__foo__MethodsWorkWithSyntacticalFormAndMethodCallInterchangably(self): """Ensure that __foo__ methods work with their special syntax forms interchangaly""" c = Controller() x = c.mock(KlassBeingMocked) x.g.__getitem__(5) c.setReturn(6) x.h.__getitem__(7) c.setReturn(8) c.replay() self.failUnless(x.g[5] == 6) self.failUnless(x.h.__getitem__(7) == 8)
def testEnsureThatContainsWorks(self): """Verify that __contains__ is implemented""" c = Controller() x = c.mock() 1 in x c.setReturn(True) 2 in x c.setReturn(False) c.replay() self.failUnless(1 in x) self.failIf(2 in x)
def testIterWithException(self): """Ensure that __iter__ records and replays exceptions""" c = Controller() x = c.mock() x.__iter__() c.setReturn(1) c.setException(Exception) c.replay() i = x.__iter__() self.failUnless(i.next() == 1) self.failUnlessRaises(Exception, i.next)
def testAtLeastSetsLimit(self): """Ensure that setattr returns function calls""" c = Controller() x = c.mock(KlassBeingMocked) x.g = 6 c.atLeast(2) c.replay() x.g = 6 self.failUnlessRaises(Exception, c.verify) x.g = 6 c.verify()