Exemplo n.º 1
0
    def test_quickstart_example_1(self):
        def handler(func, *args, **kwargs):
            return func(*args, **kwargs) * 2

        intercepts.register(sum, handler)
        self.assertEqual(sum([2, 2]), 8)
        self.assertEqual(sum([1, 2, 3, 4, 5, 6]), 42)
Exemplo n.º 2
0
    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()
Exemplo n.º 3
0
 def test_unregister_all(self):
     t = TestClass()
     result1 = t.method_no_arg()
     result2 = t.method_one_arg(1)
     intercepts.register(t.method_no_arg, handler)
     intercepts.register(t.method_one_arg, handler)
     self.assertEqual(
         t.method_no_arg(),
         ("handled", result1),
         "handler function should modify first output",
     )
     self.assertEqual(
         t.method_one_arg(1),
         ("handled", result2),
         "handler function should modify second output",
     )
     intercepts.unregister_all()
     self.assertEqual(
         t.method_no_arg(), result1, "first function should no longer be intercepted"
     )
     self.assertEqual(
         t.method_one_arg(1),
         result2,
         "second function should no longer be intercepted",
     )
     self.assertEqual(
         intercepts.registration._HANDLERS,
         {},
         "All function intercept handlers should be unregistered.",
     )
Exemplo n.º 4
0
    def test_quickstart_example_2(self):
        def pre_handler(func, *args, **kwargs):
            print("Executing function:", func.__name__)
            return func(*args, **kwargs)

        def post_handler(func, *args, **kwargs):
            result = func(*args, **kwargs)
            print("Executed function:", func.__name__)
            return result

        intercepts.register(sum, pre_handler)
        intercepts.register(sum, post_handler)

        self.assertEqual(sum([1, 2, 3, 4, 5, 6]), 21)
        self.assertEqual(
            sys.stdout.out,
            [
                "Executing function:",
                " ",
                "sum",
                "\n",
                "Executed function:",
                " ",
                "sum",
                "\n",
            ],
        )
Exemplo n.º 5
0
 def test_unregister_all(self):
     result = func_no_args()
     intercepts.register(func_no_args, handler)
     intercepts.register(func_no_return, handler)
     self.assertEqual(
         func_no_args(),
         ("handled", result),
         "handler function should modify first output",
     )
     self.assertEqual(
         func_no_return(),
         ("handled", None),
         "handler function should modify second output",
     )
     intercepts.unregister_all()
     self.assertEqual(
         func_no_args(), result, "first function should no longer be intercepted"
     )
     self.assertIsNone(
         func_no_return(), "second function should no longer be intercepted"
     )
     self.assertEqual(
         intercepts.registration._HANDLERS,
         {},
         "All function intercept handlers should be unregistered.",
     )
Exemplo n.º 6
0
 def test_register_func_one_kwarg_1(self):
     result = func_one_kwarg()
     intercepts.register(func_one_kwarg, handler)
     self.assertEqual(
         func_one_kwarg(),
         ("handled", result),
         "handler function should modify output",
     )
Exemplo n.º 7
0
 def test_resister_print_call(self):
     args = ("test message", )
     result = print(*args)
     self.assertEqual(sys.stdout.out, [" ".join(args), "\n"])
     intercepts.register(print, handler)
     self.assertEqual(print(*args), ("handled", result),
                      "handler function should modify output")
     self.assertEqual(sys.stdout.out, [" ".join(args), "\n"] * 2)
Exemplo n.º 8
0
 def test_register_func_two_args(self):
     arg1, arg2 = 1, 2
     result = func_two_args(arg1, arg2)
     intercepts.register(func_two_args, handler)
     self.assertEqual(
         func_two_args(arg1, arg2),
         ("handled", result),
         "handler function should modify output",
     )
Exemplo n.º 9
0
 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)
Exemplo n.º 10
0
 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")
Exemplo n.º 11
0
 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"
     )
Exemplo n.º 12
0
 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")
