Exemple #1
0
def test_list_of_aspects():
    with aspectlib.weave(module_func, [mock('foobar'), record]):
        assert module_func(1, 2, 3) == 'foobar'
        assert module_func.calls == [(None, (1, 2, 3), {})]

    with aspectlib.weave(module_func, [mock('foobar', call=True), record]):
        raises(TypeError, module_func, 1, 2, 3)
        assert module_func.calls == [(None, (1, 2, 3), {})]
Exemple #2
0
def test_weave_class_meth_no_aliases_unsupported_on_py3():
    with aspectlib.weave(Global.meth, mock('stuff')):
        assert Global().meth() == 'stuff'
        assert Global2().meth() == 'stuff'

    assert Global().meth() == 'base'
    assert Global2().meth() == 'base'
Exemple #3
0
def test_weave_multiple():
    with aspectlib.weave((module_func, module_func2), mock('foobar')):
        assert module_func() == 'foobar'
        assert module_func2() == 'foobar'

    assert module_func() is None
    assert module_func2() is None
def test_weave_class_meth_no_aliases_unsupported_on_py3():
    with aspectlib.weave(Global.meth, mock("stuff")):
        assert Global().meth() == "stuff"
        assert Global2().meth() == "stuff"

    assert Global().meth() == "base"
    assert Global2().meth() == "base"
def test_weave_old_style_method_no_warn_patch_module():
    calls = []
    with aspectlib.weave("warnings.warn", record(calls=calls)):
        with aspectlib.weave("test_aspectlib.LegacyTestClass.foobar", mock("stuff")):
            assert LegacyTestClass().foobar() == "stuff"

    assert calls == []
Exemple #6
0
def test_weave_missing_global(cls=Global):
    global Global
    Global = 'crap'
    try:
        raises(AssertionError, aspectlib.weave, cls, mock('stuff'), lazy=True)
    finally:
        Global = cls
Exemple #7
0
def test_weave_str_target():
    with aspectlib.weave('test_pkg1.test_pkg2.test_mod.target', mock('foobar')):
        from test_pkg1.test_pkg2.test_mod import target
        assert target() == 'foobar'

    from test_pkg1.test_pkg2.test_mod import target
    assert target() is None
def test_fixture_2(weave):
    assert Foo().bar() == 1

    with weave(Foo.bar, test.mock(2)):
        assert Foo().bar() == 2

    assert Foo().bar() == 1
Exemple #9
0
def test_weave_old_style_method_no_warn_patch_module():
    calls = []
    with aspectlib.weave('warnings.warn', record(calls=calls)):
        with aspectlib.weave('test_aspectlib.LegacyTestClass.foobar', mock('stuff')):
            assert LegacyTestClass().foobar() == 'stuff'

    assert calls == []
Exemple #10
0
def test_weave_str_class_meth_target():
    with aspectlib.weave('test_pkg1.test_pkg2.test_mod.Stuff.meth', mock('foobar')):
        from test_pkg1.test_pkg2.test_mod import Stuff
        assert Stuff().meth() == 'foobar'

    from test_pkg1.test_pkg2.test_mod import Stuff
    assert Stuff().meth() is None
Exemple #11
0
def test_weave_wrong_module():
    calls = []
    with aspectlib.weave('warnings.warn', record(calls=calls)):
        aspectlib.weave(AliasedGlobal, mock('stuff'), lazy=True)
    assert calls == [(None, (
        "Setting test_aspectlib.MissingGlobal to <class 'test_aspectlib.MissingGlobal'>. "
        "There was no previous definition, probably patching the wrong module.",
    ), {})]
def test_weave_class_meth_no_aliases():
    with aspectlib.weave(Global.meth, mock("stuff"), aliases=False, lazy=True):
        assert Global().meth() == "stuff"
        assert Global2 is not Global
        assert Global2().meth() == "base"

    assert Global().meth() == "base"
    assert Global2 is Global
    assert Global2().meth() == "base"
def test_weave_str_class_meth_target():
    with aspectlib.weave("test_pkg1.test_pkg2.test_mod.Stuff.meth", mock("foobar")):
        from test_pkg1.test_pkg2.test_mod import Stuff

        assert Stuff().meth() == "foobar"

    from test_pkg1.test_pkg2.test_mod import Stuff

    assert Stuff().meth() is None
def test_weave_str_target():
    with aspectlib.weave("test_pkg1.test_pkg2.test_mod.target", mock("foobar")):
        from test_pkg1.test_pkg2.test_mod import target

        assert target() == "foobar"

    from test_pkg1.test_pkg2.test_mod import target

    assert target() is None
Exemple #15
0
def test_weave_class_no_aliases():
    with aspectlib.weave(Global, mock('stuff'), aliases=False, lazy=True):
        assert Global().meth() == 'stuff'
        assert Global2 is not Global
        assert Global2().meth() == 'base'

    assert Global().meth() == 'base'
    assert Global2 is Global
    assert Global2().meth() == 'base'
Exemple #16
0
def test_weave_no_aliases():
    with aspectlib.weave(module_func2, mock('stuff'), aliases=False):
        assert module_func2() == 'stuff'
        assert module_func2 is not module_func3
        assert module_func3() is None

    assert module_func2() is None
    assert module_func3() is None
    assert module_func2 is module_func3
