Skip to content

Commit

Permalink
Removed _get_original from SinonStub
Browse files Browse the repository at this point in the history
  * Removed recursion and simplified readability
  * SinonStubCondition now starts with
    underscore ("_") to show that it is package
    private and not for end-user consumption
  * Renamed __copy_of to _copy (less awkward).
    Also, after removing _get_original it was
    no longer possible for __copy_of to remain
    private.
  • Loading branch information
jonathan-benn-copilot authored and note35 committed Nov 15, 2017
1 parent 0b7e17f commit bc701e7
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions sinon/lib/stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, obj=None, prop=None, func=None):
super(SinonStub, self).__init__(obj, prop)
self._stubfunc = func if func else Wrapper.empty_function
super(SinonStub, self).wrap2stub(self._stubfunc)
self._cond_args = self._cond_kwargs = self._oncall = None
self._copy = self._cond_args = self._cond_kwargs = self._oncall = None
# Todo: target is a dirty hack
self._conditions = {"args":[], "kwargs":[], "action": [], "oncall": [], "target": self.obj}

Expand All @@ -33,7 +33,7 @@ def _append_condition(self, sinon_stub_condition, func):
Permanently saves the current (volatile) conditions, which would be otherwise lost
Args:
sinon_stub_condition: the SinonStubCondition object that holds the current conditions
sinon_stub_condition: the _SinonStubCondition object that holds the current conditions
func: returns a value or raises an exception, as specified by the user
Returns: the SinonStub._conditions dictionary (for convenience)
'''
Expand All @@ -43,12 +43,6 @@ def _append_condition(self, sinon_stub_condition, func):
self._conditions["action"].append(func)
return self._conditions

def _get_original(self):
"""
Returns the original SinonStub object that wrapped the function under test
"""
return self

def withArgs(self, *args, **kwargs): #pylint: disable=invalid-name
"""
Adds a condition for when the stub is called. When the condition is met, a special
Expand All @@ -66,7 +60,8 @@ def withArgs(self, *args, **kwargs): #pylint: disable=invalid-name
"""
cond_args = args if len(args) > 0 else None
cond_kwargs = kwargs if len(kwargs) > 0 else None
return SinonStubCondition(copy_of=self, cond_args=cond_args, cond_kwargs=cond_kwargs, oncall=self._oncall)
copy = self if not self._copy else self._copy
return _SinonStubCondition(copy=copy, cond_args=cond_args, cond_kwargs=cond_kwargs, oncall=self._oncall)

def onCall(self, n): #pylint: disable=invalid-name
"""
Expand All @@ -89,7 +84,8 @@ def onCall(self, n): #pylint: disable=invalid-name
a SinonStub object (able to be chained)
"""
cond_oncall = n + 1
return SinonStubCondition(copy_of=self, oncall=cond_oncall, cond_args=self._cond_args, cond_kwargs=self._cond_kwargs)
copy = self if not self._copy else self._copy
return _SinonStubCondition(copy=copy, oncall=cond_oncall, cond_args=self._cond_args, cond_kwargs=self._cond_kwargs)

def onFirstCall(self): #pylint: disable=invalid-name
"""
Expand Down Expand Up @@ -133,7 +129,7 @@ def exception_function(*args, **kwargs):
super(SinonStub, self).wrap2stub(exception_function)
return self

class SinonStubCondition(SinonStub):
class _SinonStubCondition(SinonStub):
"""
Allows a new SinonStub object to be created each time the end user specifies a new condition. This is necessary
to mimic the behaviour of Sinon.JS.
Expand All @@ -147,25 +143,19 @@ def __new__(cls, *args, **kwargs):
"""
return object.__new__(cls)

def __init__(self, copy_of, cond_args, cond_kwargs, oncall):
def __init__(self, copy, cond_args, cond_kwargs, oncall):
"""
Args:
copy_of: the original SinonStub object that spawned this one
copy: the original SinonStub object that spawned this one
cond_args: the args to which a subsequent call to returns/throws should apply
cond_kwargs: the kwargs to which a subsequent call to returns/throws should apply
oncall: the integer call number to which a subsequent call to returns/throws should apply
"""
self.__copy_of = copy_of
self._copy = copy
self._cond_args = cond_args
self._cond_kwargs = cond_kwargs
self._oncall = oncall

def _get_original(self):
"""
Returns the original SinonStub object that wrapped the function under test
"""
return self.__copy_of._get_original()

def returns(self, obj):
"""
Customizes the return values of the stub function. If conditions like withArgs or onCall
Expand All @@ -174,9 +164,8 @@ def returns(self, obj):
Args: obj (anything)
Return: a SinonStub object (able to be chained)
"""
original = self._get_original()
conditions = original._append_condition(self, lambda *args, **kwargs: obj)
super(SinonStub, original).wrap2stub(original._stubfunc, conditions)
conditions = self._copy._append_condition(self, lambda *args, **kwargs: obj)
super(SinonStub, self._copy).wrap2stub(self._copy._stubfunc, conditions)
return self

def throws(self, exception=Exception):
Expand All @@ -189,7 +178,6 @@ def throws(self, exception=Exception):
"""
def exception_function(*args, **kwargs):
raise exception
original = self._get_original()
conditions = original._append_condition(self, exception_function)
super(SinonStub, original).wrap2stub(original._stubfunc, conditions)
conditions = self._copy._append_condition(self, exception_function)
super(SinonStub, self._copy).wrap2stub(self._copy._stubfunc, conditions)
return self

0 comments on commit bc701e7

Please sign in to comment.