def test_staticmethod_interception_preserves_original_func_dict(self): def method_interceptor(context): return context.proceed() intercepted = intercept(self.MyTestClass, "static_method", method_interceptor) self.assertEquals(789, intercepted.static_method.someattribute)
def test_instancemethod_interception(self): def method_interceptor(context): value = context.proceed() return value * 4 intercepted = intercept(self.MyTestClass, "method", method_interceptor) test_obj = intercepted(3) self.assertEquals((3 + 5) * 4, test_obj.method(5))
def test_static_method_interception(self): def static_method_interceptor(context): value = context.proceed() return value * 2 intercepted = intercept(self.MyTestClass, "static_method", static_method_interceptor) test_obj = intercepted self.assertEquals((5 + 7) * 2, test_obj.static_method(5)) test_obj = intercepted(0) self.assertEquals((5 + 7) * 2, test_obj.static_method(5))
def test_class_method_interception(self): def class_method_interceptor(context): value = context.proceed() return value * 2 intercepted = intercept(self.MyTestClass, "class_method", class_method_interceptor) test_obj = intercepted self.assertEquals((5 + 12) * 2, test_obj.class_method(5)) test_obj = intercepted(0) self.assertEquals((5 + 12) * 2, test_obj.class_method(5))
def test_init_interception(self): def init_interceptor(context): self.assertRaises(AttributeError, getattr, context.args[0], "arg") context.proceed() instance = context.args[0] instance.other_arg = 5 intercepted = intercept(self.MyTestClass, "__init__", init_interceptor) test_obj = intercepted(3) self.assertEquals(3, test_obj.arg) self.assertEquals(5, test_obj.other_arg)
def test_static_method_interception(self): def static_method_interceptor(context): value = context.proceed() return value * 2 intercepted = intercept(self.MyTestClass, "static_method", static_method_interceptor) test_obj = intercepted self.assertEquals(( 5 + 7) *2, test_obj.static_method(5)) test_obj = intercepted(0) self.assertEquals((5 + 7) * 2, test_obj.static_method(5))
def __call__(self, config_cls): for section_name in self._co.sections: def section_interceptor(context): o = context.proceed() for k, v in self._co[section_name].items(): setattr(o, k, v) return o # this creates a new subclass every time! we should change the way # intercept works?? config_cls = intercept(config_cls, section_name, section_interceptor) return config_cls