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 == []
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 == []
def test_record_exception(): fun = record(results=True)(rfun) raises(RuntimeError, fun) assert fun.calls == [ (None, (), {}, None, exc), ]
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_record(): fun = record(nfun) assert fun(2, 3) == (2, 3) assert fun(3, b=4) == (3, 4) assert fun.calls == [ (None, (2, 3), {}), (None, (3, ), {'b': 4}), ]
def test_record_exception_callback(): calls = [] fun = record(results=True, callback=lambda *args: calls.append(args))(rfun) raises(RuntimeError, fun) assert calls == [ (None, 'test_aspectlib_test.rfun', (), {}, None, exc), ]
def test_double_recording(): with record(module_fun) as history: with record(module_fun2) as history2: module_fun(2, 3) module_fun2(2, 3) assert history.calls == [ (None, (2, 3), {}), ] del history.calls[:] assert history2.calls == [ (None, (2, 3), {}), ] del history2.calls[:] module_fun(2, 3) assert history.calls == [] assert history2.calls == []
def test_record_result(): fun = record(results=True)(nfun) assert fun(2, 3) == (2, 3) assert fun(3, b=4) == (3, 4) assert fun.calls == [ (None, (2, 3), {}, (2, 3), None), (None, (3, ), {'b': 4}, (3, 4), None), ]
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_module(strmod=None): calls = [] from test_pkg1.test_pkg2 import test_mod with aspectlib.weave(strmod or test_mod, record(calls=calls, extended=True)): test_mod.target() obj = test_mod.Stuff() obj.meth() assert calls == [(None, 'test_pkg1.test_pkg2.test_mod.target', (), {}), (obj, 'test_pkg1.test_pkg2.test_mod.meth', (), {})]
def test_weave_os_module(): calls = [] with aspectlib.weave('os', record(calls=calls, extended=True), methods="getenv|walk"): os.getenv('BUBU', 'bubu') os.walk('.') assert calls == [ (None, 'os.getenv', ('BUBU', 'bubu'), {}), (None, 'os.walk', ('.',), {}) ]
def test_weave_module(strmod=None): calls = [] from test_pkg1.test_pkg2 import test_mod with aspectlib.weave(strmod or test_mod, record(calls=calls, extended=True)): test_mod.target() obj = test_mod.Stuff() obj.meth() assert calls == [ (None, 'test_pkg1.test_pkg2.test_mod.target', (), {}), (obj, 'test_pkg1.test_pkg2.test_mod.meth', (), {}) ]
def test_record_callback(): calls = [] fun = record(callback=lambda *args: calls.append(args))(nfun) assert fun(2, 3) == (2, 3) assert fun(3, b=4) == (3, 4) assert calls == [ (None, 'test_aspectlib_test.nfun', (2, 3), {}), (None, 'test_aspectlib_test.nfun', (3, ), {'b': 4}), ]
def test_socket_meth(meth=socket.socket.close): calls = [] with aspectlib.weave(meth, record(calls=calls)): s = socket.socket() assert s.close() is None assert calls == [(s, (), {})] del calls[:] s = socket.socket() assert s.close() is None assert calls == []
def test_record_as_context(): with record(module_fun) as history: module_fun(2, 3) module_fun(3, b=4) assert history.calls == [ (None, (2, 3), {}), (None, (3, ), {'b': 4}), ] del history.calls[:] module_fun(2, 3) module_fun(3, b=4) assert history.calls == []
def test_mock_no_calls(): with record(module_fun) as history: assert "foobar" == mock("foobar")(module_fun)(2) assert history.calls == []
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,), {})]
def test_record_not_iscalled_and_results(): raises(AssertionError, record, module_fun, iscalled=False, results=True) record(module_fun, iscalled=False, results=False) record(module_fun, iscalled=True, results=True) record(module_fun, iscalled=True, results=False)