|
|
import sys |
|
|
import unittest |
|
|
from inspect import getmodule, signature, Signature, Parameter |
|
|
|
|
|
|
|
|
from main import create_function, wraps, partial, with_partial |
|
|
|
|
|
PY2 = sys.version_info < (3, ) |
|
|
|
|
|
class TestFunctionCreation(unittest.TestCase): |
|
|
|
|
|
def test_create_facades(self): |
|
|
""" |
|
|
Simple test to create multiple functions with the same body. |
|
|
""" |
|
|
|
|
|
def generic_impl(f, *args, **kwargs): |
|
|
print("This is generic impl called by %s" % f.__name__) |
|
|
if f.__name__ == "func1": |
|
|
print("called from func1 !") |
|
|
return args, kwargs |
|
|
|
|
|
my_module = getmodule(generic_impl) |
|
|
|
|
|
for f_name, f_params in [("func1", "b, *, a"), |
|
|
("func2", "*args, **kwargs"), |
|
|
("func3", "c, *, a, d=None")]: |
|
|
if f_name in {"func1", "func3"} and sys.version_info < (3, 0): |
|
|
pass |
|
|
else: |
|
|
f_sig = "%s(%s)" % (f_name, f_params) |
|
|
f = create_function(f_sig, generic_impl, inject_as_first_arg=True) |
|
|
setattr(my_module, f_name, f) |
|
|
|
|
|
if sys.version_info >= (3, 0): |
|
|
func1 = getattr(my_module, 'func1') |
|
|
self.assertEqual(func1(25, a=12), ((), dict(b=25, a=12))) |
|
|
|
|
|
func2 = getattr(my_module, 'func2') |
|
|
self.assertEqual(func2(25, a=12), ((25,), dict(a=12))) |
|
|
|
|
|
if sys.version_info >= (3, 0): |
|
|
func3 = getattr(my_module, 'func3') |
|
|
self.assertEqual(func3(25, a=12), ((), dict(c=25, a=12, d=None))) |
|
|
|
|
|
def test_so_decorator(self): |
|
|
""" |
|
|
Tests that solution at Stack Overflow works. |
|
|
""" |
|
|
|
|
|
def makebold(fn): |
|
|
@wraps(fn) |
|
|
def wrapped(): |
|
|
return "<b>" + fn() + "</b>" |
|
|
return wrapped |
|
|
|
|
|
def makeitalic(fn): |
|
|
@wraps(fn) |
|
|
def wrapped(): |
|
|
return "<i>" + fn() + "</i>" |
|
|
return wrapped |
|
|
|
|
|
@makebold |
|
|
@makeitalic |
|
|
def hello(): |
|
|
return "hello world" |
|
|
|
|
|
self.assertEqual(hello(), "<b><i>hello world</i></b>") |
|
|
self.assertEqual(hello.__name__, "hello") |
|
|
help(hello) |
|
|
self.assertTrue(hasattr(hello, '__wrapped__')) |
|
|
|
|
|
def test_so_facade(self): |
|
|
def create_initiation_function(cls, gen_init): |
|
|
params = [Parameter('self', kind=Parameter.POSITIONAL_OR_KEYWORD)] |
|
|
for mandatory_arg_name in cls.__init_args__: |
|
|
params.append(Parameter(mandatory_arg_name, kind=Parameter.POSITIONAL_OR_KEYWORD)) |
|
|
for default_arg_name, default_arg_val in cls.__opt_init_args__.items(): |
|
|
params.append(Parameter(default_arg_name, kind=Parameter.POSITIONAL_OR_KEYWORD, default=default_arg_val)) |
|
|
sig = Signature(params) |
|
|
return create_function(sig, gen_init) |
|
|
|
|
|
def generic_init(self, *args, **kwargs): |
|
|
assert len(args) == 0 |
|
|
for name, val in kwargs.items(): |
|
|
setattr(self, name, val) |
|
|
|
|
|
class my_class: |
|
|
__init_args__ = ["x", "y"] |
|
|
__opt_init_args__ = {"my_opt": None} |
|
|
|
|
|
my_class.__init__ = create_initiation_function(my_class, generic_init) |
|
|
|
|
|
o1 = my_class(1, 2) |
|
|
self.assertEqual(vars(o1), {'y': 2, 'x': 1, 'my_opt': None}) |
|
|
|
|
|
o2 = my_class(1, 2, 3) |
|
|
self.assertEqual(vars(o2), {'y': 2, 'x': 1, 'my_opt': 3}) |
|
|
|
|
|
o3 = my_class(my_opt='hello', y=3, x=2) |
|
|
self.assertEqual(vars(o3), {'y': 3, 'x': 2, 'my_opt': 'hello'}) |
|
|
|
|
|
def test_so_sig_preserving(self): |
|
|
def my_decorator(func): |
|
|
@wraps(func) |
|
|
def wrapper(*args, **kwargs): |
|
|
return func(*args, **kwargs) |
|
|
wrapper._decorator_name_ = 'my_decorator' |
|
|
return wrapper |
|
|
|
|
|
@my_decorator |
|
|
def my_func(x): |
|
|
print('hello %s' % x) |
|
|
|
|
|
self.assertEqual(my_func._decorator_name_, 'my_decorator') |
|
|
help(my_func) |
|
|
|
|
|
def test_so_partial(self): |
|
|
def foo(a, b, c=1): |
|
|
return (a + b) * c |
|
|
|
|
|
bar10_p = partial(foo, b=10) |
|
|
|
|
|
self.assertEqual(bar10_p(0), 10) |
|
|
self.assertEqual(bar10_p(0, c=2), 20) |
|
|
help(bar10_p) |
|
|
|
|
|
def test_so_partial2(self): |
|
|
@with_partial(a='hello', b='world') |
|
|
def test(a, b, x, y): |
|
|
print(a, b) |
|
|
print(x, y) |
|
|
|
|
|
test(x=1, y=2) |
|
|
|
|
|
help(test) |
|
|
|
|
|
def test_so_partial_posargs(self): |
|
|
def test(a, b, x, y): |
|
|
print(a, b) |
|
|
print(x, y) |
|
|
|
|
|
fp = partial(test, 'hello', b='world') |
|
|
help(fp) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
unittest.main() |