<s> """<STR_LIT>""" <EOL> import sys <EOL> import weakref <EOL> from flexx . util . testing import run_tests_if_main , skipif , raises <EOL> from flexx . react import HasSignals , input , connect , lazy , Signal , InputSignal <EOL> def test_signals_on_classes_are_descriptors ( ) : <EOL> shown = [ ] <EOL> class Test ( HasSignals ) : <EOL> @ input <EOL> def title ( v = '<STR_LIT>' ) : <EOL> return str ( v ) <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def show_title1 ( v = '<STR_LIT>' ) : <EOL> shown . append ( v ) <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def show_title2 ( self , v = '<STR_LIT>' ) : <EOL> shown . append ( v ) <EOL> assert len ( shown ) == <NUM_LIT:0> <EOL> assert Test . title . not_connected <EOL> assert Test . show_title1 . not_connected <EOL> assert Test . show_title2 . not_connected <EOL> raises ( RuntimeError , Test . show_title1 . connect ) <EOL> t = Test ( ) <EOL> assert len ( shown ) == <NUM_LIT:2> <EOL> def test_hassignals_without_self ( ) : <EOL> title_lengths = [ ] <EOL> class Test ( HasSignals ) : <EOL> @ input <EOL> def title ( v = '<STR_LIT>' ) : <EOL> return str ( v ) <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def title_len ( v ) : <EOL> return len ( v ) <EOL> @ connect ( '<STR_LIT>' ) <EOL> def show_title ( v ) : <EOL> title_lengths . append ( v ) <EOL> t = Test ( ) <EOL> assert t . title . _self is t <EOL> assert set ( t . __signals__ ) == set ( [ '<STR_LIT:title>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert len ( title_lengths ) == <NUM_LIT:1> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:0> <EOL> t . title ( '<STR_LIT:foo>' ) <EOL> assert len ( title_lengths ) == <NUM_LIT:2> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:3> <EOL> def test_hassignals_with_self ( ) : <EOL> title_lengths = [ ] <EOL> class Test ( HasSignals ) : <EOL> @ input <EOL> def title ( self , v = '<STR_LIT>' ) : <EOL> return str ( v ) <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def title_len ( self , v ) : <EOL> return len ( v ) <EOL> @ connect ( '<STR_LIT>' ) <EOL> def show_title ( self , v ) : <EOL> title_lengths . append ( v ) <EOL> t = Test ( ) <EOL> assert set ( t . __signals__ ) == set ( [ '<STR_LIT:title>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert len ( title_lengths ) == <NUM_LIT:1> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:0> <EOL> t . title ( '<STR_LIT:foo>' ) <EOL> assert len ( title_lengths ) == <NUM_LIT:2> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:3> <EOL> def test_hassignals_init ( ) : <EOL> class Str ( InputSignal ) : <EOL> def __init__ ( self , func = None , upstream = [ ] , * args ) : <EOL> InputSignal . __init__ ( self , str , [ ] , * args ) <EOL> class Test ( HasSignals ) : <EOL> name = Str <EOL> @ input <EOL> def title ( self , v = '<STR_LIT:a>' ) : <EOL> return str ( v ) <EOL> raises ( ValueError , Test , foo = <NUM_LIT:3> ) <EOL> t = Test ( ) <EOL> assert t . title ( ) == '<STR_LIT:a>' <EOL> assert t . name ( ) == '<STR_LIT>' <EOL> t = Test ( title = '<STR_LIT:b>' , name = '<STR_LIT:c>' ) <EOL> assert t . title ( ) == '<STR_LIT:b>' <EOL> assert t . name ( ) == '<STR_LIT:c>' <EOL> def test_anyclass ( ) : <EOL> title_lengths = [ ] <EOL> class Test ( object ) : <EOL> @ input <EOL> def title ( self , v = '<STR_LIT>' ) : <EOL> return str ( v ) <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def title_len ( self , v ) : <EOL> return len ( v ) <EOL> @ connect ( '<STR_LIT>' ) <EOL> def show_title ( self , v ) : <EOL> title_lengths . append ( v ) <EOL> t = Test ( ) <EOL> assert not hasattr ( t , '<STR_LIT>' ) <EOL> assert len ( title_lengths ) == <NUM_LIT:0> <EOL> t . show_title . connect ( False ) <EOL> assert len ( title_lengths ) == <NUM_LIT:0> <EOL> t . title . connect ( ) <EOL> t . title_len . connect ( ) <EOL> t . show_title . connect ( ) <EOL> assert len ( title_lengths ) == <NUM_LIT:1> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:0> <EOL> t . title ( '<STR_LIT:foo>' ) <EOL> assert len ( title_lengths ) == <NUM_LIT:2> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:3> <EOL> def test_connection_locals1 ( ) : <EOL> class Test ( HasSignals ) : <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def title_len ( self , v ) : <EOL> return len ( v ) <EOL> t = Test ( ) <EOL> assert t . title_len . not_connected <EOL> @ input <EOL> def title ( v = '<STR_LIT>' ) : <EOL> return str ( v ) <EOL> t . connect_signals ( ) <EOL> assert not t . title_len . not_connected <EOL> assert t . title_len ( ) == <NUM_LIT:0> <EOL> title ( '<STR_LIT:foo>' ) <EOL> assert t . title_len ( ) == <NUM_LIT:3> <EOL> def test_connection_locals2 ( ) : <EOL> class Test ( HasSignals ) : <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def title_len ( self , v ) : <EOL> return len ( v ) <EOL> def create_instance ( ) : <EOL> @ input <EOL> def title ( v = '<STR_LIT>' ) : <EOL> return str ( v ) <EOL> return Test ( ) <EOL> t = create_instance ( ) <EOL> assert t . title_len . not_connected <EOL> def test_func_name ( ) : <EOL> with raises ( RuntimeError ) : <EOL> class Test ( HasSignals ) : <EOL> s1 = Signal ( lambda x : x , [ ] ) <EOL> with raises ( RuntimeError ) : <EOL> class Test ( HasSignals ) : <EOL> s2 = Signal ( float , [ ] ) <EOL> def test_props ( ) : <EOL> from flexx . react . decorators import prop <EOL> title_lengths = [ ] <EOL> class Test ( HasSignals ) : <EOL> @ prop <EOL> def title ( self , v = '<STR_LIT>' ) : <EOL> return str ( v ) <EOL> @ connect ( '<STR_LIT:title>' ) <EOL> def title_len ( self , v ) : <EOL> return len ( v ) <EOL> @ connect ( '<STR_LIT>' ) <EOL> def show_title ( self , v ) : <EOL> title_lengths . append ( v ) <EOL> t = Test ( ) <EOL> assert set ( t . __signals__ ) == set ( [ '<STR_LIT:title>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert len ( title_lengths ) == <NUM_LIT:1> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:0> <EOL> t . title = '<STR_LIT:foo>' <EOL> assert len ( title_lengths ) == <NUM_LIT:2> <EOL> assert title_lengths [ - <NUM_LIT:1> ] == <NUM_LIT:3> <EOL> @ skipif ( '<STR_LIT>' in sys . builtin_module_names , reason = '<STR_LIT>' ) <EOL> def test_no_memory_leak ( ) : <EOL> def stub ( v = '<STR_LIT>' ) : pass <EOL> class Foo ( HasSignals ) : <EOL> @ input <EOL> def parent ( self , v ) : <EOL> return v <EOL> class Test ( HasSignals ) : <EOL> def __init__ ( self ) : <EOL> self . foo = Foo ( parent = self ) <EOL> self . bar = Signal ( stub , [ ] ) <EOL> HasSignals . __init__ ( self ) <EOL> @ connect ( '<STR_LIT>' ) <EOL> def title_len ( self , v ) : <EOL> return len ( v ) <EOL> t = Test ( ) <EOL> wt = weakref . ref ( t ) <EOL> assert sys . getrefcount ( t ) >= <NUM_LIT:3> <EOL> t . disconnect_signals ( ) <EOL> t . foo . disconnect_signals ( ) <EOL> t . bar . disconnect ( ) <EOL> del t <EOL> assert wt ( ) is None <EOL> run_tests_if_main ( ) </s>
