diff options
author | Gustavo Niemeyer <gustavo@niemeyer.net> | 2007-12-05 20:49:55 -0200 |
---|---|---|
committer | Gustavo Niemeyer <gustavo@niemeyer.net> | 2007-12-05 20:49:55 -0200 |
commit | b04e870828e81f0f519ad31eae43d1eb0091d692 (patch) | |
tree | 236d722852f5ce47a261a3460e04fe9d83c3aeba | |
parent | 5c7342b0fd9caf32d9b5d3077175e2bf21b7460f (diff) | |
download | mocker-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-- | NEWS | 3 | ||||
-rw-r--r-- | mocker.py | 12 | ||||
-rwxr-xr-x | test.py | 16 |
3 files changed, 28 insertions, 3 deletions
@@ -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)). @@ -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.""" @@ -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}) |