Ejemplo n.º 1
0
def test_argument_checks_with_deprecated_methods():
    """When used with @deprecated, argument checks still work."""
    @deprecated('this test class is deprecated', '1.2')
    class ADeprecatedClass:
        @arg_checks(y=IsInstance(int), title=IsInstance(str))
        def __init__(self, x, y, title='a title', style=None):
            pass

    with expected(IsInstance):
        ADeprecatedClass(1, 'y')
    with expected(IsInstance):
        ArgumentCheckedCallable(ADeprecatedClass).checkargs('x', 'y')
    with expected(IncorrectArgumentError):
        ArgumentCheckedCallable(ADeprecatedClass, explanation='an explanation').checkargs('x', NotYetAvailable('x'), title='a valid title')


    class ADeprecatedClass:
        @deprecated('this instance method is deprecated', '1.3')
        @arg_checks(y=IsInstance(int), title=IsInstance(str))
        def instance_method(self, x, y, title='a title', style=None):
            pass

        @deprecated('this class method is deprecated', '2.3')
        @arg_checks(y=IsInstance(int), title=IsInstance(str))
        @classmethod
        def class_method(cls, x, y, title='a title', style=None):
            pass

    with expected(IsInstance):
        ADeprecatedClass().instance_method('x', 'y')
    with expected(IsInstance):
        ADeprecatedClass.class_method('x', 'y')
Ejemplo n.º 2
0
def test_checking_arguments(argument_check_fixture):
    """Methods can be augmented with argument checks. These checks are done when calling such a method,
       or before an actual call is made using ArgumentCheckedCallable.checkargs."""
    fixture = argument_check_fixture
    with expected(fixture.expected_exception):
        fixture.callable(*fixture.call_args, **fixture.kwargs)
    with expected(fixture.expected_exception):
        ArgumentCheckedCallable(fixture.callable).checkargs(*fixture.args, **fixture.kwargs)

    wrapped_exception = NoException
    if fixture.expected_exception is not NoException:
        wrapped_exception = IncorrectArgumentError

    with expected(wrapped_exception):
        ArgumentCheckedCallable(fixture.callable, explanation='some message').checkargs(*fixture.args, **fixture.kwargs)
Ejemplo n.º 3
0
def test_checking_before_args_available():
    """When checking arguments before a method call, the checks for some arguments may be ignored by passing NotYetAvailable
       instances for them. The name of NotYetAvailable arguments should match the name of the argument it replaces."""
    class ModelObject:
        @arg_checks(y=IsInstance(int), title=IsInstance(str))
        def do_something(self, x, y, title='a title', style=None):
            pass

    model_object = ModelObject()
    with expected(NoException):
        ArgumentCheckedCallable(model_object.do_something).checkargs('x', NotYetAvailable('y'), title='a title')
    with expected(IsInstance):
        ArgumentCheckedCallable(model_object.do_something).checkargs('x', NotYetAvailable('y'), title=123)

    with expected(IncorrectArgumentError):
        ArgumentCheckedCallable(model_object.do_something, explanation='an explanation').checkargs('x', NotYetAvailable('x'), title='a valid title')