# Path: mockito/mockito.py
# def when(obj, strict=True):
#     """Central interface to stub functions on a given `obj`
# 
#     `obj` should be a module, a class or an instance of a class; it can be
#     a Dummy you created with :func:`mock`. ``when`` exposes a fluent interface
#     where you configure a stub in three steps::
# 
#         when(<obj>).<method_name>(<args>).thenReturn(<value>)
# 
#     Compared to simple *patching*, stubbing in mockito requires you to specify
#     conrete `args` for which the stub will answer with a concrete `<value>`.
#     All invocations that do not match this specific call signature will be
#     rejected. They usually throw at call time.
# 
#     Stubbing in mockito's sense thus means not only to get rid of unwanted
#     side effects, but effectively to turn function calls into constants.
# 
#     E.g.::
# 
#         # Given ``dog`` is an instance of a ``Dog``
#         when(dog).bark('Grrr').thenReturn('Wuff')
#         when(dog).bark('Miau').thenRaise(TypeError())
# 
#         # With this configuration set up:
#         assert dog.bark('Grrr') == 'Wuff'
#         dog.bark('Miau')  # will throw TypeError
#         dog.bark('Wuff')  # will throw unwanted interaction
# 
#     Stubbing can effectively be used as monkeypatching; usage shown with
#     the `with` context managing::
# 
#         with when(os.path).exists('/foo').thenReturn(True):
#             ...
# 
#     Most of the time verifying your interactions is not necessary, because
#     your code under tests implicitly verifies the return value by evaluating
#     it. See :func:`verify` if you need to, see also :func:`expect` to setup
#     expected call counts up front.
# 
#     If your function is pure side effect and does not return something, you
#     can omit the specific answer. The default then is `None`::
# 
#         when(manager).do_work()
# 
#     `when` verifies the method name, the expected argument signature, and the
#     actual, factual arguments your code under test uses against the original
#     object and its function so its easier to spot changing interfaces.
# 
#     Sometimes it's tedious to spell out all arguments::
# 
#         from mockito import ANY, ARGS, KWARGS
#         when(requests).get('http://example.com/', **KWARGS).thenReturn(...)
#         when(os.path).exists(ANY)
#         when(os.path).exists(ANY(str))
# 
#     .. note:: You must :func:`unstub` after stubbing, or use `with`
#         statement.
# 
#     Set ``strict=False`` to bypass the function signature checks.
# 
#     See related :func:`when2` which has a more pythonic interface.
# 
#     """
# 
#     if isinstance(obj, str):
#         obj = get_obj(obj)
# 
#     theMock = _get_mock(obj, strict=strict)
# 
#     class When(object):
#         def __getattr__(self, method_name):
#             return invocation.StubbedInvocation(
#                 theMock, method_name, strict=strict)
# 
#     return When()
# 
# def unstub(*objs):
#     """Unstubs all stubbed methods and functions
# 
#     If you don't pass in any argument, *all* registered mocks and
#     patched modules, classes etc. will be unstubbed.
# 
#     Note that additionally, the underlying registry will be cleaned.
#     After an `unstub` you can't :func:`verify` anymore because all
#     interactions will be forgotten.
#     """
# 
#     if objs:
#         for obj in objs:
#             mock_registry.unstub(obj)
#     else:
#         mock_registry.unstub_all()
# 
# def verify(obj, times=1, atleast=None, atmost=None, between=None,
#            inorder=False):
#     """Central interface to verify interactions.
# 
#     `verify` uses a fluent interface::
# 
#         verify(<obj>, times=2).<method_name>(<args>)
# 
#     `args` can be as concrete as necessary. Often a catch-all is enough,
#     especially if you're working with strict mocks, bc they throw at call
#     time on unwanted, unconfigured arguments::
# 
#         from mockito import ANY, ARGS, KWARGS
#         when(manager).add_tasks(1, 2, 3)
#         ...
#         # no need to duplicate the specification; every other argument pattern
#         # would have raised anyway.
#         verify(manager).add_tasks(1, 2, 3)  # duplicates `when`call
#         verify(manager).add_tasks(*ARGS)
#         verify(manager).add_tasks(...)       # Py3
#         verify(manager).add_tasks(Ellipsis)  # Py2
# 
#     """
# 
#     if isinstance(obj, str):
#         obj = get_obj(obj)
# 
#     verification_fn = _get_wanted_verification(
#         times=times, atleast=atleast, atmost=atmost, between=between)
#     if inorder:
#         verification_fn = verification.InOrder(verification_fn)
# 
#     theMock = _get_mock_or_raise(obj)
# 
#     class Verify(object):
#         def __getattr__(self, method_name):
#             return invocation.VerifiableInvocation(
#                 theMock, method_name, verification_fn)
# 
#     return Verify()
# 
# class ArgumentError(Exception):
#     pass