Exemplo n.º 13
0
    def test_register_sorted_rev(self):
        args = ([1, 4, 6, 2, 9, 5, 10, 11, 11, 3, -18], )
        result = sorted(*args)

        def handler_rev(func, *args, **kwargs):
            return ("handled", list(reversed(func(*args, **kwargs))))

        intercepts.register(sorted, handler_rev)
        self.assertEqual(
            sorted(*args),
            ("handled", list(reversed(result))),
            "handler function should modify output",
        )
Exemplo n.º 14
0
 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"
     )
Exemplo n.º 15
0
 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"
     )
Exemplo n.º 16
0
 def test_register_import(self):
     result = __import__("os", globals(), locals(), level=0)
     intercepts.register(
         __import__,
         lambda func, *args, **kwargs:
         [print(args[0]), func(*args, **kwargs)][-1],
     )
     result_ = __import__("os", globals(), locals(), level=0)
     self.assertEqual(result_, result,
                      "handler function should not modify output")
     self.assertEqual(
         sys.stdout.out,
         ["os", "\n"],
         "handler function should print the name of the imported module",
     )
Exemplo n.º 17
0
 def test_register_method_one_arg(self):
     t = TestClass()
     t2 = TestClass()
     result = t.method_one_arg(1)
     intercepts.register(t.method_one_arg, handler)
     self.assertEqual(
         t.method_one_arg(1),
         ("handled", result),
         "handler function should modify output",
     )
     self.assertEqual(
         t2.method_one_arg(1),
         ("handled", result),
         "handler function should modify output of other methods",
     )
Exemplo n.º 18
0
 def test_register_class_function(self):
     t = TestClass()
     result = t.method_no_arg()
     intercepts.register(TestClass.method_no_arg, handler)
     self.assertEqual(
         t.method_no_arg(),
         ("handled", result),
         "handler function should modify output",
     )
     t2 = TestClass()
     self.assertEqual(
         t2.method_no_arg(),
         ("handled", result),
         "handler function should modify output",
     )
Exemplo n.º 19
0
 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")
Exemplo n.º 20
0
 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",
     )
Exemplo n.º 21
0
 def test_resister_print(self):
     self.assertEqual(
         intercepts.register(print, handler),
         print,
         "intercepts.register should return the handled function",
     )
     self.assertTrue(isinstance(print, types.BuiltinFunctionType))
Exemplo n.º 22
0
 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",
     )
Exemplo n.º 23
0
 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",
     )
Exemplo n.º 24
0
 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")
Exemplo n.º 25
0
    def test_quickstart_example_3(self):
        def handler_0(func, *args, **kwargs):
            print("handler 0")
            return func(*args, **kwargs)

        def handler_1(func, *args, **kwargs):
            print("handler 1")
            return func(*args, **kwargs)

        intercepts.register(abs, handler_0)
        intercepts.register(abs, handler_1)

        self.assertEqual(abs(33), 33)
        self.assertEqual(sys.stdout.out,
                         ["handler 1", "\n", "handler 0", "\n"])

        sys.stdout.out = []
        self.assertEqual(abs(-6), 6)
        self.assertEqual(sys.stdout.out,
                         ["handler 1", "\n", "handler 0", "\n"])
Exemplo n.º 26
0
    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")
Exemplo n.º 27
0
 def test_register_func_no_args(self):
     result = func_no_args()
     intercepts.register(func_no_args, handler)
     self.assertEqual(
         func_no_args(), ("handled", result), "handler function should modify output"
     )
Exemplo n.º 28
0
 def test_register_func_no_return(self):
     intercepts.register(func_no_return, handler)
     self.assertEqual(
         func_no_return(), ("handled", None), "handler function should modify output"
     )
Exemplo n.º 29
0
 def test_handle_handler(self):
     with self.assertRaises(ValueError):
         intercepts.register(handler, handler)
         handler(func)
Exemplo n.º 30
0
 def test_register_sum(self):
     args = ([1, 4, 6, 2, 9, 5, 10, 11, 11, 3, -18], )
     result = sum(*args)
     intercepts.register(sum, handler)
     self.assertEqual(sum(*args), ("handled", result),
                      "handler function should modify output")