Skip to content

Commit

Permalink
Fixed API & implementation of firstCall to lastCall
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
jonathan-benn-copilot authored and note35 committed Oct 13, 2017
1 parent 1c95275 commit 1d799ea
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
29 changes: 21 additions & 8 deletions sinon/lib/spy.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,33 @@ def calledThrice(self): #pylint: disable=invalid-name,missing-docstring
return True if self.callCount == 3 else False

@property
def firstCall(self): #pylint: disable=invalid-name,missing-docstring
return True if 0 in self.get_callqueue_idx() else False
def firstCall(self): #pylint: disable=invalid-name
"""
Return: SpyCall object for the first time this spy was called
"""
return self.getCall(0)

@property
def secondCall(self): #pylint: disable=invalid-name,missing-docstring
return True if 1 in self.get_callqueue_idx() else False
def secondCall(self): #pylint: disable=invalid-name
"""
Return: SpyCall object for the second time this spy was called
"""
return self.getCall(1)

@property
def thirdCall(self): #pylint: disable=invalid-name,missing-docstring
return True if 2 in self.get_callqueue_idx() else False
def thirdCall(self): #pylint: disable=invalid-name
"""
Return: SpyCall object for the third time this spy was called
"""
return self.getCall(2)

@property
def lastCall(self): #pylint: disable=invalid-name,missing-docstring
return True if len(Wrapper.CALLQUEUE)-1 in self.get_callqueue_idx() else False
def lastCall(self): #pylint: disable=invalid-name
"""
Return: SpyCall object for this spy's most recent call
"""
lastIndex = len(self.__get_wrapper().call_list) - 1
return self.getCall(lastIndex)

def calledBefore(self, obj): #pylint: disable=invalid-name
"""
Expand Down
38 changes: 33 additions & 5 deletions sinon/test/TestSinonSpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test049_calledThrice_empty(self):
self.assertTrue(spy.calledThrice)

@sinontest
def test050_firstCall_secondCall_thirdCall_lastCall(self):
def test050_firstCall_callId(self):
spy1 = SinonSpy(os, "system")
spy2 = SinonSpy()
spy3 = SinonSpy(B_func)
Expand All @@ -128,10 +128,10 @@ def test050_firstCall_secondCall_thirdCall_lastCall(self):
spy2()
sinon.g.B_func()
spy4()
self.assertTrue(spy1.firstCall)
self.assertTrue(spy2.secondCall)
self.assertTrue(spy3.thirdCall)
self.assertTrue(spy4.lastCall)
self.assertEqual(spy1.firstCall.callId, 0)
self.assertEqual(spy2.firstCall.callId, 1)
self.assertEqual(spy3.firstCall.callId, 2)
self.assertEqual(spy4.firstCall.callId, 3)

@sinontest
def test051_calledBefore_calledAfter_normal(self):
Expand Down Expand Up @@ -856,6 +856,34 @@ def test240_neverCalledWithMatch_args(self):
self.assertTrue(spy.neverCalledWithMatch("a", "e"))
self.assertTrue(spy.neverCalledWithMatch("d", "e", "c")) #it's a combination

@sinontest
def test250_firstCall_to_lastCall_with_call(self):
spy = SinonSpy(E_func)
sinon.g.E_func(1, 2)
sinon.g.E_func(3, 4)
sinon.g.E_func(5, 6)
sinon.g.E_func(7, 8)
self.assertEqual(type(spy.firstCall).__name__, "SpyCall")
self.assertEqual(spy.firstCall.args, (1, 2))
self.assertEqual(type(spy.secondCall).__name__, "SpyCall")
self.assertEqual(spy.secondCall.args, (3, 4))
self.assertEqual(type(spy.thirdCall).__name__, "SpyCall")
self.assertEqual(spy.thirdCall.args, (5, 6))
self.assertEqual(type(spy.lastCall).__name__, "SpyCall")
self.assertEqual(spy.lastCall.args, (7, 8))

@sinontest
def test251_firstCall_to_lastCall_without_call(self):
spy = SinonSpy(C_func)
with self.assertRaises(IndexError):
spy.firstCall
with self.assertRaises(IndexError):
spy.secondCall
with self.assertRaises(IndexError):
spy.thirdCall
with self.assertRaises(IndexError):
spy.lastCall

@sinontest
def test260_getCall(self):
spy = SinonSpy(E_func)
Expand Down

0 comments on commit 1d799ea

Please sign in to comment.