def test_wrapper_calls_passed_lambdas(self): data = {} def side_effect(ab): data['value'] = ab return ab l = lambda x: side_effect(x) * 2 wrapped = test_utils.FunctionWrapper(l) self.assertEqual(wrapped('foobar'), 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_wrapper_calls_passed_lambdas(self): data = {} def mock_function_with_side_effect(num): data['value'] = num return num mock_lambda = lambda x: mock_function_with_side_effect(x) * 2 wrapped = test_utils.FunctionWrapper(mock_lambda) self.assertEqual(wrapped('foobar'), 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_wrapper_calls_passed_static_method(self): """Tests that FunctionWrapper also works for static methods.""" data = {} class MockClass: @staticmethod def mock_staticmethod(num): data['value'] = num return num * 2 wrapped = test_utils.FunctionWrapper(MockClass.mock_staticmethod) with self.swap(MockClass, 'mock_staticmethod', wrapped): val = MockClass.mock_staticmethod('foobar') self.assertEqual(val, 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_wrapper_calls_passed_static_method(self): """Tests that FunctionWrapper also works for static methods""" data = {} class A(object): @staticmethod def static_method(ab): data['value'] = ab return ab * 2 wrapped = test_utils.FunctionWrapper(A.static_method) with self.swap(A, 'static_method', wrapped): val = A.static_method('foobar') self.assertEqual(val, 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_wrapper_calls_passed_class_method(self): """Tests that FunctionWrapper also works for class methods.""" data = {} class MockClass: str_attr = 'foo' @classmethod def mock_classmethod(cls, num): data['value'] = cls.str_attr + num return (cls.str_attr + num) * 2 wrapped = test_utils.FunctionWrapper(MockClass.mock_classmethod) with self.swap(MockClass, 'mock_classmethod', wrapped): val = MockClass.mock_classmethod('bar') self.assertEqual(val, 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_wrapper_calls_passed_class_method(self): """Tests that FunctionWrapper also works for class methods""" data = {} class A(object): a = "foo" @classmethod def class_method(cls, b): data['value'] = cls.a + b return (cls.a + b) * 2 wrapped = test_utils.FunctionWrapper(A.class_method) with self.swap(A, 'class_method', wrapped): val = A.class_method('bar') self.assertEqual(val, 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_wrapper_calls_passed_method(self): """Tests that FunctionWrapper also works for methods.""" data = {} class MockClass: def __init__(self, num1): self.num1 = num1 def mock_method(self, num2): data['value'] = self.num1 + num2 return (self.num1 + num2) * 2 wrapped = test_utils.FunctionWrapper(MockClass.mock_method) with self.swap(MockClass, 'mock_method', wrapped): val = MockClass('foo').mock_method('bar') self.assertEqual(val, 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_wrapper_calls_passed_method(self): """Tests that FunctionWrapper also works for methods""" data = {} class A(object): def __init__(self, a): self.a = a def my_method(self, b): data['value'] = self.a + b return (self.a + b) * 2 wrapped = test_utils.FunctionWrapper(A.my_method) with self.swap(A, 'my_method', wrapped): val = A('foo').my_method('bar') self.assertEqual(val, 'foobarfoobar') self.assertEqual(data.get('value'), 'foobar')
def test_pre_call_hook_does_nothing(self): function = lambda x: x ** 2 wrapped = test_utils.FunctionWrapper(function) self.assertIsNone(wrapped.pre_call_hook('args'))