<s> """<STR_LIT>""" <EOL> from envisage . api import Application , ExtensionPoint <EOL> from envisage . api import ExtensionRegistry <EOL> from traits . api import HasTraits , Int , List , TraitError <EOL> from traits . testing . unittest_tools import unittest <EOL> class TestBase ( HasTraits ) : <EOL> """<STR_LIT>""" <EOL> extension_registry = None <EOL> class ExtensionPointTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . registry = Application ( extension_registry = ExtensionRegistry ( ) ) <EOL> TestBase . extension_registry = self . registry <EOL> return <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def test_invalid_extension_point_type ( self ) : <EOL> """<STR_LIT>""" <EOL> self . failUnlessRaises ( TypeError , ExtensionPoint , Int , '<STR_LIT>' ) <EOL> return <EOL> def test_no_reference_to_extension_registry ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . set_extensions ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Foo ( HasTraits ) : <EOL> x = ExtensionPoint ( List ( Int ) , id = '<STR_LIT>' ) <EOL> f = Foo ( ) <EOL> self . failUnlessRaises ( ValueError , getattr , f , '<STR_LIT:x>' ) <EOL> return <EOL> def test_extension_point_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> class Foo ( TestBase ) : <EOL> x = ExtensionPoint ( id = '<STR_LIT>' ) <EOL> def _x_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . x_changed_called = True <EOL> return <EOL> f = Foo ( ) <EOL> ExtensionPoint . connect_extension_point_traits ( f ) <EOL> registry . set_extensions ( '<STR_LIT>' , [ <NUM_LIT> , '<STR_LIT>' , True ] ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( f . x ) ) <EOL> self . assertEqual ( [ <NUM_LIT> , '<STR_LIT>' , True ] , f . x ) <EOL> self . assert_ ( f . x_changed_called ) <EOL> f . x_changed_called = False <EOL> ExtensionPoint . disconnect_extension_point_traits ( f ) <EOL> registry . set_extensions ( '<STR_LIT>' , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> self . assertEqual ( False , f . x_changed_called ) <EOL> return <EOL> def test_untyped_extension_point ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . set_extensions ( '<STR_LIT>' , [ <NUM_LIT> , '<STR_LIT>' , True ] ) <EOL> class Foo ( TestBase ) : <EOL> x = ExtensionPoint ( id = '<STR_LIT>' ) <EOL> f = Foo ( ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( f . x ) ) <EOL> self . assertEqual ( [ <NUM_LIT> , '<STR_LIT>' , True ] , f . x ) <EOL> g = Foo ( ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( g . x ) ) <EOL> self . assertEqual ( [ <NUM_LIT> , '<STR_LIT>' , True ] , g . x ) <EOL> return <EOL> def test_typed_extension_point ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . set_extensions ( '<STR_LIT>' , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> class Foo ( TestBase ) : <EOL> x = ExtensionPoint ( List ( Int ) , id = '<STR_LIT>' ) <EOL> f = Foo ( ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( f . x ) ) <EOL> self . assertEqual ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , f . x ) <EOL> g = Foo ( ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( g . x ) ) <EOL> self . assertEqual ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , g . x ) <EOL> return <EOL> def test_invalid_extension_point ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> registry . set_extensions ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Foo ( TestBase ) : <EOL> x = ExtensionPoint ( List ( Int ) , id = '<STR_LIT>' ) <EOL> f = Foo ( ) <EOL> self . failUnlessRaises ( TraitError , getattr , f , '<STR_LIT:x>' ) <EOL> return <EOL> def test_extension_point_with_no_id ( self ) : <EOL> """<STR_LIT>""" <EOL> def factory ( ) : <EOL> class Foo ( TestBase ) : <EOL> x = ExtensionPoint ( List ( Int ) ) <EOL> self . failUnlessRaises ( ValueError , factory ) <EOL> return <EOL> def test_set_untyped_extension_point ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> class Foo ( TestBase ) : <EOL> x = ExtensionPoint ( id = '<STR_LIT>' ) <EOL> f = Foo ( ) <EOL> f . x = [ <NUM_LIT> ] <EOL> self . assertEqual ( [ <NUM_LIT> ] , registry . get_extensions ( '<STR_LIT>' ) ) <EOL> return <EOL> def test_set_typed_extension_point ( self ) : <EOL> """<STR_LIT>""" <EOL> registry = self . registry <EOL> registry . add_extension_point ( self . _create_extension_point ( '<STR_LIT>' ) ) <EOL> class Foo ( TestBase ) : <EOL> x = ExtensionPoint ( List ( Int ) , id = '<STR_LIT>' ) <EOL> f = Foo ( ) <EOL> f . x = [ <NUM_LIT> ] <EOL> self . assertEqual ( [ <NUM_LIT> ] , registry . get_extensions ( '<STR_LIT>' ) ) <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>
