summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstevepm@google.com <stevepm@google.com@b1010a0a-674b-0410-b734-77272b80c875>2011-06-28 14:27:07 +0000
committerstevepm@google.com <stevepm@google.com@b1010a0a-674b-0410-b734-77272b80c875>2011-06-28 14:27:07 +0000
commit367d3cce17112d73abf71f3aea25221a4ee9adf5 (patch)
tree137cabada24c0fd176d05820354732fb8ac1a12e
parenteb3a1a971937497f6980495e7f4ff65403b78d13 (diff)
downloadmox-367d3cce17112d73abf71f3aea25221a4ee9adf5.tar.gz
Fix for broken logic when doing method signature matching and the
first argument is a Comparator. The Comparator may raise an exception (which is expected), and since exceptions aren't caught, this will break well formed tests. git-svn-id: http://pymox.googlecode.com/svn/trunk@66 b1010a0a-674b-0410-b734-77272b80c875
-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)