summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmox.py12
-rwxr-xr-xmox_test.py21
2 files changed, 32 insertions, 1 deletions
diff --git a/mox.py b/mox.py
index 5739ab1..2ffdf90 100755
--- a/mox.py
+++ b/mox.py
@@ -913,7 +913,17 @@ class MethodSignatureChecker(object):
# Check if the param is an instance of the expected class,
# or check equality (useful for checking Comparators).
- if isinstance(params[0], expected) or params[0] == expected:
+
+ # This is a hack to work around the fact that the first
+ # parameter can be a Comparator, and the comparison may raise
+ # an exception during this comparison, which is OK.
+ try:
+ param_equality = (params[0] == expected)
+ except:
+ param_equality = False;
+
+
+ if isinstance(params[0], expected) or param_equality:
params = params[1:]
# If the IsA() comparator is being used, we need to check the
# inverse of the usual case - that the given instance is a subclass
diff --git a/mox_test.py b/mox_test.py
index f6d5026..f865fb9 100755
--- a/mox_test.py
+++ b/mox_test.py
@@ -1282,6 +1282,27 @@ class MoxTest(unittest.TestCase):
self.assertEquals("yes", ret_val)
self.mox.VerifyAll()
+ def testSignatureMatchingWithComparatorAsFirstArg(self):
+ """Test that the first argument can be a comparator."""
+
+ def VerifyLen(val):
+ """This will raise an exception when not given a list.
+
+ This exception will be raised when trying to infer/validate the
+ method signature.
+ """
+ return len(val) != 1
+
+ mock_obj = self.mox.CreateMock(TestClass)
+ # This intentionally does not name the 'nine' param so it triggers
+ # deeper inspection.
+ mock_obj.MethodWithArgs(mox.Func(VerifyLen), mox.IgnoreArg(), None)
+ self.mox.ReplayAll()
+
+ mock_obj.MethodWithArgs([1, 2], "foo", None)
+
+ self.mox.VerifyAll()
+
def testCallableObject(self):
"""Test recording calls to a callable object works."""
mock_obj = self.mox.CreateMock(CallableClass)