<s> import unittest <EOL> import itertools <EOL> class TestWorld ( object ) : <EOL> def __init__ ( self , ** kw ) : <EOL> self . __dict__ . update ( kw ) <EOL> self . components = self <EOL> self . entities = set ( ) <EOL> self . new_entity_id = itertools . count ( ) . __next__ <EOL> self . new_entity_id ( ) <EOL> for comp in list ( kw . values ( ) ) : <EOL> comp . world = self <EOL> class TestComponent ( dict ) : <EOL> def __init__ ( self ) : <EOL> self . entities = set ( ) <EOL> def set ( self , entity ) : <EOL> data = TestData ( ) <EOL> self [ entity ] = data <EOL> self . entities . add ( entity ) <EOL> return data <EOL> def remove ( self , entity ) : <EOL> del self [ entity ] <EOL> class TestData ( object ) : <EOL> attr = '<STR_LIT>' <EOL> def __init__ ( self , ** kw ) : <EOL> self . __dict__ . update ( kw ) <EOL> class EntityTestCase ( unittest . TestCase ) : <EOL> def test_repr ( self ) : <EOL> from grease import Entity <EOL> entity = Entity ( TestWorld ( ) ) <EOL> self . assertTrue ( repr ( entity ) . startswith ( <EOL> '<STR_LIT>' % entity . entity_id ) , <EOL> ( '<STR_LIT>' % entity . entity_id , repr ( entity ) ) ) <EOL> def test_accessor_getattr_for_nonexistant_component ( self ) : <EOL> from grease import Entity <EOL> comp = TestComponent ( ) <EOL> world = TestWorld ( test = comp ) <EOL> entity = Entity ( world ) <EOL> self . assertTrue ( entity not in comp ) <EOL> self . assertRaises ( AttributeError , getattr , entity , '<STR_LIT:foo>' ) <EOL> def test_accessor_getattr_for_non_member_entity ( self ) : <EOL> from grease import Entity <EOL> comp = TestComponent ( ) <EOL> world = TestWorld ( test = comp ) <EOL> entity = Entity ( world ) <EOL> accessor = entity . test <EOL> self . assertFalse ( entity in comp ) <EOL> self . assertRaises ( AttributeError , getattr , accessor , '<STR_LIT>' ) <EOL> def test_accessor_getattr_for_member_entity ( self ) : <EOL> from grease import Entity <EOL> comp = TestComponent ( ) <EOL> world = TestWorld ( test = comp ) <EOL> entity = Entity ( world ) <EOL> comp . set ( entity ) <EOL> self . assertTrue ( entity in comp ) <EOL> self . assertEqual ( entity . test . attr , '<STR_LIT>' ) <EOL> def test_accessor_setattr_adds_non_member_entity ( self ) : <EOL> from grease import Entity <EOL> comp = TestComponent ( ) <EOL> world = TestWorld ( test = comp ) <EOL> entity = Entity ( world ) <EOL> self . assertFalse ( entity in comp ) <EOL> entity . test . attr = '<STR_LIT>' <EOL> self . assertEqual ( entity . test . attr , '<STR_LIT>' ) <EOL> self . assertTrue ( entity in comp ) <EOL> def test_accessor_setattr_for_member_entity ( self ) : <EOL> from grease import Entity <EOL> comp = TestComponent ( ) <EOL> world = TestWorld ( test = comp ) <EOL> entity = Entity ( world ) <EOL> comp . set ( entity ) <EOL> self . assertNotEqual ( entity . test . attr , '<STR_LIT>' ) <EOL> entity . test . attr = '<STR_LIT>' <EOL> self . assertTrue ( entity in comp ) <EOL> self . assertEqual ( entity . test . attr , '<STR_LIT>' ) <EOL> def test_eq ( self ) : <EOL> from grease import Entity <EOL> world = TestWorld ( ) <EOL> e1 = Entity ( world ) <EOL> e2 = Entity ( world ) <EOL> self . assertNotEqual ( e1 , e2 ) <EOL> e2 . entity_id = e1 . entity_id <EOL> self . assertEqual ( e1 , e2 ) <EOL> otherworld = TestWorld ( ) <EOL> e3 = Entity ( otherworld ) <EOL> self . assertNotEqual ( e1 , e3 ) <EOL> self . assertNotEqual ( e2 , e3 ) <EOL> e3 . entity_id = e1 . entity_id <EOL> self . assertNotEqual ( e1 , e3 ) <EOL> self . assertNotEqual ( e2 , e3 ) <EOL> def test_delattr ( self ) : <EOL> from grease import Entity <EOL> comp = TestComponent ( ) <EOL> world = TestWorld ( test = comp ) <EOL> entity = Entity ( world ) <EOL> comp . set ( entity ) <EOL> self . assertTrue ( entity in comp ) <EOL> del entity . test <EOL> self . assertFalse ( entity in comp ) <EOL> def test_entity_id ( self ) : <EOL> from grease import Entity <EOL> world = TestWorld ( ) <EOL> entity1 = Entity ( world ) <EOL> entity2 = Entity ( world ) <EOL> self . assertTrue ( entity1 . entity_id > <NUM_LIT:0> ) <EOL> self . assertTrue ( entity2 . entity_id > <NUM_LIT:0> ) <EOL> self . assertNotEqual ( entity1 . entity_id , entity2 . entity_id ) <EOL> def test_delete_exists ( self ) : <EOL> from grease import Entity <EOL> world = TestWorld ( ) <EOL> self . assertEqual ( world . entities , set ( ) ) <EOL> entity1 = Entity ( world ) <EOL> entity2 = Entity ( world ) <EOL> self . assertEqual ( world . entities , set ( [ entity1 , entity2 ] ) ) <EOL> self . assertTrue ( entity1 . exists ) <EOL> self . assertTrue ( entity2 . exists ) <EOL> entity1 . delete ( ) <EOL> self . assertEqual ( world . entities , set ( [ entity2 ] ) ) <EOL> self . assertFalse ( entity1 . exists ) <EOL> self . assertTrue ( entity2 . exists ) <EOL> entity2 . delete ( ) <EOL> self . assertEqual ( world . entities , set ( ) ) <EOL> self . assertFalse ( entity1 . exists ) <EOL> self . assertFalse ( entity2 . exists ) <EOL> def test_entity_subclass_slots ( self ) : <EOL> from grease import Entity <EOL> class NewEntity ( Entity ) : <EOL> pass <EOL> world = TestWorld ( ) <EOL> entity = NewEntity ( world ) <EOL> self . assertRaises ( AttributeError , setattr , entity , '<STR_LIT>' , <NUM_LIT> ) <EOL> def test_entity_subclass_cant_have_slots ( self ) : <EOL> from grease import Entity <EOL> self . assertRaises ( TypeError , <EOL> type , '<STR_LIT>' , ( Entity , ) , { '<STR_LIT>' : ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) } ) <EOL> def test_entity_subclass_init ( self ) : <EOL> from grease import Entity <EOL> stuff = [ ] <EOL> class TestEntity ( Entity ) : <EOL> def __init__ ( self , world , other ) : <EOL> stuff . append ( world ) <EOL> stuff . append ( other ) <EOL> world = TestWorld ( ) <EOL> TestEntity ( world , self ) <EOL> self . assertEqual ( stuff , [ world , self ] ) <EOL> class EntityComponentAccessorTestCase ( unittest . TestCase ) : <EOL> def test_getattr ( self ) : <EOL> from grease . entity import EntityComponentAccessor <EOL> from grease import Entity <EOL> world = TestWorld ( ) <EOL> entity = Entity ( world ) <EOL> component = { entity : TestData ( foo = <NUM_LIT:5> ) } <EOL> accessor = EntityComponentAccessor ( component , entity ) <EOL> self . assertEqual ( accessor . foo , <NUM_LIT:5> ) <EOL> self . assertRaises ( AttributeError , getattr , accessor , '<STR_LIT:bar>' ) <EOL> entity2 = Entity ( world ) <EOL> accessor = EntityComponentAccessor ( component , entity2 ) <EOL> self . assertRaises ( AttributeError , getattr , accessor , '<STR_LIT:foo>' ) <EOL> self . assertRaises ( AttributeError , getattr , accessor , '<STR_LIT:bar>' ) <EOL> def test_setattr_member_entity ( self ) : <EOL> from grease . entity import EntityComponentAccessor <EOL> from grease import Entity <EOL> world = TestWorld ( ) <EOL> entity = Entity ( world ) <EOL> data = TestData ( foo = <NUM_LIT:5> ) <EOL> accessor = EntityComponentAccessor ( { entity : data } , entity ) <EOL> self . assertEqual ( data . foo , <NUM_LIT:5> ) <EOL> accessor . foo = <NUM_LIT> <EOL> self . assertEqual ( data . foo , <NUM_LIT> ) <EOL> accessor . bar = '<STR_LIT>' <EOL> self . assertEqual ( data . bar , '<STR_LIT>' ) <EOL> def test_setattr_nonmember_entity ( self ) : <EOL> from grease . entity import EntityComponentAccessor <EOL> from grease import Entity <EOL> world = TestWorld ( ) <EOL> entity = Entity ( world ) <EOL> component = TestComponent ( ) <EOL> accessor = EntityComponentAccessor ( component , entity ) <EOL> self . assertRaises ( AttributeError , getattr , entity , '<STR_LIT>' ) <EOL> self . assertTrue ( entity not in component ) <EOL> accessor . baz = <NUM_LIT:1000> <EOL> self . assertTrue ( entity in component ) <EOL> self . assertEqual ( accessor . baz , <NUM_LIT:1000> ) <EOL> self . assertEqual ( component [ entity ] . baz , <NUM_LIT:1000> ) <EOL> def test_truthiness ( self ) : <EOL> from grease . entity import EntityComponentAccessor <EOL> from grease import Entity <EOL> world = TestWorld ( ) <EOL> entity = Entity ( world ) <EOL> component = TestComponent ( ) <EOL> accessor = EntityComponentAccessor ( component , entity ) <EOL> self . assertFalse ( accessor ) <EOL> component [ entity ] = <NUM_LIT> <EOL> self . assertTrue ( accessor ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