def test_weave_wrong_module():
    calls = []
    with aspectlib.weave('warnings.warn', record(calls=calls)):
        aspectlib.weave(AliasedGlobal, mock('stuff'), lazy=True)
    assert calls == [
        (None,
         ("Setting test_aspectlib.MissingGlobal to <class 'test_aspectlib.MissingGlobal'>. "
          "There was no previous definition, probably patching the wrong module.",),
         {})
    ]
Exemple #18
0
def test_fork():
    with aspectlib.weave('os.fork', mock('foobar')):
        pid = os.fork()
        if not pid:
            os._exit(0)
        assert pid == 'foobar'

    pid = os.fork()
    if not pid:
        os._exit(0)
    assert pid != 'foobar'
def test_invalid_string_target():
    raises(SyntaxError, aspectlib.weave, "inva lid", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.inva lid", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.2invalid", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.some,junk", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.some?junk", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.some*junk", mock(None))

    with aspectlib.weave("test_aspectlib._internal", mock("stuff")):
        assert _internal() == "stuff"
Exemple #20
0
def test_invalid_string_target():
    raises(SyntaxError, aspectlib.weave, 'inva lid', mock(None))
    raises(SyntaxError, aspectlib.weave, 'os.inva lid', mock(None))
    raises(SyntaxError, aspectlib.weave, 'os.2invalid', mock(None))
    raises(SyntaxError, aspectlib.weave, 'os.some,junk', mock(None))
    raises(SyntaxError, aspectlib.weave, 'os.some?junk', mock(None))
    raises(SyntaxError, aspectlib.weave, 'os.some*junk', mock(None))

    with aspectlib.weave('test_aspectlib._internal', mock('stuff')):
        assert _internal() == 'stuff'
def test_fixture_1(weave):
    weave(Foo.bar, test.mock(2))
    assert Foo().bar() == 2
Exemple #22
0
def test_weave_str_bad_target():
    raises(TypeError, aspectlib.weave, 'test_pkg1.test_pkg2.test_mod.a', mock('foobar'))
Exemple #23
0
def test_weave_str_missing_target():
    raises(AttributeError, aspectlib.weave, 'test_pkg1.test_pkg2.target', mock('foobar'))
Exemple #24
0
def test_weave_func():
    with aspectlib.weave(module_func, mock('stuff')):
        assert module_func() == 'stuff'

    assert module_func() is None
Exemple #25
0
def test_simple_mock():
    assert "foobar" == mock("foobar")(module_fun)(1)
Exemple #26
0
def test_mock_with_calls():
    with record(module_fun) as history:
        assert "foobar" == mock("foobar", call=True)(module_fun)(3)
    assert history.calls == [(None, (3,), {})]
Exemple #27
0
def test_weave_subclass(Bub=Sub):
    with aspectlib.weave(Sub, mock('foobar'), lazy=True):
        assert Sub().meth() == 'foobar'
        assert Bub().meth() == 'base'
    assert Sub().meth() == 'base'
    assert Bub is Sub
Exemple #28
0
def test_weave_bad_args2():
    aspectlib.weave('warnings.warn', mock('stuff'), methods='(?!asdf)')
Exemple #29
0
def test_weave_bad_args4():
    aspectlib.weave('warnings.warn', mock('stuff'), subclasses=False)
Exemple #30
0
def test_weave_subclass_meth_manual():
    with aspectlib.weave(Sub, mock('foobar'), lazy=True, methods=['meth']):
        assert Sub().meth() == 'foobar'

    assert Sub().meth() == 'base'
Exemple #31
0
def test_mock_no_calls():
    with record(module_fun) as history:
        assert "foobar" == mock("foobar")(module_fun)(2)
    assert history.calls == []
Exemple #32
0
def test_weave_bad_args1():
    aspectlib.weave('warnings.warn', mock('stuff'), methods=['asdf'])
Exemple #33
0
def test_weave_subclass_meth_auto2():
    with aspectlib.weave(Sub.meth, mock('foobar')):
        assert Sub().meth() == 'foobar'

    assert Sub().meth() == 'base'
Exemple #34
0
def test_weave_bad_args3():
    aspectlib.weave('warnings.warn', mock('stuff'), lazy=False)
Exemple #35
0
 def test_py2_invalid_unicode_in_string_target():
     raises(SyntaxError, aspectlib.weave, 'os.ăa', mock(None))
     raises(SyntaxError, aspectlib.weave, u'os.ăa', mock(None))
     raises(SyntaxError, aspectlib.weave, 'os.aă', mock(None))
     raises(SyntaxError, aspectlib.weave, u'os.aă', mock(None))
Exemple #36
0
def test_weave_bad_args5():
    raises(TypeError, aspectlib.weave, Sub, mock('stuff'), methods=False)
Exemple #37
0
def test_mock_builtin_os():
    print(os.open.__name__)
    with aspectlib.weave('os.open', mock('foobar')):
        assert os.open('???') == 'foobar'

    assert os.open(__file__, 0) != 'foobar'