<s> """<STR_LIT>""" <EOL> import sys <EOL> import unittest <EOL> from traits . api import ( HasTraits , Float , Instance , <EOL> Str , List , Bool ) <EOL> from tvtk . api import tvtk <EOL> from apptools . scripting . api import ( Recorder , recordable , <EOL> set_recorder ) <EOL> class Toy ( HasTraits ) : <EOL> color = Str <EOL> type = Str <EOL> ignore = Bool ( False , record = False ) <EOL> class Child ( HasTraits ) : <EOL> name = Str ( '<STR_LIT>' ) <EOL> age = Float ( <NUM_LIT> ) <EOL> property = Instance ( tvtk . Property , ( ) , record = True ) <EOL> toy = Instance ( Toy , record = True ) <EOL> friends = List ( Str ) <EOL> @ recordable <EOL> def grow ( self , x ) : <EOL> """<STR_LIT>""" <EOL> self . age += x <EOL> self . f ( <NUM_LIT:1> ) <EOL> @ recordable <EOL> def f ( self , args ) : <EOL> """<STR_LIT>""" <EOL> return args <EOL> def not_recordable ( self ) : <EOL> pass <EOL> class Parent ( HasTraits ) : <EOL> children = List ( Child , record = True ) <EOL> recorder = Instance ( Recorder , record = False ) <EOL> class Test ( HasTraits ) : <EOL> recorder = Instance ( HasTraits ) <EOL> _ignore = Bool ( False ) <EOL> ignore_ = Bool ( False ) <EOL> class TestRecorder ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . tape = Recorder ( ) <EOL> set_recorder ( self . tape ) <EOL> p = Parent ( ) <EOL> c = Child ( ) <EOL> toy = Toy ( color = '<STR_LIT>' , type = '<STR_LIT>' ) <EOL> c . toy = toy <EOL> p . children . append ( c ) <EOL> self . p = p <EOL> return <EOL> def tearDown ( self ) : <EOL> self . tape . clear ( ) <EOL> set_recorder ( None ) <EOL> return <EOL> def test_unique_name ( self ) : <EOL> "<STR_LIT>" <EOL> t = tvtk . XMLUnstructuredGridWriter ( ) <EOL> tape = self . tape <EOL> self . assertEqual ( tape . _get_unique_name ( t ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( tape . _get_unique_name ( t ) , <EOL> '<STR_LIT>' ) <EOL> t = Toy ( ) <EOL> self . assertEqual ( tape . _get_unique_name ( t ) , <EOL> '<STR_LIT>' ) <EOL> t = ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . assertEqual ( tape . _get_unique_name ( t ) , <EOL> '<STR_LIT>' ) <EOL> l = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> self . assertEqual ( tape . _get_unique_name ( l ) , <EOL> '<STR_LIT>' ) <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> } <EOL> self . assertEqual ( tape . _get_unique_name ( d ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( tape . _get_unique_name ( <NUM_LIT:1> ) , <EOL> '<STR_LIT>' ) <EOL> def test_record ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> p = self . p <EOL> c = p . children [ <NUM_LIT:0> ] <EOL> toy = c . toy <EOL> tape . recording = True <EOL> tape . register ( p ) <EOL> self . assertEqual ( tape , p . recorder ) <EOL> self . assertEqual ( tape . get_script_id ( p ) , '<STR_LIT>' ) <EOL> self . assertEqual ( tape . get_object_path ( p ) , '<STR_LIT>' ) <EOL> self . assertEqual ( tape . get_script_id ( c ) , '<STR_LIT>' ) <EOL> self . assertEqual ( tape . get_object_path ( c ) , '<STR_LIT>' ) <EOL> self . assertEqual ( tape . get_script_id ( toy ) , '<STR_LIT>' ) <EOL> self . assertEqual ( tape . get_object_path ( toy ) , <EOL> '<STR_LIT>' ) <EOL> c . name = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> c . age = <NUM_LIT> <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> c . property . representation = '<STR_LIT:w>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> c . property . color = ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> toy . color = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> toy . type = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> toy . ignore = True <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> tape . recording = False <EOL> toy . type = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> n = len ( tape . lines ) <EOL> tape . unregister ( p ) <EOL> c . property . representation = '<STR_LIT>' <EOL> toy . type = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( n , len ( tape . lines ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( tape . _registry ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( tape . _reverse_registry ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( tape . _known_ids ) ) <EOL> def test_recorded_trait_replaced ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> p = self . p <EOL> c = p . children [ <NUM_LIT:0> ] <EOL> toy = c . toy <EOL> tape . recording = True <EOL> tape . register ( p ) <EOL> toy . color = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> t1 = Toy ( name = '<STR_LIT>' ) <EOL> c . toy = t1 <EOL> t1 . color = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def test_clear ( self ) : <EOL> "<STR_LIT>" <EOL> p = self . p <EOL> tape = self . tape <EOL> tape . register ( p ) <EOL> tape . clear ( ) <EOL> self . assertEqual ( p . recorder , None ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( tape . _registry ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( tape . _reverse_registry ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( tape . _known_ids ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( tape . _name_map ) ) <EOL> def test_create_object ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> tape . recording = True <EOL> t = Toy ( ) <EOL> tape . register ( t ) <EOL> t . type = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:3> ] [ - <NUM_LIT:10> : ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> t1 = Toy ( ) <EOL> tape . register ( t1 , known = True ) <EOL> t1 . type = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> def test_list_items_changed ( self ) : <EOL> "<STR_LIT>" <EOL> p = self . p <EOL> tape = self . tape <EOL> child = p . children [ <NUM_LIT:0> ] <EOL> tape . register ( p , known = True ) <EOL> tape . recording = True <EOL> child . friends = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> child . friends [ <NUM_LIT:1> : ] = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> child . friends [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> child1 = Child ( ) <EOL> tape . register ( child1 ) <EOL> p . children . append ( child1 ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> del p . children [ <NUM_LIT:1> ] <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> p . children [ <NUM_LIT:0> ] = child1 <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> def test_path_change_on_list ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> p = self . p <EOL> child1 = Child ( ) <EOL> p . children . append ( child1 ) <EOL> tape . register ( p ) <EOL> tape . recording = True <EOL> self . assertEqual ( tape . get_object_path ( child1 ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( tape . get_script_id ( child1 ) , <EOL> '<STR_LIT>' ) <EOL> del p . children [ <NUM_LIT:0> ] <EOL> self . assertEqual ( tape . get_object_path ( child1 ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( tape . get_script_id ( child1 ) , <EOL> '<STR_LIT>' ) <EOL> def test_write_script_id_in_namespace ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> tape . recording = True <EOL> tape . write_script_id_in_namespace ( '<STR_LIT:foo>' ) <EOL> def test_recorder_and_ignored ( self ) : <EOL> "<STR_LIT>" <EOL> t = Test ( ) <EOL> self . assertEqual ( t . recorder , None ) <EOL> self . assertEqual ( t . _ignore , False ) <EOL> self . assertEqual ( t . ignore_ , False ) <EOL> tape = Recorder ( ) <EOL> tape . register ( t ) <EOL> tape . recording = True <EOL> self . assertEqual ( t . recorder , tape ) <EOL> t . _ignore = True <EOL> t . ignore_ = True <EOL> self . assertEqual ( len ( tape . script . strip ( ) ) , <NUM_LIT:0> ) <EOL> def test_record_function ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> c = self . p . children [ <NUM_LIT:0> ] <EOL> tape . register ( c ) <EOL> tape . recording = True <EOL> c . age = <NUM_LIT:11> <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> c . f ( c . toy ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> c . f ( <NUM_LIT:1> ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> c . grow ( <NUM_LIT:1> ) <EOL> self . assertEqual ( c . age , <NUM_LIT> ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> c . not_recordable ( ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> @ recordable <EOL> def func ( x , y ) : <EOL> return x , y <EOL> result = func ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def test_non_has_traits ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> p = self . p <EOL> c = p . children [ <NUM_LIT:0> ] <EOL> class A ( object ) : <EOL> @ recordable <EOL> def __init__ ( self , x , y = <NUM_LIT:1> ) : <EOL> self . x = x <EOL> self . y = y <EOL> @ recordable <EOL> def f ( self , x , y ) : <EOL> return x , y <EOL> @ recordable <EOL> def g ( self , x ) : <EOL> return x <EOL> def not_recordable ( self ) : <EOL> pass <EOL> tape . register ( p ) <EOL> tape . recording = True <EOL> a = A ( x = <NUM_LIT:1> ) <EOL> a . f ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:3> ] [ - <NUM_LIT:8> : ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> result = a . f ( p , c ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:3> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> result = a . g ( c ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> a . not_recordable ( ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> r = a . g ( Toy ( ) ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:3> ] [ - <NUM_LIT:10> : ] , "<STR_LIT>" , <EOL> '<STR_LIT:\n>' . join ( tape . lines [ - <NUM_LIT:5> : ] ) ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def test_set_script_id ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> p = self . p <EOL> c = p . children [ <NUM_LIT:0> ] <EOL> tape . register ( p , script_id = '<STR_LIT>' ) <EOL> tape . recording = True <EOL> self . assertEqual ( tape . get_script_id ( p ) , '<STR_LIT>' ) <EOL> c1 = Child ( ) <EOL> tape . register ( c1 ) <EOL> self . assertEqual ( tape . get_script_id ( c1 ) , '<STR_LIT>' ) <EOL> p . children . append ( c1 ) <EOL> self . assertEqual ( tape . lines [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> def test_save ( self ) : <EOL> "<STR_LIT>" <EOL> tape = self . tape <EOL> p = self . p <EOL> c = p . children [ <NUM_LIT:0> ] <EOL> toy = c . toy <EOL> tape . register ( p ) <EOL> tape . recording = True <EOL> toy . type = '<STR_LIT>' <EOL> tape . recording = False <EOL> tape . unregister ( p ) <EOL> import io <EOL> f = io . StringIO ( ) <EOL> tape . save ( f ) <EOL> expect = [ "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> f . seek ( <NUM_LIT:0> ) <EOL> lines = f . readlines ( ) <EOL> self . assertEqual ( expect , lines ) <EOL> f . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