import pytest
from mockito import when, unstub, verify, ArgumentError

    def testNesting(self):
        # That's not a real test, I just wanted to see how it would
        # look like; bc you run out of space pretty quickly
        rex = Dog()
        mox = Dog()

        with when(rex).waggle().thenReturn('Yup'):
            with when(mox).waggle().thenReturn('Nope'):
                assert rex.waggle() == 'Yup'

        assert rex.waggle() == 'Unsure'
        assert mox.waggle() == 'Unsure'
        # though that's a good looking option
        with when(rex).waggle().thenReturn('Yup'), \
             when(mox).waggle().thenReturn('Nope'):  # noqa: E127
            assert rex.waggle() == 'Yup'
            assert mox.waggle() == 'Nope'

        assert rex.waggle() == 'Unsure'
        assert mox.waggle() == 'Unsure'

    def testOnlyUnstubTheExactStub(self):
        rex = Dog()

        when(rex).bark('Shhh').thenReturn('Nope')
        with when(rex).bark('Miau').thenReturn('Grrr'):
            assert rex.bark('Miau') == 'Grrr'

        assert rex.bark('Shhh') == 'Nope'

# Path: mockito/mockito.py
# def when(obj, strict=True):
#     """Central interface to stub functions on a given `obj`
# 
#     `obj` should be a module, a class or an instance of a class; it can be
#     a Dummy you created with :func:`mock`. ``when`` exposes a fluent interface
#     where you configure a stub in three steps::
# 
#         when(<obj>).<method_name>(<args>).thenReturn(<value>)
# 
#     Compared to simple *patching*, stubbing in mockito requires you to specify
#     conrete `args` for which the stub will answer with a concrete `<value>`.
#     All invocations that do not match this specific call signature will be
#     rejected. They usually throw at call time.
# 
#     Stubbing in mockito's sense thus means not only to get rid of unwanted
#     side effects, but effectively to turn function calls into constants.
# 
#     E.g.::
# 
#         # Given ``dog`` is an instance of a ``Dog``
#         when(dog).bark('Grrr').thenReturn('Wuff')
#         when(dog).bark('Miau').thenRaise(TypeError())
# 
#         # With this configuration set up:
#         assert dog.bark('Grrr') == 'Wuff'
#         dog.bark('Miau')  # will throw TypeError
#         dog.bark('Wuff')  # will throw unwanted interaction
# 
#     Stubbing can effectively be used as monkeypatching; usage shown with
#     the `with` context managing::
# 
#         with when(os.path).exists('/foo').thenReturn(True):
#             ...
# 
#     Most of the time verifying your interactions is not necessary, because
#     your code under tests implicitly verifies the return value by evaluating
#     it. See :func:`verify` if you need to, see also :func:`expect` to setup
#     expected call counts up front.
# 
#     If your function is pure side effect and does not return something, you
#     can omit the specific answer. The default then is `None`::
# 
#         when(manager).do_work()
# 
#     `when` verifies the method name, the expected argument signature, and the
#     actual, factual arguments your code under test uses against the original
#     object and its function so its easier to spot changing interfaces.
# 
#     Sometimes it's tedious to spell out all arguments::
# 
#         from mockito import ANY, ARGS, KWARGS
#         when(requests).get('http://example.com/', **KWARGS).thenReturn(...)
#         when(os.path).exists(ANY)
#         when(os.path).exists(ANY(str))
# 
#     .. note:: You must :func:`unstub` after stubbing, or use `with`
#         statement.
# 
#     Set ``strict=False`` to bypass the function signature checks.
# 
#     See related :func:`when2` which has a more pythonic interface.
# 
#     """
# 
#     if isinstance(obj, str):
#         obj = get_obj(obj)
# 
#     theMock = _get_mock(obj, strict=strict)
# 
#     class When(object):
#         def __getattr__(self, method_name):
#             return invocation.StubbedInvocation(
#                 theMock, method_name, strict=strict)
# 
#     return When()
# 
# def unstub(*objs):
#     """Unstubs all stubbed methods and functions
# 
#     If you don't pass in any argument, *all* registered mocks and
#     patched modules, classes etc. will be unstubbed.
# 
#     Note that additionally, the underlying registry will be cleaned.
#     After an `unstub` you can't :func:`verify` anymore because all
#     interactions will be forgotten.
#     """
# 
#     if objs:
#         for obj in objs:
#             mock_registry.unstub(obj)
#     else:
#         mock_registry.unstub_all()
# 
# def verify(obj, times=1, atleast=None, atmost=None, between=None,
#            inorder=False):
#     """Central interface to verify interactions.
# 
#     `verify` uses a fluent interface::
# 
#         verify(<obj>, times=2).<method_name>(<args>)
# 
#     `args` can be as concrete as necessary. Often a catch-all is enough,
#     especially if you're working with strict mocks, bc they throw at call
#     time on unwanted, unconfigured arguments::
# 
#         from mockito import ANY, ARGS, KWARGS
#         when(manager).add_tasks(1, 2, 3)
#         ...
#         # no need to duplicate the specification; every other argument pattern
#         # would have raised anyway.
#         verify(manager).add_tasks(1, 2, 3)  # duplicates `when`call
#         verify(manager).add_tasks(*ARGS)
#         verify(manager).add_tasks(...)       # Py3
#         verify(manager).add_tasks(Ellipsis)  # Py2
# 
#     """
# 
#     if isinstance(obj, str):
#         obj = get_obj(obj)
# 
#     verification_fn = _get_wanted_verification(
#         times=times, atleast=atleast, atmost=atmost, between=between)
#     if inorder:
#         verification_fn = verification.InOrder(verification_fn)
# 
#     theMock = _get_mock_or_raise(obj)
# 
#     class Verify(object):
#         def __getattr__(self, method_name):
#             return invocation.VerifiableInvocation(
#                 theMock, method_name, verification_fn)
# 
#     return Verify()
# 
# class ArgumentError(Exception):
#     pass


# Path: tests/unstub_test.py
import pytest
from mockito import when, unstub, verify, ArgumentError


    def testNesting(self):
        # That's not a real test, I just wanted to see how it would
        # look like; bc you run out of space pretty quickly
        rex = Dog()
        mox = Dog()

        with when(rex).waggle().thenReturn('Yup'):
            with when(mox).waggle().thenReturn('Nope'):
                assert rex.waggle() == 'Yup'

        assert rex.waggle() == 'Unsure'
        assert mox.waggle() == 'Unsure'
        # though that's a good looking option
        with when(rex).waggle().thenReturn('Yup'), \
             when(mox).waggle().thenReturn('Nope'):  # noqa: E127
            assert rex.waggle() == 'Yup'
            assert mox.waggle() == 'Nope'

        assert rex.waggle() == 'Unsure'
        assert mox.waggle() == 'Unsure'

    def testOnlyUnstubTheExactStub(self):
        rex = Dog()

        when(rex).bark('Shhh').thenReturn('Nope')
        with when(rex).bark('Miau').thenReturn('Grrr'):
            assert rex.bark('Miau') == 'Grrr'

        assert rex.bark('Shhh') == 'Nope'

        verify(rex, times=2).bark(Ellipsis)