<s> """<STR_LIT>""" <EOL> from envisage . api import ExtensionPoint <EOL> from envisage . api import bind_extension_point <EOL> from traits . api import HasTraits , List <EOL> from traits . testing . unittest_tools import unittest <EOL> from envisage . tests . mutable_extension_registry import ( <EOL> MutableExtensionRegistry <EOL> ) <EOL> def listener ( obj , trait_name , old , new ) : <EOL> """<STR_LIT>""" <EOL> listener . obj = obj <EOL> listener . trait_name = trait_name <EOL> listener . old = old <EOL> listener . new = new <EOL> return <EOL> class ExtensionPointBindingTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . extension_registry = MutableExtensionRegistry ( ) <EOL> ExtensionPoint . extension_registry = self . extension_registry <EOL> return <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def test_untyped_extension_point ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . extension_registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . add_extension ( '<STR_LIT>' , <NUM_LIT> ) <EOL> class Foo ( HasTraits ) : <EOL> x = List <EOL> f = Foo ( ) <EOL> f . on_trait_change ( listener ) <EOL> bind_extension_point ( f , '<STR_LIT:x>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( f . x ) ) <EOL> self . assertEqual ( <NUM_LIT> , f . x [ <NUM_LIT:0> ] ) <EOL> registry . add_extension ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( f . x ) ) <EOL> self . assert_ ( <NUM_LIT> in f . x ) <EOL> self . assert_ ( '<STR_LIT>' in f . x ) <EOL> self . assertEqual ( f , listener . obj ) <EOL> self . assertEqual ( '<STR_LIT>' , listener . trait_name ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( listener . new . added ) ) <EOL> self . assert_ ( '<STR_LIT>' in listener . new . added ) <EOL> return <EOL> def test_set_extensions_via_trait ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . extension_registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . add_extension ( '<STR_LIT>' , <NUM_LIT> ) <EOL> class Foo ( HasTraits ) : <EOL> x = List <EOL> f = Foo ( ) <EOL> f . on_trait_change ( listener ) <EOL> bind_extension_point ( f , '<STR_LIT:x>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( f . x ) ) <EOL> self . assertEqual ( <NUM_LIT> , f . x [ <NUM_LIT:0> ] ) <EOL> f . x = [ '<STR_LIT>' ] <EOL> self . assertEqual ( <NUM_LIT:1> , len ( f . x ) ) <EOL> self . assert_ ( '<STR_LIT>' in f . x ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( registry . get_extensions ( '<STR_LIT>' ) ) ) <EOL> self . assert_ ( '<STR_LIT>' in registry . get_extensions ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( f , listener . obj ) <EOL> self . assertEqual ( '<STR_LIT:x>' , listener . trait_name ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( listener . new ) ) <EOL> self . assert_ ( '<STR_LIT>' in listener . new ) <EOL> return <EOL> def test_set_extensions_via_registry ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . extension_registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . add_extension ( '<STR_LIT>' , <NUM_LIT> ) <EOL> class Foo ( HasTraits ) : <EOL> x = List <EOL> f = Foo ( ) <EOL> f . on_trait_change ( listener ) <EOL> bind_extension_point ( f , '<STR_LIT:x>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( f . x ) ) <EOL> self . assertEqual ( <NUM_LIT> , f . x [ <NUM_LIT:0> ] ) <EOL> registry . set_extensions ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( f . x ) ) <EOL> self . assert_ ( '<STR_LIT>' in f . x ) <EOL> self . assertEqual ( f , listener . obj ) <EOL> self . assertEqual ( '<STR_LIT:x>' , listener . trait_name ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( listener . new ) ) <EOL> self . assert_ ( '<STR_LIT>' in listener . new ) <EOL> return <EOL> def test_explicit_extension_registry ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . extension_registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . add_extension ( '<STR_LIT>' , <NUM_LIT> ) <EOL> class Foo ( HasTraits ) : <EOL> x = List <EOL> f = Foo ( ) <EOL> f . on_trait_change ( listener ) <EOL> extension_registry = MutableExtensionRegistry ( ) <EOL> bind_extension_point ( f , '<STR_LIT:x>' , '<STR_LIT>' , extension_registry ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( f . x ) ) <EOL> return <EOL> def test_should_be_able_to_bind_multiple_traits_on_a_single_object ( self ) : <EOL> registry = self . extension_registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> class Foo ( HasTraits ) : <EOL> x = List <EOL> y = List <EOL> f = Foo ( ) <EOL> bind_extension_point ( f , '<STR_LIT:x>' , '<STR_LIT>' , registry ) <EOL> bind_extension_point ( f , '<STR_LIT:y>' , '<STR_LIT>' , registry ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( f . x ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( f . y ) ) <EOL> registry . add_extension ( '<STR_LIT>' , <NUM_LIT> ) <EOL> registry . add_extensions ( '<STR_LIT>' , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( f . x ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( f . y ) ) <EOL> return <EOL> def _create_extension_point ( self , id , trait_type = List , desc = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return ExtensionPoint ( id = id , trait_type = trait_type , desc = desc ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
