Browse files

added tests for splatwise operators and cleaned up documentation

  • Loading branch information...
1 parent bafe7d8 commit 75813634c8d85a81f1990787c2ce2453049f481c @timbertson committed Feb 27, 2011
Showing with 48 additions and 10 deletions.
  1. +2 −0 mocktest/__init__.py
  2. +5 −7 mocktest/matchers/base.py
  3. +15 −1 mocktest/matchers/collections.py
  4. +3 −2 mocktest/mocking.py
  5. +23 −0 test/mocking_test.py
View
2 mocktest/__init__.py
@@ -14,8 +14,10 @@
instances are returned instances from other (public) methods:
.. autoclass:: mocktest.mocking.RecursiveStub
+ :members:
.. autoclass:: mocktest.mocking.RecursiveAssignmentWrapper
+ :members:
.. _setting-expectations:
View
12 mocktest/matchers/base.py
@@ -10,7 +10,7 @@
It returns a matcher for any instance of that type.
-.. data:: _any
+.. data:: any_
Alias for :data:`Any`
@@ -83,14 +83,14 @@ def matcher(matches, desc = 'anonymous matcher'):
class SplatMatcherMaker(Matcher):
def __init__(self, matcher):
self._matcher = matcher
+
+ def __iter__(self):
+ return iter((SplatMatcher(self._matcher),))
def matches(self, *a):
raise RuntimeError("SplatMatcher instance used without prefixing with '*'")
desc = matches
- def __iter__(self):
- return iter([SplatMatcher(self._matcher)])
-
class SplatMatcher(object):
def __init__(self, matcher):
self._matcher = matcher
@@ -136,11 +136,9 @@ def matches(self, *a):
raise RuntimeError("KwargsMatcher instance used without prefixing with '**'")
desc = matches
-kwargs_with = KwargsMatcher
-args_with = SplatMatcherMaker
-
Not = NegatedMatcher
not_ = NegatedMatcher
Any = AnyObject()
any_ = Any
+
View
16 mocktest/matchers/collections.py
@@ -10,6 +10,16 @@
Matches an object that includes all given elements.
+.. data:: any_args
+
+ like `any`, but used for a series of arguments - e.g.
+ foo(*any_args)
+
+.. data:: any_kwargs
+
+ like `any`, but used for a dict of keyword arguments - e.g.
+ foo(**any_kwargs)
+
.. function:: args_containing(*args)
Just like :func:`object_containing`, but used for matching args. E.g:
@@ -26,12 +36,14 @@
"""
-from base import Matcher, KwargsMatcher, SplatMatcherMaker
+from base import Matcher, KwargsMatcher, SplatMatcherMaker, Any
__all__ = [
'object_containing',
'dict_containing',
'kwargs_containing',
'args_containing',
+ 'any_kwargs',
+ 'any_args',
'any_of',
]
@@ -81,5 +93,7 @@ def desc(self):
kwargs_containing = lambda **k: KwargsMatcher(dict_containing(**k))
args_containing = lambda *a: SplatMatcherMaker(object_containing(*a))
+any_args = SplatMatcherMaker(Any)
+any_kwargs = KwargsMatcher(Any)
any_of = ItemMatcher
View
5 mocktest/mocking.py
@@ -115,7 +115,7 @@ def children(self, **children):
def methods(self, **methods):
"""
- Set children via kwargs, e.g.:
+ Set child methods via kwargs, e.g.:
>>> modify(obj).children(x=1, y=mock('child y'))
"""
@@ -129,7 +129,8 @@ def do_return(return_value):
def copying(self, other, value=lambda *a, **kw: None):
"""
Copy all non-special attributes of `other`, setting
- the value of each child to `value`.
+ the value of each child to `value`. The default `value` paramater
+ is a function returning `None` for all arguments.
"""
for attr in dir(other):
if _special_method(attr): continue
View
23 test/mocking_test.py
@@ -351,3 +351,26 @@ def test_that_ensures_setup_was_run(self):
assert result.wasSuccessful(), result.errors[0][1]
+from mocktest.mocking import MockAct
+from mocktest import args_containing, any_args, kwargs_containing, any_kwargs
+class TestSplatMatchers(TestCase):
+ def mock_expecting(self, *a, **k):
+ return MockAct('anonymous mock')(*a, **k).once()
+
+ 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)]))
+ self.assertFalse(self.mock_expecting(1, 2, *args_containing(1))._satisfied_by([Call.like(1,2,4,3)]))
+
+ def test_splat_kwargs(self):
+ self.assertTrue(self.mock_expecting(**any_kwargs)._satisfied_by([Call.like()]))
+ 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)]))
+
+

0 comments on commit 7581363

Please sign in to comment.