summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2007-12-05 20:49:55 -0200
committerGustavo Niemeyer <gustavo@niemeyer.net>2007-12-05 20:49:55 -0200
commitb04e870828e81f0f519ad31eae43d1eb0091d692 (patch)
tree236d722852f5ce47a261a3460e04fe9d83c3aeba
parent5c7342b0fd9caf32d9b5d3077175e2bf21b7460f (diff)
downloadmocker-b04e870828e81f0f519ad31eae43d1eb0091d692.tar.gz
New MATCH() argument matcher, which allows using a function
to match an argument generically. E.g. MATCH(lambda x: x > 10)
-rw-r--r--NEWS3
-rw-r--r--mocker.py12
-rwxr-xr-xtest.py16
3 files changed, 28 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 78ffccf..10cb855 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,9 @@
- MockerTestCase now verifies if the mocker is put in replay
mode in cases where events were recorded.
+- New MATCH() argument matcher, which allows using a function
+ to match an argument generically. E.g. MATCH(lambda x: x > 10)
+
- Now when a spec is provided (or with proxy/replace/patch) the
existence of the real method is checked even if the mocked
method doesn't have to execute (e.g. due to count(0)).
diff --git a/mocker.py b/mocker.py
index e546266..ee19523 100644
--- a/mocker.py
+++ b/mocker.py
@@ -18,7 +18,8 @@ if sys.version_info < (2, 4):
from sets import Set as set # pragma: nocover
-__all__ = ["Mocker", "expect", "IS", "CONTAINS", "IN", "ANY", "ARGS", "KWARGS"]
+__all__ = ["Mocker", "expect", "IS", "CONTAINS", "IN", "MATCH",
+ "ANY", "ARGS", "KWARGS"]
__author__ = "Gustavo Niemeyer <gustavo@niemeyer.net>"
@@ -1350,6 +1351,15 @@ class IN(SpecialArgument):
return other in self.object
+class MATCH(SpecialArgument):
+
+ def matches(self, other):
+ return bool(self.object(other))
+
+ def __eq__(self, other):
+ return type(other) == type(self) and self.object is other.object
+
+
def match_params(args1, kwargs1, args2, kwargs2):
"""Match the two sets of parameters, considering special parameters."""
diff --git a/test.py b/test.py
index 3d27d54..a37977b 100755
--- a/test.py
+++ b/test.py
@@ -23,8 +23,8 @@ from mocker import \
PathMatcher, path_matcher_recorder, RunCounter, ImplicitRunCounter, \
run_counter_recorder, run_counter_removal_recorder, MockReturner, \
mock_returner_recorder, FunctionRunner, Orderer, SpecChecker, \
- spec_checker_recorder, match_params, ANY, IS, CONTAINS, IN, ARGS, KWARGS, \
- MatchError, PathExecuter, ProxyReplacer, Patcher, Undefined, \
+ spec_checker_recorder, match_params, ANY, IS, CONTAINS, IN, MATCH, ARGS, \
+ KWARGS, MatchError, PathExecuter, ProxyReplacer, Patcher, Undefined, \
PatchedMethod, MockerTestCase, ReplayRestoreEvent, OnRestoreCaller
@@ -2089,6 +2089,18 @@ class MatchParamsTest(TestCase):
self.assertFalse(IN([1]).matches([1]))
self.assertFalse(IN([1]).matches(object()))
+ def test_match_repr(self):
+ self.assertEquals(repr(MATCH("obj")), "MATCH('obj')")
+
+ def test_match_equals(self):
+ obj1, obj2 = [], []
+ self.assertEquals(MATCH(obj1), MATCH(obj1))
+ self.assertNotEquals(MATCH(obj1), MATCH(obj2))
+
+ def test_match_matches(self):
+ self.assertTrue(MATCH(lambda x: x > 10).matches(15))
+ self.assertFalse(MATCH(lambda x: x > 10).matches(5))
+
def test_normal(self):
self.true((), {}, (), {})
self.true((1, 2), {"a": 3}, (1, 2), {"a": 3})