def _test_open_helper(self, *args, **kwargs): from io import TextIOBase builtin_func = open result = builtin_func(*args, **kwargs) stdout = get_stdout() intercepts.register(builtin_func, handler) handled_str, handled_result = builtin_func(*args, **kwargs) handled_stdout = get_stdout() self.assertEqual(handled_str, "handled") self.assertEqual(result.name, handled_result.name) self.assertEqual(type(result), type(handled_result)) if isinstance(result, TextIOBase): self.assertEqual(result.mode, handled_result.mode) self.assertEqual(result.encoding, handled_result.encoding) self.assertEqual(stdout, handled_stdout, "stdout should not change") intercepts.unregister(builtin_func) unhandled_result = builtin_func(*args, **kwargs) unhandled_stdout = get_stdout() self.assertEqual( unhandled_result.name, result.name, "function should no longer be intercepted", ) self.assertEqual(type(result), type(unhandled_result)) if isinstance(result, TextIOBase): self.assertEqual(result.mode, unhandled_result.mode) self.assertEqual(result.encoding, unhandled_result.encoding) self.assertEqual(stdout, unhandled_stdout, "stdout should not change") result.close() handled_result.close() unhandled_result.close()
def test_readme_example(self): self.assertEqual(increment(41), 42) intercepts.register(increment, handler) self.assertEqual(increment(41), 40) intercepts.unregister(increment) intercepts.register(increment, handler2) self.assertEqual(increment(41), "The answer is: 42") intercepts.unregister_all() self.assertEqual(increment(41), 42)
def test_unregister(self): args = (5, 11) result = pow(*args) intercepts.register(pow, handler) self.assertEqual(pow(*args), ("handled", result), "handler function should modify output") intercepts.unregister(pow) self.assertEqual(pow(*args), result, "function should no longer be intercepted")
def test_unregister(self): result = func_no_args() intercepts.register(func_no_args, handler) self.assertEqual( func_no_args(), ("handled", result), "handler function should modify output" ) intercepts.unregister(func_no_args) self.assertEqual( func_no_args(), result, "function should no longer be intercepted" )
def test_unregister_multiple_handlers(self): args = (self, ) result = repr(*args) intercepts.register(repr, handler) intercepts.register(repr, handler) self.assertEqual( repr(*args), ("handled", ("handled", result)), "handler functions should modify output", ) intercepts.unregister(repr) self.assertEqual(repr(*args), result, "function should no longer be intercepted")
def test_unregister(self): t = TestClass() result = t.method_no_arg() intercepts.register(t.method_no_arg, handler) self.assertEqual( t.method_no_arg(), ("handled", result), "handler function should modify output", ) intercepts.unregister(t.method_no_arg) self.assertEqual( t.method_no_arg(), result, "method should no longer be intercepted" )
def test_unregister_multiple_mixed_handlers_4(self): t = TestClass() result = t.method_no_arg() intercepts.register(TestClass.method_no_arg, handler) intercepts.register(t.method_no_arg, handler_0) self.assertEqual( t.method_no_arg(), ("handled_0", ("handled", result)), "handler function should modify output", ) intercepts.unregister(TestClass.method_no_arg, 1) self.assertEqual( t.method_no_arg(), ("handled", result), "method should still be intercepted" )
def run_test(self, builtin_func, *args, **kwargs): result = builtin_func(*args, **kwargs) stdout = get_stdout() intercepts.register(builtin_func, handler) handled_result = builtin_func(*args, **kwargs) handled_stdout = get_stdout() intercepts.unregister(builtin_func) self.assertEqual(handled_result, ("handled", result), "handler function should modify output") self.assertEqual(stdout, handled_stdout, "stdout should not change") unhandled_result = builtin_func(*args, **kwargs) unhandled_stdout = get_stdout() self.assertEqual(unhandled_result, result, "function should no longer be intercepted") self.assertEqual(stdout, unhandled_stdout, "stdout should not change")
def test_unregister_multiple_handlers_depth_1(self): result = func_no_args() intercepts.register(func_no_args, handler) intercepts.register(func_no_args, handler) self.assertEqual( func_no_args(), ("handled", ("handled", result)), "handler functions should modify output", ) intercepts.unregister(func_no_args, depth=1) self.assertEqual( func_no_args(), ("handled", result), "one handler function should modify output", )
def test_unregister_multiple_handlers_depth_1(self): t = TestClass() result = t.method_no_arg() intercepts.register(t.method_no_arg, handler) intercepts.register(t.method_no_arg, handler) self.assertEqual( t.method_no_arg(), ("handled", ("handled", result)), "handler function should modify output", ) intercepts.unregister(t.method_no_arg, depth=1) self.assertEqual( t.method_no_arg(), ("handled", result), "one handler function should modify output", )
def _test_iter_helper(self, *args, **kwargs): builtin_func = iter result = list(builtin_func(*args, **kwargs)) stdout = get_stdout() intercepts.register(builtin_func, handler) handled_result = builtin_func(*args, **kwargs) handled_stdout = get_stdout() intercepts.unregister(builtin_func) self.assertTrue(isinstance(handled_result, tuple)) self.assertEqual(handled_result[0], "handled") self.assertEqual(list(handled_result[1]), result) self.assertEqual(stdout, handled_stdout, "stdout should not change") unhandled_result = list(builtin_func(*args, **kwargs)) unhandled_stdout = get_stdout() self.assertEqual(unhandled_result, result, "function should no longer be intercepted") self.assertEqual(stdout, unhandled_stdout, "stdout should not change")
def test_unregister_multiple_handlers_depth_1(self): func = round args = (3.14159265358979, 2) result = func(*args) intercepts.register(func, handler) intercepts.register(func, handler) self.assertEqual( func(*args), ("handled", ("handled", result)), "handler functions should modify output", ) intercepts.unregister(func, depth=1) self.assertEqual( func(*args), ("handled", result), "one handler function should modify output", )
def run_local_isolated_test(self, builtin_func, *args, **kwargs): # run the function in local isolation to prevent changes due to changing local environment def isolated_func(func, *args, **kwargs): a = 0 b = 1 c = [] return func(*args, **kwargs) result = isolated_func(builtin_func, *args, **kwargs) stdout = get_stdout() intercepts.register(builtin_func, handler) handled_result = isolated_func(builtin_func, *args, **kwargs) handled_stdout = get_stdout() self.assertEqual(handled_result, ("handled", result), "handler function should modify output") self.assertEqual(stdout, handled_stdout, "stdout should not change") intercepts.unregister(builtin_func) unhandled_result = isolated_func(builtin_func, *args, **kwargs) unhandled_stdout = get_stdout() self.assertEqual(unhandled_result, result, "function should no longer be intercepted") self.assertEqual(stdout, unhandled_stdout, "stdout should not change")