Permalink
Browse files

fixed bug when combining explicit kwargs with a wildcard kwarg matcher

  • Loading branch information...
1 parent 5b0a3e9 commit 24aef2af02b5a058ce05162848511eb6bc5f87f7 @timbertson committed Mar 5, 2011
Showing with 21 additions and 5 deletions.
  1. +16 −2 mocktest/mocking.py
  2. +5 −3 test/mocking_test.py
View
@@ -2,6 +2,7 @@
from mockerror import MockError
from callrecord import Call
from transaction import MockTransaction
+import itertools
from lib.singletonclass import ensure_singleton_class
__unittest = True
@@ -11,6 +12,7 @@
'expect',
'mock',
'modify',
+ 'Object',
]
def when(obj):
@@ -442,8 +444,20 @@ def check_args(a, args):
return True
def check_kwargs(k, kwargs):
- if kwargs.keys() == ['__kwargs']:
- return kwargs['__kwargs'].matches(k)
+ kwargs = kwargs.copy()
+ wildcard_kwargs_matcher = kwargs.pop('__kwargs', None)
+ if wildcard_kwargs_matcher is not None:
+ def splitdict(d, keys):
+ included, others = {}, {}
+ for k, v in d.items():
+ if k in keys:
+ included[k] = v
+ else:
+ others[k] = v
+ return included, others
+
+ explicit_kwargs, wildcard_kwargs = splitdict(k, kwargs.keys())
+ return check_kwargs(explicit_kwargs, kwargs) and wildcard_kwargs_matcher.matches(wildcard_kwargs)
if not len(k) == len(kwargs):
return False
View
@@ -374,9 +374,6 @@ def mock_expecting(self, *a, **k):
def test_splat_args(self):
self.assertTrue(self.mock_expecting(*any_args)._satisfied_by([Call.like(1,2,3)]))
- print repr(type(args_containing(1)))
- print repr(type(*args_containing(1)))
- print "-0-"
self.assertTrue(self.mock_expecting(*args_containing(1))._satisfied_by([Call.like(1,2,3)]))
self.assertTrue(self.mock_expecting(*args_containing(1))._satisfied_by([Call.like(2,1,3)]))
self.assertFalse(self.mock_expecting(*args_containing(1))._satisfied_by([Call.like(2,2,3)]))
@@ -387,5 +384,10 @@ def test_splat_kwargs(self):
self.assertTrue(self.mock_expecting(**any_kwargs)._satisfied_by([Call.like(x=1)]))
self.assertTrue(self.mock_expecting(**kwargs_containing(x=1))._satisfied_by([Call.like(x=1, y=2)]))
self.assertFalse(self.mock_expecting(**kwargs_containing(x=1))._satisfied_by([Call.like(y=2)]))
+
+ def test_matching_subset_of_kwargs(self):
+ self.assertTrue(self.mock_expecting(x=1, **any_kwargs)._satisfied_by([Call.like(x=1, y=2)]))
+ self.assertFalse(self.mock_expecting(x=1, **any_kwargs)._satisfied_by([Call.like(x=2, y=2)]))
+

0 comments on commit 24aef2a

Please sign in to comment.