Exemplo n.º 1
0
def test_with_wraps_arguments():
    """
    Make sure it works for the slightly weirder case of a decorator that
    accepts arguments and in turn returns a decorator.
    """
    s = mock.sentinel
    mock_func = mock.Mock()
    mock_wrapper = mock.Mock(return_value=s.return_value)
    mock_decorator = mock.Mock()
    mock_decorator.__name__ = 'mock_decorator'
    mock_decorator.return_value = mock_wrapper
    mock_decorator_with_arguments = mock.Mock()
    mock_decorator_with_arguments.__name__ = 'mock_decorator_with_arguments'
    mock_decorator_with_arguments.return_value = mock_decorator

    wraps_decorator = with_wraps(arguments=True)
    decorator_generator = wraps_decorator(mock_decorator_with_arguments)
    decorator = decorator_generator(s.arg1, s.arg2, kwarg1=s.kwarg1,
                                    kwarg2=s.kwarg2)
    assert mock_decorator_with_arguments.call_args_list == [
        mock.call(s.arg1, s.arg2, kwarg1=s.kwarg1, kwarg2=s.kwarg2)
    ]

    wrapper = decorator(mock_func)
    assert mock_decorator.call_args_list == [mock.call(mock_func)]
    assert wrapper._wraps == mock_func

    return_value = wrapper(s.arg1, s.arg2, kwarg1=s.kwarg1, kwarg2=s.kwarg2)
    assert return_value == s.return_value
    assert mock_wrapper.call_args_list == [
        mock.call(s.arg1, s.arg2, kwarg1=s.kwarg1, kwarg2=s.kwarg2)
    ]
Exemplo n.º 2
0
def test_with_wraps():
    """Make sure it works for a typical decorator use case."""
    s = mock.sentinel
    mock_func = mock.Mock()
    mock_wrapper = mock.Mock(return_value=s.return_value)
    mock_decorator = mock.Mock()
    mock_decorator.__name__ = 'mock_decorator'
    mock_decorator.return_value = mock_wrapper

    decorator = with_wraps(mock_decorator)
    wrapper = decorator(mock_func)
    assert wrapper._wraps == mock_func

    return_value = wrapper(s.arg1, s.arg2, kwarg1=s.kwarg1, kwarg2=s.kwarg2)
    assert return_value == s.return_value
    assert mock_wrapper.call_args_list == [
        mock.call(s.arg1, s.arg2, kwarg1=s.kwarg1, kwarg2=s.kwarg2)
    ]
Exemplo n.º 3
0
def test_with_wraps_no_arguments():
    """It should raise a TypeError if called without arguments."""
    with pytest.raises(TypeError):
        with_wraps()