<s> """<STR_LIT>""" <EOL> import cooper <EOL> from itertools import repeat <EOL> import unittest <EOL> def make_test_hierarchy ( trace , decorator = lambda x : x , metacls = type ) : <EOL> @ decorator <EOL> class _A ( object ) : <EOL> __metaclass__ = metacls <EOL> @ cooper . cooperate <EOL> def __init__ ( self ) : <EOL> trace . append ( _A . __init__ ) <EOL> @ cooper . cooperative <EOL> def method ( self , mparam ) : <EOL> self . _a_mparam = mparam <EOL> trace . append ( _A . method ) <EOL> @ cooper . cooperative <EOL> def post_method ( self , pmparam ) : <EOL> self . _a_pmparam = pmparam <EOL> trace . append ( _A . post_method ) <EOL> @ decorator <EOL> class _B ( _A ) : <EOL> __metaclass__ = metacls <EOL> @ cooper . cooperate <EOL> def __init__ ( self , b_param = '<STR_LIT>' ) : <EOL> self . _b_param = b_param <EOL> trace . append ( _B . __init__ ) <EOL> @ cooper . cooperate <EOL> def method ( self , mparam , b_mparam = '<STR_LIT>' ) : <EOL> self . _b_mparam = b_mparam <EOL> trace . append ( _B . method ) <EOL> @ cooper . post_cooperate <EOL> def post_method ( self , pmparam , b_pmparam = '<STR_LIT>' ) : <EOL> self . _b_pmparam = b_pmparam <EOL> trace . append ( _B . post_method ) <EOL> @ decorator <EOL> class _C ( _A ) : <EOL> __metaclass__ = metacls <EOL> @ cooper . cooperate <EOL> def __init__ ( self ) : <EOL> trace . append ( _C . __init__ ) <EOL> @ cooper . cooperate <EOL> def method ( self , mparam ) : <EOL> self . _c_mparam = mparam <EOL> trace . append ( _C . method ) <EOL> @ cooper . post_cooperate <EOL> def post_method ( self , pmparam ) : <EOL> self . _c_pmparam = pmparam <EOL> trace . append ( _C . post_method ) <EOL> @ decorator <EOL> class _D ( _B , _C ) : <EOL> __metaclass__ = metacls <EOL> @ cooper . cooperate <EOL> def __init__ ( self , d_param = '<STR_LIT>' ) : <EOL> self . _d_param = d_param <EOL> trace . append ( _D . __init__ ) <EOL> @ cooper . cooperate <EOL> def method ( self , mparam , d_mparam = '<STR_LIT>' ) : <EOL> self . _d_mparam = d_mparam <EOL> trace . append ( _D . method ) <EOL> @ cooper . post_cooperate <EOL> def post_method ( self , pmparam , d_pmparam = '<STR_LIT>' ) : <EOL> self . _d_pmparam = d_pmparam <EOL> trace . append ( _D . post_method ) <EOL> @ decorator <EOL> class _F ( _D , _A ) : <EOL> __metaclass__ = metacls <EOL> return _A , _B , _C , _D , _F <EOL> class TestCoop ( unittest . TestCase ) : <EOL> cls_decorator = cooper . cooperative_class <EOL> cls_meta = type <EOL> def setUp ( self ) : <EOL> self . _trace = [ ] <EOL> self . _A , self . _B , self . _C , self . _D , self . _F = make_test_hierarchy ( <EOL> self . _trace , <EOL> decorator = self . cls_decorator . im_func , <EOL> metacls = self . cls_meta ) <EOL> def test_init_parameter_passing ( self ) : <EOL> obj = self . _D ( ) <EOL> self . assertEqual ( obj . _b_param , '<STR_LIT>' ) <EOL> self . assertEqual ( obj . _d_param , '<STR_LIT>' ) <EOL> obj = self . _D ( b_param = '<STR_LIT>' ) <EOL> self . assertEqual ( obj . _b_param , '<STR_LIT>' ) <EOL> self . assertEqual ( obj . _d_param , '<STR_LIT>' ) <EOL> obj = self . _D ( d_param = '<STR_LIT>' ) <EOL> self . assertEqual ( obj . _b_param , '<STR_LIT>' ) <EOL> self . assertEqual ( obj . _d_param , '<STR_LIT>' ) <EOL> obj = self . _D ( d_param = '<STR_LIT>' , <EOL> b_param = '<STR_LIT>' ) <EOL> self . assertEqual ( obj . _b_param , '<STR_LIT>' ) <EOL> self . assertEqual ( obj . _d_param , '<STR_LIT>' ) <EOL> def test_init_check_no_positional ( self ) : <EOL> def make_cls ( ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Bad ( object ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperate <EOL> def __init__ ( self , positional ) : <EOL> pass <EOL> self . assertRaises ( cooper . CooperativeError , make_cls ) <EOL> def test_init_check_no_variadic ( self ) : <EOL> def make_cls ( ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Bad ( object ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperate <EOL> def __init__ ( self , * a ) : <EOL> pass <EOL> self . assertRaises ( cooper . CooperativeError , make_cls ) <EOL> def test_init_check_no_variadic_keywords ( self ) : <EOL> def make_cls ( ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Bad ( object ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperate <EOL> def __init__ ( self , ** k ) : <EOL> pass <EOL> self . assertRaises ( cooper . CooperativeError , make_cls ) <EOL> def test_init_must_cooperate ( self ) : <EOL> def make_cls ( ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Bad ( object ) : <EOL> __metaclass__ = self . cls_meta <EOL> def __init__ ( self ) : <EOL> pass <EOL> self . assertRaises ( cooper . CooperativeError , make_cls ) <EOL> def test_init_must_override ( self ) : <EOL> def make_cls ( ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Bad ( object ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperative <EOL> def __init__ ( self ) : <EOL> pass <EOL> self . assertRaises ( cooper . CooperativeError , make_cls ) <EOL> def test_super_params_sends_params ( self ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Fixed ( self . _F ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperate_with_params ( b_param = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> pass <EOL> obj = _Fixed ( ) <EOL> self . assertEqual ( obj . _b_param , '<STR_LIT>' ) <EOL> def test_manual_init ( self ) : <EOL> outer_self = self <EOL> @ self . cls_decorator . im_func <EOL> class _Manual ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . manual_cooperate <EOL> def __init__ ( self , * a , ** k ) : <EOL> super ( _Manual , self ) . __init__ ( * a , ** k ) <EOL> outer_self . _trace . append ( _Manual . __init__ ) <EOL> self . _clear_trace ( ) <EOL> _Manual ( ) <EOL> self . _check_trace_calls_with_mro ( _Manual . __init__ ) <EOL> def test_can_mix_non_cooperative_superclass_single_inherit ( self ) : <EOL> class NonCooperativeSuperClass ( object ) : <EOL> pass <EOL> @ self . cls_decorator . im_func <EOL> class _Good ( NonCooperativeSuperClass ) : <EOL> __metaclass__ = self . cls_meta <EOL> self . assertTrue ( isinstance ( _Good ( ) , _Good ) ) <EOL> def test_can_not_mix_non_cooperative_superclass_multi_inherit ( self ) : <EOL> class NonCooperativeSuperClass1 ( object ) : <EOL> pass <EOL> class NonCooperativeSuperClass2 ( object ) : <EOL> pass <EOL> def make_class ( ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Bad ( NonCooperativeSuperClass1 , <EOL> NonCooperativeSuperClass2 ) : <EOL> __metaclass__ = self . cls_meta <EOL> self . assertRaises ( cooper . CooperativeError , make_class ) <EOL> def test_can_mix_non_cooperative_subclass ( self ) : <EOL> class _Good ( self . _D ) : <EOL> pass <EOL> self . _clear_trace ( ) <EOL> _Good ( ) <EOL> self . _check_trace_calls_with_mro ( self . _D . __init__ ) <EOL> def test_abstract_method_forbids_instantiation ( self ) : <EOL> @ self . cls_decorator . im_func <EOL> class _ABC ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . abstract <EOL> def abstract_method ( self ) : <EOL> return <NUM_LIT:0> <EOL> self . assertRaises ( TypeError , _ABC ) <EOL> def test_override_abstract_method_enables_instantiation ( self ) : <EOL> @ self . cls_decorator . im_func <EOL> class _ABC ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . abstract <EOL> def abstract_method ( self ) : <EOL> self . _result = <NUM_LIT:1> <EOL> @ self . cls_decorator . im_func <EOL> class _Concrete ( _ABC ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperate <EOL> def abstract_method ( self ) : <EOL> return self . _result <EOL> self . assertEqual ( _Concrete ( ) . abstract_method ( ) , <NUM_LIT:1> ) <EOL> def test_compatible_abstract_method_forbids_instantiation ( self ) : <EOL> import abc <EOL> @ self . cls_decorator . im_func <EOL> class _ABC ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ abc . abstractmethod <EOL> def abstract_method ( self ) : <EOL> return <NUM_LIT:0> <EOL> self . assertRaises ( TypeError , _ABC ) <EOL> def test_compatible_override_abstract_method_enables_instantiation ( self ) : <EOL> import abc <EOL> @ self . cls_decorator . im_func <EOL> class _ABC ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ abc . abstractmethod <EOL> def abstract_method ( self ) : <EOL> return <NUM_LIT:0> <EOL> class _Concrete ( _ABC ) : <EOL> def abstract_method ( self ) : <EOL> return super ( _Concrete , self ) . abstract_method ( ) <EOL> self . assertEqual ( _Concrete ( ) . abstract_method ( ) , <NUM_LIT:0> ) <EOL> def test_conflict_raises_error ( self ) : <EOL> @ self . cls_decorator . im_func <EOL> class _A1 ( object ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperative <EOL> def method ( self ) : <EOL> pass <EOL> @ self . cls_decorator . im_func <EOL> class _A2 ( object ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperative <EOL> def method ( self ) : <EOL> pass <EOL> def make_class ( ) : <EOL> @ self . cls_decorator . im_func <EOL> class _A12 ( _A1 , _A2 ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . cooperate <EOL> def method ( self ) : <EOL> pass <EOL> self . assertRaises ( cooper . CooperativeError , make_class ) <EOL> def test_mro_call_order ( self ) : <EOL> for cls in ( self . _D , self . _C , self . _B , self . _A ) : <EOL> obj = cls ( ) <EOL> self . _clear_trace ( ) <EOL> obj . method ( <NUM_LIT:1> ) <EOL> self . _check_trace_calls_with_mro ( cls . method ) <EOL> def test_post_mro_call_order ( self ) : <EOL> for cls in ( self . _D , self . _C , self . _B , self . _A ) : <EOL> obj = cls ( ) <EOL> self . _clear_trace ( ) <EOL> obj . post_method ( <NUM_LIT:1> ) <EOL> self . _check_trace_calls_with_mro ( cls . post_method , reverse = True ) <EOL> def test_mro_init_call_order ( self ) : <EOL> for cls in ( self . _D , self . _C , self . _B , self . _A ) : <EOL> self . _clear_trace ( ) <EOL> cls ( ) <EOL> self . _check_trace_calls_with_mro ( cls . __init__ ) <EOL> def test_mro_does_not_decorate_undefined_init ( self ) : <EOL> self . _clear_trace ( ) <EOL> self . _F ( ) <EOL> self . _check_trace_calls_with_mro ( self . _D . __init__ ) <EOL> def test_inner_cooperate ( self ) : <EOL> outer_self = self <EOL> @ self . cls_decorator . im_func <EOL> class _Cls ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . inner_cooperate <EOL> def method ( self , next_method , param ) : <EOL> next_method ( b_mparam = '<STR_LIT>' ) <EOL> outer_self . _trace . append ( _Cls . method ) <EOL> obj = _Cls ( ) <EOL> self . _clear_trace ( ) <EOL> obj . method ( <NUM_LIT:1> ) <EOL> self . _check_trace_calls_with_mro ( _Cls . method ) <EOL> self . assertEqual ( obj . _b_mparam , '<STR_LIT>' ) <EOL> def test_inner_error_call_too_much ( self ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Cls ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . inner_cooperate <EOL> def method ( self , next_method , param ) : <EOL> next_method ( ) <EOL> next_method ( ) <EOL> obj = _Cls ( ) <EOL> self . assertRaises ( cooper . CooperativeError , obj . method , <NUM_LIT:1> ) <EOL> def test_inner_error_not_call ( self ) : <EOL> @ self . cls_decorator . im_func <EOL> class _Cls ( self . _D ) : <EOL> __metaclass__ = self . cls_meta <EOL> @ cooper . inner_cooperate <EOL> def method ( self , next_method , param ) : <EOL> pass <EOL> obj = _Cls ( ) <EOL> self . assertRaises ( cooper . CooperativeError , obj . method , <NUM_LIT:1> ) <EOL> def _clear_trace ( self ) : <EOL> self . _trace [ : ] = [ ] <EOL> def _check_trace_calls_with_mro ( self , method , reverse = False ) : <EOL> cls = method . im_class <EOL> name = method . __name__ <EOL> mro = cls . __mro__ [ : - <NUM_LIT:1> ] <EOL> self . assertEqual ( zip ( mro if reverse else mro [ : : - <NUM_LIT:1> ] , repeat ( name ) ) , <EOL> [ ( m . im_class , m . __name__ ) for m in self . _trace ] ) <EOL> class TestCoopMeta ( TestCoop ) : <EOL> cls_decorator = lambda x : x <EOL> cls_meta = cooper . CooperativeMeta <EOL> def test_meta_works_on_subclasses ( self ) : <EOL> outer_self = self <EOL> class _NewClass ( self . _D ) : <EOL> @ cooper . cooperate <EOL> def __init__ ( self ) : <EOL> outer_self . _trace . append ( _NewClass . __init__ ) <EOL> self . _clear_trace ( ) <EOL> _NewClass ( ) <EOL> self . _check_trace_calls_with_mro ( _NewClass . __init__ ) <EOL> class _TestBase ( object ) : <EOL> def __init__ ( self , param = None , * a , ** k ) : <EOL> super ( _TestBase , self ) . __init__ ( * a , ** k ) <EOL> assert param == '<STR_LIT>' <EOL> class _TestDeriv ( _TestBase ) : <EOL> def __init__ ( self , * a , ** k ) : <EOL> super ( _TestDeriv , self ) . __init__ ( param = '<STR_LIT>' , * a , ** k ) <EOL> class _CoopTestBase ( cooper . Cooperative ) : <EOL> @ cooper . cooperate <EOL> def __init__ ( self , param = None ) : <EOL> assert param == '<STR_LIT>' <EOL> class _CoopTestDeriv ( _CoopTestBase ) : <EOL> @ cooper . cooperate_with_params ( param = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> pass <EOL> class _SimpleTestBase ( object ) : <EOL> def __init__ ( self , * a , ** k ) : <EOL> super ( _SimpleTestBase , self ) . __init__ ( * a , ** k ) <EOL> class _SimpleTestDeriv ( _SimpleTestBase ) : <EOL> def __init__ ( self , * a , ** k ) : <EOL> super ( _SimpleTestDeriv , self ) . __init__ ( * a , ** k ) <EOL> class _CoopSimpleTestBase ( cooper . Cooperative ) : <EOL> @ cooper . cooperate <EOL> def __init__ ( self ) : <EOL> pass <EOL> class _CoopSimpleTestDeriv ( _CoopSimpleTestBase ) : <EOL> @ cooper . cooperate <EOL> def __init__ ( self ) : <EOL> pass <EOL> class _SuperSimpleTestBase ( object ) : pass <EOL> class _SuperSimpleTestDeriv ( _SuperSimpleTestBase ) : pass <EOL> class _SuperCoopSimpleTestBase ( cooper . Cooperative ) : pass <EOL> class _SuperCoopSimpleTestDeriv ( _SuperCoopSimpleTestBase ) : pass <EOL> class TestCoopPerformance ( unittest . TestCase ) : <EOL> test_number = <NUM_LIT:1> << <NUM_LIT:8> <EOL> def test_performance_overhead_override ( self ) : <EOL> import timeit <EOL> t1 = min ( timeit . repeat ( _SimpleTestDeriv , number = self . test_number ) ) <EOL> t2 = min ( timeit . repeat ( _CoopSimpleTestDeriv , number = self . test_number ) ) <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , t1 <EOL> print "<STR_LIT>" , t2 <EOL> print "<STR_LIT>" , t2 / t1 <EOL> def test_performance_overhead_no_override ( self ) : <EOL> import timeit <EOL> t1 = min ( timeit . repeat ( _SuperSimpleTestDeriv , number = self . test_number ) ) <EOL> t2 = min ( timeit . repeat ( _SuperCoopSimpleTestDeriv , number = self . test_number ) ) <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , t1 <EOL> print "<STR_LIT>" , t2 <EOL> print "<STR_LIT>" , t2 / t1 <EOL> def test_performance_overhead_with_params ( self ) : <EOL> import timeit <EOL> t1 = min ( timeit . repeat ( _TestDeriv , number = self . test_number ) ) <EOL> t2 = min ( timeit . repeat ( _CoopTestDeriv , number = self . test_number ) ) <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , t1 <EOL> print "<STR_LIT>" , t2 <EOL> print "<STR_LIT>" , t2 / t1 </s>
