<s> """<STR_LIT>""" <EOL> import gc <EOL> import sys <EOL> import weakref <EOL> from flexx . util . testing import run_tests_if_main , skipif , skip , raises <EOL> from flexx import event <EOL> def test_method_handlers ( ) : <EOL> events1 = [ ] <EOL> events2 = [ ] <EOL> class Foo ( event . HasEvents ) : <EOL> @ event . connect ( '<STR_LIT>' ) <EOL> def handle1 ( self , * events ) : <EOL> events1 . extend ( events ) <EOL> @ event . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle2 ( self , * events ) : <EOL> events2 . extend ( events ) <EOL> foo = Foo ( ) <EOL> with event . loop : <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> assert len ( events1 ) == <NUM_LIT:1> <EOL> assert len ( events2 ) == <NUM_LIT:2> <EOL> assert isinstance ( foo . handle1 , event . _handler . Handler ) <EOL> assert repr ( foo . handle1 ) <EOL> assert hasattr ( foo . handle1 , '<STR_LIT>' ) <EOL> assert foo . handle1 . get_name ( ) == '<STR_LIT>' <EOL> assert foo . handle1 . get_connection_info ( ) == [ ( '<STR_LIT>' , [ '<STR_LIT>' , ] ) ] <EOL> assert foo . handle2 . get_connection_info ( ) == [ ( '<STR_LIT>' , [ '<STR_LIT>' , ] ) , ( '<STR_LIT>' , [ '<STR_LIT>' , ] ) ] <EOL> with raises ( AttributeError ) : <EOL> foo . handle1 = <NUM_LIT:3> <EOL> with raises ( AttributeError ) : <EOL> del foo . handle1 <EOL> def test_method_handlers_nodecorator ( ) : <EOL> events1 = [ ] <EOL> events2 = [ ] <EOL> class Foo ( event . HasEvents ) : <EOL> def _handle1 ( self , * events ) : <EOL> events1 . extend ( events ) <EOL> handle1 = event . connect ( _handle1 , '<STR_LIT>' ) <EOL> def _handle2 ( self , * events ) : <EOL> events2 . extend ( events ) <EOL> handle2 = event . connect ( _handle2 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> foo = Foo ( ) <EOL> with event . loop : <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> assert len ( events1 ) == <NUM_LIT:1> <EOL> assert len ( events2 ) == <NUM_LIT:2> <EOL> assert isinstance ( foo . handle1 , event . _handler . Handler ) <EOL> assert repr ( foo . handle1 ) <EOL> assert hasattr ( foo . handle1 , '<STR_LIT>' ) <EOL> assert foo . handle1 . get_name ( ) == '<STR_LIT>' <EOL> with raises ( AttributeError ) : <EOL> foo . handle1 = <NUM_LIT:3> <EOL> with raises ( AttributeError ) : <EOL> del foo . handle1 <EOL> def test_func_handlers ( ) : <EOL> events1 = [ ] <EOL> events2 = [ ] <EOL> class Foo ( event . HasEvents ) : <EOL> pass <EOL> foo = Foo ( ) <EOL> @ foo . connect ( '<STR_LIT>' ) <EOL> def handle1 ( * events ) : <EOL> events1 . extend ( events ) <EOL> @ foo . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle2 ( * events ) : <EOL> events2 . extend ( events ) <EOL> with event . loop : <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> assert len ( events1 ) == <NUM_LIT:1> <EOL> assert len ( events2 ) == <NUM_LIT:2> <EOL> assert isinstance ( handle1 , event . _handler . Handler ) <EOL> assert repr ( handle1 ) <EOL> assert hasattr ( handle1 , '<STR_LIT>' ) <EOL> assert handle1 . get_name ( ) == '<STR_LIT>' <EOL> assert handle1 . get_connection_info ( ) == [ ( '<STR_LIT>' , [ '<STR_LIT>' , ] ) ] <EOL> assert handle2 . get_connection_info ( ) == [ ( '<STR_LIT>' , [ '<STR_LIT>' , ] ) , ( '<STR_LIT>' , [ '<STR_LIT>' , ] ) ] <EOL> def test_func_handlers_nodecorator ( ) : <EOL> events1 = [ ] <EOL> events2 = [ ] <EOL> class Foo ( event . HasEvents ) : <EOL> pass <EOL> foo = Foo ( ) <EOL> def _handle1 ( * events ) : <EOL> events1 . extend ( events ) <EOL> handle1 = foo . connect ( _handle1 , '<STR_LIT>' ) <EOL> def _handle2 ( * events ) : <EOL> events2 . extend ( events ) <EOL> handle2 = foo . connect ( _handle2 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with event . loop : <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> foo . emit ( '<STR_LIT>' , { } ) <EOL> assert len ( events1 ) == <NUM_LIT:1> <EOL> assert len ( events2 ) == <NUM_LIT:2> <EOL> assert isinstance ( handle1 , event . _handler . Handler ) <EOL> assert repr ( handle1 ) <EOL> assert hasattr ( handle1 , '<STR_LIT>' ) <EOL> assert handle1 . get_name ( ) == '<STR_LIT>' <EOL> def test_func_handlers_with_method_decorator ( ) : <EOL> @ event . connect ( '<STR_LIT:x>' ) <EOL> def foo ( * events ) : <EOL> pass <EOL> assert isinstance ( foo , event . _handler . HandlerDescriptor ) <EOL> assert repr ( foo ) <EOL> def test_method_handler_invoking ( ) : <EOL> called = [ ] <EOL> class MyObject ( event . HasEvents ) : <EOL> @ event . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handler ( * events ) : <EOL> called . append ( len ( events ) ) <EOL> @ event . connect ( '<STR_LIT>' ) <EOL> def handler3 ( self , * events ) : <EOL> called . append ( len ( events ) ) <EOL> h = MyObject ( ) <EOL> handler = h . handler <EOL> handler ( ) <EOL> handler . handle_now ( ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> handler . handle_now ( ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> handler . handle_now ( ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> handler . handle_now ( ) <EOL> handler ( ) <EOL> handler . handle_now ( ) <EOL> h . handler3 ( ) <EOL> handler . handle_now ( ) <EOL> assert called == [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> def test_func_handler_invoking ( ) : <EOL> called = [ ] <EOL> h = event . HasEvents ( ) <EOL> @ h . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handler ( * events ) : <EOL> called . append ( len ( events ) ) <EOL> handler ( ) <EOL> handler . handle_now ( ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> handler . handle_now ( ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> handler . handle_now ( ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> h . emit ( '<STR_LIT>' , { } ) <EOL> handler . handle_now ( ) <EOL> handler ( ) <EOL> handler . handle_now ( ) <EOL> assert called == [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:0> ] <EOL> def test_connecting ( ) : <EOL> h = event . HasEvents ( ) <EOL> with raises ( RuntimeError ) : <EOL> @ event . connect <EOL> def foo ( * events ) : <EOL> pass <EOL> with raises ( RuntimeError ) : <EOL> @ h . connect <EOL> def foo ( * events ) : <EOL> pass <EOL> with raises ( ValueError ) : <EOL> @ event . connect ( <NUM_LIT:3> ) <EOL> def foo ( * events ) : <EOL> pass <EOL> with raises ( ValueError ) : <EOL> @ h . connect ( <NUM_LIT:3> ) <EOL> def foo ( * events ) : <EOL> pass <EOL> with raises ( TypeError ) : <EOL> event . connect ( '<STR_LIT:x>' ) ( <NUM_LIT:3> ) <EOL> with raises ( TypeError ) : <EOL> h . connect ( '<STR_LIT:x>' ) ( <NUM_LIT:3> ) <EOL> with raises ( RuntimeError ) : <EOL> h . xx = None <EOL> @ h . connect ( '<STR_LIT>' ) <EOL> def foo ( * events ) : <EOL> pass <EOL> def test_exceptions ( ) : <EOL> h = event . HasEvents ( ) <EOL> @ h . connect ( '<STR_LIT:foo>' ) <EOL> def handle_foo ( * events ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> h . emit ( '<STR_LIT:foo>' , { } ) <EOL> sys . last_traceback = None <EOL> assert sys . last_traceback is None <EOL> event . loop . iter ( ) <EOL> event . loop . iter ( ) <EOL> assert sys . last_traceback <EOL> with raises ( ZeroDivisionError ) : <EOL> handle_foo ( ) <EOL> def test_dispose1 ( ) : <EOL> h = event . HasEvents ( ) <EOL> @ h . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handler ( * events ) : <EOL> pass <EOL> handler_ref = weakref . ref ( handler ) <EOL> del handler <EOL> gc . collect ( ) <EOL> assert handler_ref ( ) is not None <EOL> handler_ref ( ) . dispose ( ) <EOL> gc . collect ( ) <EOL> assert handler_ref ( ) is None <EOL> def test_dispose2 ( ) : <EOL> h = event . HasEvents ( ) <EOL> @ h . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handler ( * events ) : <EOL> pass <EOL> handler_ref = weakref . ref ( handler ) <EOL> del handler <EOL> gc . collect ( ) <EOL> assert handler_ref ( ) is not None <EOL> h . dispose ( ) <EOL> gc . collect ( ) <EOL> assert handler_ref ( ) is None <EOL> run_tests_if_main ( ) </s>
