示例#1
0
def test_uncatch(register_advice):
    def handler(context):
        yield

    advice = Advice(methods=re.compile('check_me'), handler=handler)
    register_advice(advice)
    instance = Patched()
    with pytest.raises(TypeError):
        instance.check_me(1, 2, 3)
示例#2
0
def test_raise_before(register_advice):
    def handler(context):
        raise ZeroDivisionError

    advice = Advice(methods=re.compile('check_me'), handler=handler)
    register_advice(advice)
    instance = Patched()
    with pytest.raises(ZeroDivisionError):
        instance.check_me(1, 2)
示例#3
0
def test_naive(register_advice):
    def handler(context):
        yield

    advice = Advice(methods=re.compile('check_me'), handler=handler)
    register_advice(advice)
    instance = Patched()
    result = instance.check_me(1, 2)
    assert result == '1|2'
示例#4
0
def test_wrapping():
    advices.catalog = []
    advices.register(
        Advice(
            handler=handler1,
            modules=re.compile('textwrap'),
            targets=re.compile('fill'),
        ))
    result = module.fill('12')
    assert result == '12|3'
示例#5
0
def test_patch_input(register_advice):
    def handler(context):
        context.args = (4, 2)
        yield

    advice = Advice(methods=re.compile('check_me'), handler=handler)
    register_advice(advice)
    instance = Patched()
    result = instance.check_me(1, 2)
    assert result == '4|2'
示例#6
0
def test_method_check(register_advice):
    def handler(context):
        yield
        context.result = 13

    advice = Advice(methods=re.compile('check_me'), handler=handler)
    register_advice(advice)
    instance = Patched()
    result = instance.nothing()
    assert result == 42
示例#7
0
def test_catch(register_advice):
    def handler(context):
        try:
            yield
        except TypeError:
            pass

    advice = Advice(methods=re.compile('check_me'), handler=handler)
    register_advice(advice)
    instance = Patched()
    result = instance.check_me(1, 2, 3)
    assert result is None
示例#8
0
def test_patch_object(register_advice):
    def check_me(a, b=None):
        return '{}|{}'.format(a, b)

    def handler1(context):
        yield
        context.result += '|3'

    def handler2(context):
        yield
        context.result += '|4'

    advice = Advice(handler=handler1)
    register_advice(advice)
    patched = patch_object(check_me)

    advice = Advice(handler=handler2)
    patched = patch_object(patched)
    register_advice(advice)

    result = patched(1, 2)
    assert result == '1|2|3|4'
示例#9
0
def test_patch_function(register_advice):
    def check_me(a, b=None):
        return '{}|{}'.format(a, b)

    def handler(context):
        yield
        context.result = 'lol'

    advice = Advice(handler=handler)
    register_advice(advice)
    patched = patch_function(check_me)
    result = patched(1, 2)
    assert result == 'lol'
示例#10
0
def test_patch_class(register_advice):
    class Source:
        def check_me(self, a, b=None):
            return '{}|{}'.format(a, b)

    def handler(context):
        yield
        context.result = 'lol'

    advice = Advice(methods=re.compile('check_me'), handler=handler)
    register_advice(advice)
    Patched = patch_class(Source)
    instance = Patched()
    result = instance.check_me(1, 2)
    assert result == 'lol'
示例#11
0
@pytest.fixture()
def clean():
    if 'textwrap' in sys.modules:
        del sys.modules['textwrap']
    patchers.unpatch_import()
    patchers.unpatch_cache()


def handler(context):
    yield
    context.result = 'lol'


advice = Advice(
    handler=handler,
    modules=re.compile('textwrap'),
    targets=re.compile('fill'),
)


def test_source(clean):
    import textwrap
    assert textwrap.fill.__module__ == 'textwrap'


def test_before(register_advice, clean):
    register_advice(advice)
    patchers.patch_import()
    import textwrap

    assert isinstance(textwrap, AspectModule)