def test_save_to_context(self): context = dict() def dont_extract(number): return number @extracts("arg") def extract_arg(number): return number @extracts("arg1", "arg2") def extract_arg1_and_arg2(num1, num2): return num1, num2 @extracts("args") def extract_args(num1, num2): return num1, num2 @extracts("from_callable") class CallableExtractor: def __call__(self, number): return number class Extractor: def method(self, number): return number @classmethod def class_method(cls, number): return number @staticmethod def static_method(number): return number wrapped_method = extracts("from_method")(Extractor().method) wrapped_class_method = extracts("from_class_method")(Extractor().class_method) wrapped_static_method = extracts("from_static_method")(Extractor().static_method) save_to_context(context, dont_extract)(-1) save_to_context(context, extract_arg)(0) save_to_context(context, extract_arg1_and_arg2)(1, 2) save_to_context(context, extract_args)(1, 2) save_to_context(context, CallableExtractor())(3) save_to_context(context, wrapped_method)(4) save_to_context(context, wrapped_class_method)(5) save_to_context(context, wrapped_static_method)(6) assert_that(context, is_(equal_to(dict( arg=0, arg1=1, arg2=2, args=(1, 2), from_callable=3, from_method=4, from_class_method=5, from_static_method=6, ))))
def test_chain_in_a_chain(self): chain = Chain( extracts("arg")(lambda: 20), Chain(lambda arg: arg * 10, ), ) assert_that( chain(), is_(equal_to(200)), )
def test_chain_functions_can_use_decorators(self): chain = Chain( extracts("arg")(lambda: 20), lambda arg: arg * 10, ) assert_that( chain(), is_(equal_to(200)), )
def test_chain_in_a_chain(self): chain = Chain( extracts("arg")(lambda: 20), Chain( lambda arg: arg * 10, ), ) assert_that( chain(), is_(equal_to(200)), )
def test_overriding_extracts(self): context = dict() @extracts("arg") def extractarg(number): return number func = extracts("param")(extractarg) save_to_context(context, func)(10) assert_that(context, is_(equal_to(dict(param=10))))