Ejemplo n.º 1
0
	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)
Ejemplo n.º 2
0
	def testSettingClassMethod(self):
		c = Controller()
		class MethodTarget(object):
			def m(self):
				pass
		mt = MethodTarget()
		c.override(mt, 'm', lambda(x): x)
Ejemplo n.º 3
0
	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)
Ejemplo n.º 4
0
	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)
Ejemplo n.º 5
0
	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)
Ejemplo n.º 6
0
	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)
Ejemplo n.º 7
0
	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)
Ejemplo n.º 8
0
	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
Ejemplo n.º 9
0
	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
Ejemplo n.º 10
0
	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)
Ejemplo n.º 11
0
	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)
Ejemplo n.º 12
0
	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
Ejemplo n.º 13
0
	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
Ejemplo n.º 14
0
	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
Ejemplo n.º 15
0
	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
Ejemplo n.º 16
0
	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)
Ejemplo n.º 17
0
	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)
Ejemplo n.º 18
0
	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
Ejemplo n.º 19
0
	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
Ejemplo n.º 20
0
	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()
Ejemplo n.º 21
0
	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()
Ejemplo n.º 22
0
	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()
Ejemplo n.º 23
0
	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)
Ejemplo n.º 24
0
	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])
Ejemplo n.º 25
0
	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)
Ejemplo n.º 26
0
	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))
Ejemplo n.º 27
0
	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)
Ejemplo n.º 28
0
	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)
Ejemplo n.º 29
0
	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)
Ejemplo n.º 30
0
	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()