Skip to content

Commit

Permalink
- altered nomenclature for action to stub to follow jmock convention
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamcarlyle committed Jul 25, 2004
1 parent 106a174 commit 8b285ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
38 changes: 19 additions & 19 deletions src/pmock.py
Expand Up @@ -121,30 +121,30 @@ def __init__(self, invocation_matcher):
self._matchers = []
self._invocation_matcher = invocation_matcher
self._matchers.append(invocation_matcher)
self._action = None
self._stub = None
self._id = None

def __str__(self):
strs = ["%s: " % str(self._invocation_matcher)]
for matcher in self._matchers[1:]:
strs.append(str(matcher))
if self._action is not None:
strs.append(", %s" % self._action)
if self._stub is not None:
strs.append(", %s" % self._stub)
if self._id is not None:
strs.append(" [%s]" % self._id)
return "".join(strs)

def add_matcher(self, matcher):
self._matchers.append(matcher)

def set_action(self, action):
self._action = action
def set_stub(self, stub):
self._stub = stub

def invoke(self, invocation):
for matcher in self._matchers:
matcher.invoked(invocation)
if self._action is not None:
return self._action.invoke(invocation)
if self._stub is not None:
return self._stub.invoke(invocation)

def matches(self, invocation):
for matcher in self._matchers:
Expand Down Expand Up @@ -320,9 +320,9 @@ def no_args(self):
self._mocker.add_matcher(NO_ARGS_MATCHER)
return self

def will(self, action):
"""Set action when method is called."""
self._mocker.set_action(action)
def will(self, stub):
"""Set stub when method is called."""
self._mocker.set_stub(stub)
return self

def id(self, id_str):
Expand Down Expand Up @@ -510,10 +510,10 @@ def mock(self):


##############################################################################
# Mocked method actions
# Mocked method stubs
##############################################################################

class ReturnValueAction(object):
class ReturnValueStub(object):

def __init__(self, value):
self._value = value
Expand All @@ -526,14 +526,14 @@ def invoke(self, invocation):


def return_value(value):
"""Action that returns the supplied value.
"""Stub that returns the supplied value.
Convenience function for creating a L{ReturnValueAction} instance.
Convenience function for creating a L{ReturnValueStub} instance.
"""
return ReturnValueAction(value)
return ReturnValueStub(value)


class RaiseExceptionAction(object):
class RaiseExceptionStub(object):

def __init__(self, exception):
self._exception = exception
Expand All @@ -546,11 +546,11 @@ def invoke(self, invocation):


def raise_exception(exception):
"""Action that raises the supplied exception.
"""Stub that raises the supplied exception.
Convenience function for creating a L{RaiseExceptionAction} instance.
Convenience function for creating a L{RaiseExceptionStub} instance.
"""
return RaiseExceptionAction(exception)
return RaiseExceptionStub(exception)


##############################################################################
Expand Down
52 changes: 26 additions & 26 deletions src/unit_tests.py
Expand Up @@ -185,15 +185,15 @@ def test_matches_passes_invocation_to_matcher(self):
mocker.matches(invocation)
self.assertEqual(matcher.matches_invocation, invocation)

def test_no_action_returns_none(self):
def test_no_stub_returns_none(self):
mocker = pmock.InvocationMocker(self.MockMatcher(True))
self.assert_(mocker.invoke(pmock.Invocation("duck", (), {})) is None)

def test_invoke_returns_actions_value(self):
class MockAction:
def test_invoke_returns_stubs_value(self):
class MockStub:
def invoke(self, invocation): return 'value'
mocker = pmock.InvocationMocker(self.MockMatcher(True))
mocker.set_action(MockAction())
mocker.set_stub(MockStub())
self.assert_(mocker.invoke(pmock.Invocation("duck", (), {})) ==
'value')

