<s> import reg <EOL> from reg import ClassIndex , KeyIndex <EOL> import morepath <EOL> from morepath . error import ConfigError <EOL> import pytest <EOL> def setup_module ( module ) : <EOL> morepath . disable_implicit ( ) <EOL> def test_dispatch ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch ( '<STR_LIT>' ) <EOL> def f ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , obj = Foo ) <EOL> def f_foo ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> @ App . function ( f , obj = Bar ) <EOL> def f_bar ( obj ) : <EOL> return "<STR_LIT:bar>" <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT:foo>' <EOL> assert f ( Bar ( ) , lookup = lookup ) == '<STR_LIT:bar>' <EOL> assert f ( Other ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> def test_dispatch_external_predicates ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def f ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex ) <EOL> def f_obj ( obj ) : <EOL> return obj . __class__ <EOL> @ App . function ( f , model = Foo ) <EOL> def f_foo ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> @ App . function ( f , model = Bar ) <EOL> def f_bar ( obj ) : <EOL> return "<STR_LIT:bar>" <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT:foo>' <EOL> assert f ( Bar ( ) , lookup = lookup ) == '<STR_LIT:bar>' <EOL> assert f ( Other ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> def test_dispatch_external_predicates_predicate_fallback ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def f ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex ) <EOL> def f_obj ( obj ) : <EOL> return obj . __class__ <EOL> @ App . predicate_fallback ( f , f_obj ) <EOL> def f_obj_fallback ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Foo ) <EOL> def f_foo ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> @ App . function ( f , model = Bar ) <EOL> def f_bar ( obj ) : <EOL> return "<STR_LIT:bar>" <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT:foo>' <EOL> assert f ( Bar ( ) , lookup = lookup ) == '<STR_LIT:bar>' <EOL> assert f ( Other ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> def test_dispatch_external_predicates_ordering_after ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def f ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex ) <EOL> def pred_obj ( obj ) : <EOL> return obj . __class__ <EOL> @ App . predicate ( f , name = '<STR_LIT:name>' , default = '<STR_LIT>' , index = KeyIndex , after = pred_obj ) <EOL> def pred_name ( name ) : <EOL> return name <EOL> @ App . function ( f , model = Foo , name = '<STR_LIT>' ) <EOL> def f_foo_default ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Foo , name = '<STR_LIT>' ) <EOL> def f_foo_edit ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Bar , name = '<STR_LIT>' ) <EOL> def f_bar_default ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Bar , name = '<STR_LIT>' ) <EOL> def f_bar_edit ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Bar ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Foo ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Bar ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Other ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Other ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> def test_dispatch_external_predicates_ordering_before ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def f ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . predicate ( f , name = '<STR_LIT:name>' , default = '<STR_LIT>' , index = KeyIndex ) <EOL> def pred_name ( name ) : <EOL> return name <EOL> @ App . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex , <EOL> before = pred_name ) <EOL> def pred_obj ( obj ) : <EOL> return obj . __class__ <EOL> @ App . function ( f , model = Foo , name = '<STR_LIT>' ) <EOL> def f_foo_default ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Foo , name = '<STR_LIT>' ) <EOL> def f_foo_edit ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Bar , name = '<STR_LIT>' ) <EOL> def f_bar_default ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Bar , name = '<STR_LIT>' ) <EOL> def f_bar_edit ( obj , name ) : <EOL> return "<STR_LIT>" <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Bar ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Foo ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Bar ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Other ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Other ( ) , '<STR_LIT>' , lookup = lookup ) == '<STR_LIT>' <EOL> def test_dispatch_external_override_fallback ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Sub ( App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def f ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex ) <EOL> def f_obj ( obj ) : <EOL> return obj . __class__ <EOL> @ App . predicate_fallback ( f , f_obj ) <EOL> def f_obj_fallback ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ Sub . predicate_fallback ( f , f_obj ) <EOL> def f_obj_fallback_sub ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Foo ) <EOL> def f_foo ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> @ Sub . function ( f , model = Foo ) <EOL> def f_foo_sub ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Bar ) <EOL> def f_bar ( obj ) : <EOL> return "<STR_LIT:bar>" <EOL> s = Sub ( ) <EOL> lookup = s . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Bar ( ) , lookup = lookup ) == '<STR_LIT:bar>' <EOL> assert f ( Other ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT:foo>' <EOL> assert f ( Bar ( ) , lookup = lookup ) == '<STR_LIT:bar>' <EOL> assert f ( Other ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> def test_dispatch_external_override_predicate ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Sub ( App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def f ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex ) <EOL> def f_obj ( obj ) : <EOL> return obj . __class__ <EOL> @ Sub . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex ) <EOL> def f_obj_sub ( obj ) : <EOL> return Bar <EOL> @ App . predicate_fallback ( f , f_obj ) <EOL> def f_obj_fallback ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f , model = Foo ) <EOL> def f_foo ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> @ Sub . function ( f , model = Foo ) <EOL> def f_foo_sub ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> @ App . function ( f , model = Bar ) <EOL> def f_bar ( obj ) : <EOL> return "<STR_LIT:bar>" <EOL> @ Sub . function ( f , model = Bar ) <EOL> def f_bar_sub ( obj ) : <EOL> return "<STR_LIT>" <EOL> s = Sub ( ) <EOL> lookup = s . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Bar ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> assert f ( Other ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT:foo>' <EOL> assert f ( Bar ( ) , lookup = lookup ) == '<STR_LIT:bar>' <EOL> assert f ( Other ( ) , lookup = lookup ) == '<STR_LIT>' <EOL> def test_wrong_predicate_arguments_single ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ reg . dispatch ( '<STR_LIT>' ) <EOL> def f ( obj ) : <EOL> return "<STR_LIT>" <EOL> class Foo ( object ) : <EOL> pass <EOL> @ App . function ( f , wrong = Foo ) <EOL> def f_foo ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> a = App ( ) <EOL> assert f ( Foo ( ) , lookup = a . lookup ) == '<STR_LIT>' <EOL> def test_wrong_predicate_arguments_multi ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ reg . dispatch ( '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> def f ( a , b ) : <EOL> return "<STR_LIT>" <EOL> class Foo ( object ) : <EOL> pass <EOL> @ App . function ( f , wrong = Foo ) <EOL> def f_foo ( a , b ) : <EOL> return "<STR_LIT:foo>" <EOL> a = App ( ) <EOL> assert f ( Foo ( ) , Foo ( ) , lookup = a . lookup ) == '<STR_LIT>' <EOL> def test_predicate_not_for_dispatch_external_predicates ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ reg . dispatch ( '<STR_LIT:a>' ) <EOL> def f ( a ) : <EOL> pass <EOL> @ App . predicate ( f , name = '<STR_LIT>' , default = None , index = ClassIndex ) <EOL> def model_predicate ( a ) : <EOL> return a . __class__ <EOL> with pytest . raises ( ConfigError ) : <EOL> App ( ) . lookup <EOL> def test_dispatch_external_predicates_without_predicate_directives ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Foo ( object ) : <EOL> pass <EOL> class Bar ( object ) : <EOL> pass <EOL> class Other ( object ) : <EOL> pass <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def f ( obj ) : <EOL> return "<STR_LIT>" <EOL> @ App . function ( f ) <EOL> def f_foo ( obj ) : <EOL> return "<STR_LIT:foo>" <EOL> a = App ( ) <EOL> lookup = a . lookup <EOL> assert f ( Foo ( ) , lookup = lookup ) == '<STR_LIT:foo>' </s>
