# Path: mockito/mockito.py
# def when2(fn, *args, **kwargs):
#     """Stub a function call with the given arguments
# 
#     Exposes a more pythonic interface than :func:`when`. See :func:`when` for
#     more documentation.
# 
#     Returns `AnswerSelector` interface which exposes `thenReturn`,
#     `thenRaise`, and `thenAnswer` as usual. Always `strict`.
# 
#     Usage::
# 
#         # Given `dog` is an instance of a `Dog`
#         when2(dog.bark, 'Miau').thenReturn('Wuff')
# 
#     .. note:: You must :func:`unstub` after stubbing, or use `with`
#         statement.
# 
#     """
#     obj, name = get_obj_attr_tuple(fn)
#     theMock = _get_mock(obj, strict=True)
#     return invocation.StubbedInvocation(theMock, name)(*args, **kwargs)
# 
# def patch(fn, attr_or_replacement, replacement=None):
#     """Patch/Replace a function.
# 
#     This is really like monkeypatching, but *note* that all interactions
#     will be recorded and can be verified. That is, using `patch` you stay in
#     the domain of mockito.
# 
#     Two ways to call this. Either::
# 
#         patch(os.path.exists, lambda str: True)  # two arguments
#         # OR
#         patch(os.path, 'exists', lambda str: True)  # three arguments
# 
#     If called with three arguments, the mode is *not* strict to allow *adding*
#     methods. If called with two arguments, mode is always `strict`.
# 
#     .. note:: You must :func:`unstub` after stubbing, or use `with`
#         statement.
# 
#     """
#     if replacement is None:
#         replacement = attr_or_replacement
#         return when2(fn, Ellipsis).thenAnswer(replacement)
#     else:
#         obj, name = fn, attr_or_replacement
#         theMock = _get_mock(obj, strict=True)
#         return invocation.StubbedInvocation(
#             theMock, name, strict=False)(Ellipsis).thenAnswer(replacement)
# 
# 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()
# 
# Path: mockito/spying.py
# def spy2(fn):  # type: (...) -> None
#     """Spy usage of given `fn`.
# 
#     Patches the module, class or object `fn` lives in, so that all
#     interactions can be recorded; otherwise executes `fn` as before, so
#     that all side effects happen as before.
# 
#     E.g.::
# 
#         import time
#         spy2(time.time)
#         do_work(...)  # nothing injected, uses global patched `time` module
#         verify(time).time()
# 
#     Note that builtins often cannot be patched because they're read-only.
# 
# 
#     """
#     if isinstance(fn, str):
#         answer = get_obj(fn)
#     else:
#         answer = fn
# 
#     when2(fn, Ellipsis).thenAnswer(answer)
# 
# Path: mockito/utils.py
# def newmethod(fn, obj):
#     return types.MethodType(fn, obj)


import pytest
import os
        import time
from mockito import when2, patch, spy2, verify
from mockito.utils import newmethod
        assert rex.bark('Miau') == 'Miau!'


    def testPatch2(self):
        rex = Dog()
        patch(rex.bark, rex.bark_hard)
        assert rex.bark('Miau') == 'Miau!'

    def testPatch3(self):
        rex = Dog()

        def f(self, sound):
            return self.bark_hard(sound)

        f = newmethod(f, rex)
        patch(rex.bark, f)

        assert rex.bark('Miau') == 'Miau!'

    def testAddFnWithPatch(self):
        rex = Dog()

        patch(rex, 'newfn', lambda s: s)
        assert rex.newfn('Hi') == 'Hi'


class TestFancyObjResolver:
    def testWhen2WithArguments(self):
        # This test is a bit flaky bc pytest does not like a patched
        # `os.path.exists` module.

# Path: mockito/mockito.py
# def when2(fn, *args, **kwargs):
#     """Stub a function call with the given arguments
# 
#     Exposes a more pythonic interface than :func:`when`. See :func:`when` for
#     more documentation.
# 
#     Returns `AnswerSelector` interface which exposes `thenReturn`,
#     `thenRaise`, and `thenAnswer` as usual. Always `strict`.
# 
#     Usage::
# 
#         # Given `dog` is an instance of a `Dog`
#         when2(dog.bark, 'Miau').thenReturn('Wuff')
# 
#     .. note:: You must :func:`unstub` after stubbing, or use `with`
#         statement.
# 
#     """
#     obj, name = get_obj_attr_tuple(fn)
#     theMock = _get_mock(obj, strict=True)
#     return invocation.StubbedInvocation(theMock, name)(*args, **kwargs)
# 
# def patch(fn, attr_or_replacement, replacement=None):
#     """Patch/Replace a function.
# 
#     This is really like monkeypatching, but *note* that all interactions
#     will be recorded and can be verified. That is, using `patch` you stay in
#     the domain of mockito.
# 
#     Two ways to call this. Either::
# 
#         patch(os.path.exists, lambda str: True)  # two arguments
#         # OR
#         patch(os.path, 'exists', lambda str: True)  # three arguments
# 
#     If called with three arguments, the mode is *not* strict to allow *adding*
#     methods. If called with two arguments, mode is always `strict`.
# 
#     .. note:: You must :func:`unstub` after stubbing, or use `with`
#         statement.
# 
#     """
#     if replacement is None:
#         replacement = attr_or_replacement
#         return when2(fn, Ellipsis).thenAnswer(replacement)
#     else:
#         obj, name = fn, attr_or_replacement
#         theMock = _get_mock(obj, strict=True)
#         return invocation.StubbedInvocation(
#             theMock, name, strict=False)(Ellipsis).thenAnswer(replacement)
# 
# 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()
# 
# Path: mockito/spying.py
# def spy2(fn):  # type: (...) -> None
#     """Spy usage of given `fn`.
# 
#     Patches the module, class or object `fn` lives in, so that all
#     interactions can be recorded; otherwise executes `fn` as before, so
#     that all side effects happen as before.
# 
#     E.g.::
# 
#         import time
#         spy2(time.time)
#         do_work(...)  # nothing injected, uses global patched `time` module
#         verify(time).time()
# 
#     Note that builtins often cannot be patched because they're read-only.
# 
# 
#     """
#     if isinstance(fn, str):
#         answer = get_obj(fn)
#     else:
#         answer = fn
# 
#     when2(fn, Ellipsis).thenAnswer(answer)
# 
# Path: mockito/utils.py
# def newmethod(fn, obj):
#     return types.MethodType(fn, obj)


# Path: tests/when2_test.py
import pytest
import os
        import time
from mockito import when2, patch, spy2, verify
from mockito.utils import newmethod

        assert rex.bark('Miau') == 'Miau!'


    def testPatch2(self):
        rex = Dog()
        patch(rex.bark, rex.bark_hard)
        assert rex.bark('Miau') == 'Miau!'

    def testPatch3(self):
        rex = Dog()

        def f(self, sound):
            return self.bark_hard(sound)

        f = newmethod(f, rex)
        patch(rex.bark, f)

        assert rex.bark('Miau') == 'Miau!'

    def testAddFnWithPatch(self):
        rex = Dog()

        patch(rex, 'newfn', lambda s: s)
        assert rex.newfn('Hi') == 'Hi'


class TestFancyObjResolver:
    def testWhen2WithArguments(self):
        # This test is a bit flaky bc pytest does not like a patched
        # `os.path.exists` module.

        when2(os.path.commonprefix, '/Foo').thenReturn(False)