summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2011-06-06 18:36:37 +0000
committersmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2011-06-06 18:36:37 +0000
commit6014b468c07df11d7f44840e8322c06fc1e46d8b (patch)
tree8f5c2c91806bcd14245f8d408a7c7f5c13f28d01
parent9011fcf628196660fda01dc4d1ffc23b5315dfe0 (diff)
downloadmox-6014b468c07df11d7f44840e8322c06fc1e46d8b.tar.gz
git-svn-id: http://pymox.googlecode.com/svn/trunk@62 b1010a0a-674b-0410-b734-77272b80c875
-rwxr-xr-xmox.py5
-rwxr-xr-xmox_test.py56
2 files changed, 57 insertions, 4 deletions
diff --git a/mox.py b/mox.py
index e4535a6..eb0717d 100755
--- a/mox.py
+++ b/mox.py
@@ -1245,10 +1245,7 @@ class Comparator:
raise NotImplementedError, 'method must be implemented by a subclass.'
def __eq__(self, rhs):
- try:
- return self.equals(rhs)
- except Exception:
- return False
+ return self.equals(rhs)
def __ne__(self, rhs):
return not self.equals(rhs)
diff --git a/mox_test.py b/mox_test.py
index a18b0f5..7b23397 100755
--- a/mox_test.py
+++ b/mox_test.py
@@ -101,6 +101,35 @@ class AndTest(unittest.TestCase):
self.failIf(mox.And(mox.In("NOTFOUND"),
mox.ContainsKeyValue("mock", "obj")) == test_dict)
+class FuncTest(unittest.TestCase):
+ """Test Func correctly evaluates based upon true-false return."""
+
+ def testFuncTrueFalseEvaluation(self):
+ """Should return True if the validating function returns True."""
+ equals_one = lambda x: x == 1
+ always_none = lambda x: None
+
+ self.assert_(mox.Func(equals_one) == 1)
+ self.failIf(mox.Func(equals_one) == 0)
+
+
+ self.failIf(mox.Func(always_none) == 1)
+ self.failIf(mox.Func(always_none) == 0)
+ self.failIf(mox.Func(always_none) == None)
+
+ def testFuncExceptionPropagation(self):
+ """Exceptions within the validating function should propagate."""
+ class TestException(Exception):
+ pass
+
+ def raiseExceptionOnNotOne(value):
+ if value != 1:
+ raise TestException
+ else:
+ return True
+
+ self.assert_(mox.Func(raiseExceptionOnNotOne) == 1)
+ self.assertRaises(TestException, mox.Func(raiseExceptionOnNotOne).__eq__, 2)
class SameElementsAsTest(unittest.TestCase):
"""Test SameElementsAs correctly identifies sequences with same elements."""
@@ -1715,6 +1744,33 @@ class MoxTest(unittest.TestCase):
self.mox.VerifyAll()
self.mox.UnsetStubs()
+ def testStubOutMethod_Func_PropgatesExceptions(self):
+ """Errors in a Func comparator should propagate to the calling method."""
+ class TestException(Exception):
+ pass
+
+ def raiseExceptionOnNotOne(value):
+ if value == 1:
+ return True
+ else:
+ raise TestException
+
+ test_obj = TestClass()
+ self.mox.StubOutWithMock(test_obj, 'MethodWithArgs')
+ test_obj.MethodWithArgs(
+ mox.IgnoreArg(), mox.Func(raiseExceptionOnNotOne)).AndReturn(1)
+ test_obj.MethodWithArgs(
+ mox.IgnoreArg(), mox.Func(raiseExceptionOnNotOne)).AndReturn(1)
+ self.mox.ReplayAll()
+
+ self.assertEqual(test_obj.MethodWithArgs('ignored', 1), 1)
+ self.assertRaises(TestException,
+ test_obj.MethodWithArgs, 'ignored', 2)
+
+ self.mox.VerifyAll()
+ self.mox.UnsetStubs()
+
+
def testStubOut_SignatureMatching_init_(self):
self.mox.StubOutWithMock(mox_test_helper.ExampleClass, '__init__')
mox_test_helper.ExampleClass.__init__(mox.IgnoreArg())