Expand Down Expand Up @@ -238,14 +238,14 @@ def test_str(self):
class MockMatcher:
def __init__(self, str_str): self._str = str_str
def __str__(self): return self._str
MockAction = MockMatcher
MockStub = MockMatcher
mocker = pmock.InvocationMocker(MockMatcher("invocation_matcher"))
mocker.add_matcher(MockMatcher("added_matcher1"))
mocker.add_matcher(MockMatcher("added_matcher2"))
mocker.set_action(MockAction("action"))
mocker.set_stub(MockStub("stub"))
self.assertEqual(str(mocker),
"invocation_matcher: added_matcher1added_matcher2, "
"action")
"stub")

def test_no_id_str(self):
class MockMatcher:
Expand All @@ -259,13 +259,13 @@ def test_id_str(self):
class MockMatcher:
def __init__(self, str_str): self._str = str_str
def __str__(self): return self._str
MockAction = MockMatcher
MockStub = MockMatcher
mocker = pmock.InvocationMocker(MockMatcher("invocation_matcher"))
mocker.add_matcher(MockMatcher("added_matcher1"))
mocker.set_action(MockAction("action"))
mocker.set_stub(MockStub("stub"))
mocker.set_id("quack")
self.assertEqual(str(mocker),
"invocation_matcher: added_matcher1, action [quack]")
"invocation_matcher: added_matcher1, stub [quack]")


class InvocationMockerBuilderTest(testsupport.ErrorMsgAssertsMixin,
Expand All @@ -275,13 +275,13 @@ class MockInvocationMocker:
def __init__(self):
self.added_matchers = []
self.id = None
self.action = None
self.stub = None
def add_matcher(self, matcher):
self.added_matchers.append(matcher)
def set_id(self, mocker_id):
self.id = mocker_id
def set_action(self, action):
self.action = action
def set_stub(self, stub):
self.stub = stub
def _get_only_added_matcher(self):
if len(self.added_matchers) != 1:
raise AssertionError('more than one matcher has been added')
Expand Down Expand Up @@ -347,11 +347,11 @@ def test_add_any_args_matcher(self):
self.assert_(self.builder.any_args() is not None)
self.assertEqual(self.mocker.added_matcher, pmock.ANY_ARGS_MATCHER)

def test_set_will_action(self):
class MockAction: pass
action = MockAction()
self.assert_(self.builder.will(action) is not None)
self.assertEqual(self.mocker.action, action)
def test_set_will_stub(self):
class MockStub: pass
stub = MockStub()
self.assert_(self.builder.will(stub) is not None)
self.assertEqual(self.mocker.stub, stub)

def test_set_id(self):
self.assert_(self.builder.id("poultry") is not None)
Expand Down Expand Up @@ -724,37 +724,37 @@ def test_method(self):


##############################################################################
# Mocked method actions
# Mocked method stubs
##############################################################################

class ReturnValueTest(unittest.TestCase):

def setUp(self):
self.action = pmock.ReturnValueAction("owl")
self.stub = pmock.ReturnValueStub("owl")

def test_invoke(self):
self.assertEqual(self.action.invoke(pmock.Invocation("hoot", (), {})),
self.assertEqual(self.stub.invoke(pmock.Invocation("hoot", (), {})),
"owl")

def test_str(self):
self.assertEqual(str(self.action), "returns 'owl'")
self.assertEqual(str(self.stub), "returns 'owl'")


class RaiseExceptionAction(unittest.TestCase):
class RaiseExceptionStub(unittest.TestCase):

def setUp(self):
self.exception = RuntimeError("owl")
self.action = pmock.RaiseExceptionAction(self.exception)
self.stub = pmock.RaiseExceptionStub(self.exception)

def test_invoke(self):
try:
self.action.invoke(pmock.Invocation("hoot", (), {}))
self.stub.invoke(pmock.Invocation("hoot", (), {}))
self.fail("expected exception to be raised")
except RuntimeError, err:
self.assertEqual(err, self.exception)

def test_str(self):
self.assertEqual(str(self.action), "raises %s" % self.exception)
self.assertEqual(str(self.stub), "raises %s" % self.exception)


##############################################################################
Expand Down

0 comments on commit 8b285ff

Please sign in to comment.