<s> import unittest <EOL> class TestData ( object ) : <EOL> def __init__ ( self , entity = None , ** kw ) : <EOL> self . entity = entity <EOL> self . __dict__ . update ( kw ) <EOL> class TestComponent ( dict ) : <EOL> def __init__ ( self , entities = ( ) ) : <EOL> self . entities = set ( entities ) <EOL> def __getitem__ ( self , entity ) : <EOL> return self . setdefault ( entity , TestData ( entity ) ) <EOL> class TestField ( object ) : <EOL> def __init__ ( self , component , name ) : <EOL> self . component = component <EOL> self . name = name <EOL> self . cast = int <EOL> class TestAccessor ( object ) : <EOL> def __init__ ( self , field , entities ) : <EOL> self . field = field <EOL> self . entities = entities <EOL> class FieldTestCase ( unittest . TestCase ) : <EOL> def test_basics ( self ) : <EOL> from grease . component . field import Field <EOL> comp = TestComponent ( ) <EOL> f = Field ( comp , "<STR_LIT>" , int ) <EOL> self . assertTrue ( f . component is comp , ( f . component , comp ) ) <EOL> self . assertTrue ( f . type is int , f . type ) <EOL> self . assertEqual ( f . default ( ) , <NUM_LIT:0> ) <EOL> def test_cast ( self ) : <EOL> from grease . component . field import Field <EOL> from grease . geometry import Vec2d <EOL> f = Field ( None , "<STR_LIT:string>" , str ) <EOL> self . assertEqual ( f . cast ( <NUM_LIT> ) , "<STR_LIT>" ) <EOL> f = Field ( None , "<STR_LIT:int>" , int ) <EOL> self . assertEqual ( f . cast ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> f = Field ( None , "<STR_LIT>" , Vec2d ) <EOL> self . assertEqual ( f . cast ( ( <NUM_LIT:11> , <NUM_LIT:12> ) ) , Vec2d ( <NUM_LIT:11> , <NUM_LIT:12> ) ) <EOL> def test_accessor_default_set ( self ) : <EOL> from grease . component . field import Field <EOL> comp = TestComponent ( ) <EOL> f = Field ( comp , "<STR_LIT>" , str , TestAccessor ) <EOL> acc = f . accessor ( ) <EOL> self . assertTrue ( acc . field is f , ( acc . field , f ) ) <EOL> self . assertTrue ( acc . entities is comp . entities , ( acc . entities , comp . entities ) ) <EOL> def test_accessor_subset ( self ) : <EOL> from grease . component . field import Field <EOL> comp = TestComponent ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> f = Field ( comp , "<STR_LIT>" , str , TestAccessor ) <EOL> acc = f . accessor ( set ( [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] ) ) <EOL> self . assertTrue ( acc . field is f , ( acc . field , f ) ) <EOL> self . assertEqual ( acc . entities , set ( [ <NUM_LIT:2> , <NUM_LIT:4> ] ) ) <EOL> class FieldAccessorTestCase ( unittest . TestCase ) : <EOL> def test_iter ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> entities = set ( [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:9> ] ) <EOL> comp = TestComponent ( ) <EOL> accessor = FieldAccessor ( TestField ( comp , '<STR_LIT>' ) , entities ) <EOL> self . assertEqual ( sorted ( iter ( accessor ) ) , [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:9> ] ) <EOL> def test_child_attr ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> entities = set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> comp = TestComponent ( ) <EOL> comp [ <NUM_LIT:1> ] = TestData ( foo = TestData ( bar = <NUM_LIT:100> , baz = TestData ( spam = - <NUM_LIT:1> ) ) ) <EOL> comp [ <NUM_LIT:2> ] = TestData ( foo = TestData ( bar = <NUM_LIT:200> , baz = TestData ( spam = - <NUM_LIT:2> ) ) ) <EOL> comp [ <NUM_LIT:3> ] = TestData ( foo = TestData ( bar = <NUM_LIT> , baz = TestData ( spam = - <NUM_LIT:3> ) ) ) <EOL> accessor = FieldAccessor ( TestField ( comp , '<STR_LIT:foo>' ) , entities ) <EOL> bar_acc = accessor . bar <EOL> self . assertTrue ( isinstance ( bar_acc , FieldAccessor ) ) <EOL> self . assertEqual ( bar_acc [ <NUM_LIT:1> ] , <NUM_LIT:100> ) <EOL> self . assertEqual ( bar_acc [ <NUM_LIT:2> ] , <NUM_LIT:200> ) <EOL> self . assertEqual ( bar_acc [ <NUM_LIT:3> ] , <NUM_LIT> ) <EOL> self . assertRaises ( KeyError , lambda : bar_acc [ <NUM_LIT:4> ] ) <EOL> baz_spam_acc = accessor . baz . spam <EOL> self . assertTrue ( isinstance ( baz_spam_acc , FieldAccessor ) ) <EOL> self . assertEqual ( baz_spam_acc [ <NUM_LIT:1> ] , - <NUM_LIT:1> ) <EOL> self . assertEqual ( baz_spam_acc [ <NUM_LIT:2> ] , - <NUM_LIT:2> ) <EOL> self . assertEqual ( baz_spam_acc [ <NUM_LIT:3> ] , - <NUM_LIT:3> ) <EOL> self . assertRaises ( KeyError , lambda : baz_spam_acc [ <NUM_LIT:4> ] ) <EOL> def test_set ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> entities = set ( [ <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> comp = TestComponent ( ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> comp [ i ] = TestData ( id = i , xy = TestData ( x = i * <NUM_LIT:10> , y = i * - <NUM_LIT:10> ) ) <EOL> id_accessor = FieldAccessor ( TestField ( comp , '<STR_LIT:id>' ) , entities ) <EOL> xy_accessor = FieldAccessor ( TestField ( comp , '<STR_LIT>' ) , entities ) <EOL> id_accessor . __set__ ( <NUM_LIT:10> ) <EOL> xy_accessor . x = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> if i in entities : <EOL> self . assertEqual ( comp [ i ] . id , <NUM_LIT:10> ) <EOL> self . assertEqual ( comp [ i ] . xy . x , <NUM_LIT:0> ) <EOL> self . assertEqual ( comp [ i ] . xy . y , i * - <NUM_LIT:10> ) <EOL> else : <EOL> self . assertEqual ( comp [ i ] . id , i ) <EOL> self . assertEqual ( comp [ i ] . xy . x , i * <NUM_LIT:10> ) <EOL> self . assertEqual ( comp [ i ] . xy . y , i * - <NUM_LIT:10> ) <EOL> def test_set_join ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> entities1 = set ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> entities2 = set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> comp1 = TestComponent ( ) <EOL> comp2 = TestComponent ( ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> comp1 [ i ] = TestData ( foo = i ) <EOL> comp2 [ i ] = TestData ( bar = - i ) <EOL> foo_accessor = FieldAccessor ( TestField ( comp1 , '<STR_LIT:foo>' ) , entities1 ) <EOL> bar_accessor = FieldAccessor ( TestField ( comp2 , '<STR_LIT:bar>' ) , entities2 ) <EOL> foo_accessor . __set__ ( bar_accessor ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> if i in entities1 & entities2 : <EOL> self . assertEqual ( comp1 [ i ] . foo , - i ) <EOL> else : <EOL> self . assertEqual ( comp1 [ i ] . foo , i ) <EOL> self . assertEqual ( comp2 [ i ] . bar , - i ) <EOL> bar_accessor = FieldAccessor ( TestField ( comp2 , '<STR_LIT:bar>' ) , entities1 ) <EOL> foo_accessor . __set__ ( bar_accessor ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> if i in entities1 : <EOL> self . assertEqual ( comp1 [ i ] . foo , - i ) <EOL> else : <EOL> self . assertEqual ( comp1 [ i ] . foo , i ) <EOL> self . assertEqual ( comp2 [ i ] . bar , - i ) <EOL> bar_accessor . __set__ ( foo_accessor ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> if i in entities1 : <EOL> self . assertEqual ( comp1 [ i ] . foo , comp2 [ i ] . bar ) <EOL> else : <EOL> self . assertEqual ( comp1 [ i ] . foo , i ) <EOL> self . assertEqual ( comp2 [ i ] . bar , - i ) <EOL> def test_use_as_bool ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> field = TestField ( TestComponent ( ) , '<STR_LIT:test>' ) <EOL> self . assertFalse ( FieldAccessor ( field , set ( ) ) ) <EOL> self . assertTrue ( FieldAccessor ( field , set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) ) <EOL> def test_repr ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> field = TestField ( TestComponent ( ) , '<STR_LIT:test>' ) <EOL> accessor = FieldAccessor ( field , set ( ) ) <EOL> self . assertTrue ( <EOL> repr ( accessor ) . startswith ( '<STR_LIT>' ) , repr ( accessor ) ) <EOL> self . assertTrue ( <EOL> repr ( accessor . foo . bar ) . startswith ( '<STR_LIT>' ) , <EOL> repr ( accessor ) ) <EOL> def test_query_ops ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> comp = TestComponent ( ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> comp [ i ] = TestData ( x = i * i , pos = TestData ( x = i , y = - i ) ) <EOL> comp [ i + <NUM_LIT:3> ] = TestData ( x = i * i , pos = TestData ( x = i , y = - i ) ) <EOL> comp [ i + <NUM_LIT:6> ] = TestData ( x = i * i , pos = TestData ( x = i , y = - i ) ) <EOL> entities = set ( range ( <NUM_LIT:1> , <NUM_LIT:7> ) ) <EOL> x_accessor = FieldAccessor ( TestField ( comp , '<STR_LIT:x>' ) , entities ) <EOL> self . assertEqual ( x_accessor == <NUM_LIT:4> , set ( [ <NUM_LIT:2> , <NUM_LIT:5> ] ) ) <EOL> self . assertEqual ( x_accessor == <NUM_LIT:0> , set ( ) ) <EOL> self . assertEqual ( x_accessor != <NUM_LIT:1> , set ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> self . assertEqual ( x_accessor != <NUM_LIT> , set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> self . assertEqual ( x_accessor > <NUM_LIT:5> , set ( [ <NUM_LIT:3> , <NUM_LIT:6> ] ) ) <EOL> self . assertEqual ( x_accessor > <NUM_LIT:9> , set ( ) ) <EOL> self . assertEqual ( x_accessor >= <NUM_LIT:4> , set ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> self . assertEqual ( x_accessor >= <NUM_LIT:10> , set ( ) ) <EOL> self . assertEqual ( x_accessor < <NUM_LIT:2> , set ( [ <NUM_LIT:1> , <NUM_LIT:4> ] ) ) <EOL> self . assertEqual ( x_accessor < <NUM_LIT:1> , set ( ) ) <EOL> self . assertEqual ( x_accessor <= <NUM_LIT:4> , set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ) <EOL> self . assertEqual ( x_accessor <= - <NUM_LIT:1> , set ( ) ) <EOL> pos_accessor = FieldAccessor ( TestField ( comp , '<STR_LIT>' ) , entities ) <EOL> self . assertEqual ( pos_accessor . x == <NUM_LIT:3> , set ( [ <NUM_LIT:3> , <NUM_LIT:6> ] ) ) <EOL> self . assertEqual ( pos_accessor . x < <NUM_LIT:3> , set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ) <EOL> def test_query_ops_join ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> comp = TestComponent ( ) <EOL> entities1 = set ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> entities2 = set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:8> ] ) <EOL> comp1 = TestComponent ( ) <EOL> comp2 = TestComponent ( ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> comp1 [ i ] = TestData ( foo = i ) <EOL> comp2 [ i ] = TestData ( bar = <NUM_LIT:6> - i ) <EOL> foo_accessor = FieldAccessor ( TestField ( comp1 , '<STR_LIT:foo>' ) , entities1 ) <EOL> bar_accessor = FieldAccessor ( TestField ( comp2 , '<STR_LIT:bar>' ) , entities2 ) <EOL> self . assertEqual ( foo_accessor == bar_accessor , set ( [ <NUM_LIT:3> ] ) ) <EOL> self . assertEqual ( foo_accessor > bar_accessor , set ( [ <NUM_LIT:8> ] ) ) <EOL> self . assertEqual ( foo_accessor >= bar_accessor , set ( [ <NUM_LIT:3> , <NUM_LIT:8> ] ) ) <EOL> self . assertEqual ( foo_accessor <= bar_accessor , set ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> self . assertEqual ( foo_accessor < bar_accessor , set ( [ <NUM_LIT:2> ] ) ) <EOL> self . assertEqual ( foo_accessor != bar_accessor , set ( [ <NUM_LIT:2> , <NUM_LIT:8> ] ) ) <EOL> def test_inplace_mutators ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> entities = set ( [ <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:7> ] ) <EOL> comp = TestComponent ( ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> comp [ i ] = TestData ( size = i , xy = TestData ( x = i * <NUM_LIT:10> , y = i * - <NUM_LIT:10> ) ) <EOL> xy_accessor = FieldAccessor ( TestField ( comp , '<STR_LIT>' ) , entities ) <EOL> sa = size_accessor = FieldAccessor ( TestField ( comp , '<STR_LIT:size>' ) , entities ) <EOL> size_accessor += <NUM_LIT:1> <EOL> xy_accessor . y += <NUM_LIT:5> <EOL> self . assertTrue ( sa is size_accessor , ( sa , size_accessor ) ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> if i in entities : <EOL> self . assertEqual ( comp [ i ] . size , i + <NUM_LIT:1> ) <EOL> self . assertEqual ( comp [ i ] . xy . y , i * - <NUM_LIT:10> + <NUM_LIT:5> ) <EOL> else : <EOL> self . assertEqual ( comp [ i ] . size , i ) <EOL> self . assertEqual ( comp [ i ] . xy . y , i * - <NUM_LIT:10> ) <EOL> self . assertEqual ( comp [ i ] . xy . x , i * <NUM_LIT:10> ) <EOL> size_accessor -= <NUM_LIT> <EOL> size_accessor *= - <NUM_LIT:12> <EOL> size_accessor /= <NUM_LIT:2> <EOL> size_accessor //= <NUM_LIT:3> <EOL> size_accessor %= <NUM_LIT:7> <EOL> size_accessor **= <NUM_LIT:2> <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> if i in entities : <EOL> expected = i + <NUM_LIT:1> - <NUM_LIT> <EOL> expected *= - <NUM_LIT:12> <EOL> expected /= <NUM_LIT:2> <EOL> expected //= <NUM_LIT:3> <EOL> expected %= <NUM_LIT:7> <EOL> expected **= <NUM_LIT:2> <EOL> self . assertEqual ( comp [ i ] . size , expected ) <EOL> else : <EOL> self . assertEqual ( comp [ i ] . size , i ) <EOL> self . assertEqual ( comp [ i ] . xy . x , i * <NUM_LIT:10> ) <EOL> def test_inplace_mutators_join ( self ) : <EOL> from grease . component . field import FieldAccessor <EOL> comp = TestComponent ( ) <EOL> entities1 = set ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> entities2 = set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:8> ] ) <EOL> comp1 = TestComponent ( ) <EOL> comp2 = TestComponent ( ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> comp1 [ i ] = TestData ( foo = i ) <EOL> comp2 [ i ] = TestData ( bar = i / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> foo_accessor = FieldAccessor ( TestField ( comp1 , '<STR_LIT:foo>' ) , entities1 ) <EOL> bar_accessor = FieldAccessor ( TestField ( comp2 , '<STR_LIT:bar>' ) , entities2 ) <EOL> foo_accessor += bar_accessor <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> if i in entities1 & entities2 : <EOL> self . assertEqual ( comp1 [ i ] . foo , i + i / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> else : <EOL> self . assertEqual ( comp1 [ i ] . foo , i ) <EOL> self . assertEqual ( comp2 [ i ] . bar , i / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> foo_accessor -= bar_accessor <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> self . assertEqual ( comp1 [ i ] . foo , i ) <EOL> self . assertEqual ( comp2 [ i ] . bar , i / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
