Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def make_closure(value):
- def func(self, *args, **kwargs):
- return value(*args, **kwargs)
- return func
- def lazymock(**kwargs):
- class Foo(object): pass
- for key, value in kwargs.iteritems():
- if isinstance(value, types.FunctionType):
- setattr(Foo, key, make_closure(value))
- else:
- setattr(Foo, key, value)
- return Foo()
- # And we should really test lazymock, so...
- class TestLazymock(unittest.TestCase):
- def testSimple(self):
- x = lazymock(foo=3)
- self.assertEqual(x.foo, 3)
- def testMany(self):
- x = lazymock(foo=3, bar='Hello')
- self.assertEqual(x.foo, 3)
- self.assertEqual(x.bar, 'Hello')
- def testFunction(self):
- x = lazymock(foo=lambda x: x + 3)
- self.assertEqual(x.foo(3), 6)
Add Comment
Please, Sign In to add comment