1 /** 2 * Unit tests for entityState.d 3 * 4 */ 5 module entityStateMachine_test; 6 7 import ashd.core.component : Component; 8 import ashd.core.entity : Entity; 9 import ashd.fsm.componentTypeProvider: ComponentTypeProvider; 10 import ashd.fsm.entityState : EntityState; 11 import ashd.fsm.entityStateMachine : EntityStateMachine; 12 import ashd.fsm.IComponentProvider : IComponentProvider; 13 14 15 int main() 16 { 17 (new EntityStateMachineTests).enterStateAddsStatesComponents(); 18 (new EntityStateMachineTests).enterSecondStateAddsSecondStatesComponents(); 19 (new EntityStateMachineTests).enterSecondStateRemovesFirstStatesComponents(); 20 (new EntityStateMachineTests).enterSecondStateDoesNotRemoveOverlappingComponents(); 21 (new EntityStateMachineTests).enterSecondStateRemovesDifferentComponentsOfSameType(); 22 23 return 0; 24 } 25 26 27 28 public class EntityStateMachineTests 29 { 30 31 EntityStateMachine mFsm; 32 Entity mEntity; 33 34 public this() 35 { 36 mEntity = new Entity(); 37 mFsm = new EntityStateMachine( mEntity ); 38 } 39 40 public void enterStateAddsStatesComponents() 41 { 42 EntityState state = new EntityState(); 43 MockComponent component = new MockComponent(); 44 state.add( typeid(MockComponent) ).withInstance!MockComponent( component ); 45 mFsm.addState( "test", state ); 46 mFsm.changeState( "test" ); 47 assert( mEntity.get!MockComponent() is component ); 48 } 49 50 public void enterSecondStateAddsSecondStatesComponents() 51 { 52 EntityState state1 = new EntityState(); 53 MockComponent component1 = new MockComponent(); 54 state1.add( typeid(MockComponent) ).withInstance!MockComponent( component1 ); 55 mFsm.addState( "test1", state1 ); 56 mFsm.changeState( "test1" ); 57 58 EntityState state2 = new EntityState(); 59 MockComponent2 component2 = new MockComponent2(); 60 state2.add( typeid(MockComponent2) ).withInstance!MockComponent2( component2 ); 61 mFsm.addState( "test2", state2 ); 62 mFsm.changeState( "test2" ); 63 64 assert( mEntity.get!MockComponent2() is component2 ); 65 } 66 67 public void enterSecondStateRemovesFirstStatesComponents() 68 { 69 EntityState state1 = new EntityState(); 70 MockComponent component1 = new MockComponent(); 71 state1.add( typeid(MockComponent) ).withInstance!MockComponent( component1 ); 72 mFsm.addState( "test1", state1 ); 73 mFsm.changeState( "test1" ); 74 75 EntityState state2 = new EntityState(); 76 MockComponent2 component2 = new MockComponent2(); 77 state2.add( typeid(MockComponent2) ).withInstance!MockComponent2( component2 ); 78 mFsm.addState( "test2", state2 ); 79 mFsm.changeState( "test2" ); 80 81 assert( mEntity.has!MockComponent() == false ); 82 } 83 84 public void enterSecondStateDoesNotRemoveOverlappingComponents() 85 { 86 class Watcher 87 { 88 void failIfCalled( Entity entity_a, ClassInfo class_a ) 89 { 90 assert( "Component was removed when it shouldn't have been." ); 91 } 92 } 93 Watcher testWatch = new Watcher(); 94 95 mEntity.componentRemoved.connect( &testWatch.failIfCalled ); 96 97 EntityState state1 = new EntityState(); 98 MockComponent component1 = new MockComponent(); 99 state1.add( typeid(MockComponent) ).withInstance!MockComponent( component1 ); 100 mFsm.addState( "test1", state1 ); 101 mFsm.changeState( "test1" ); 102 103 EntityState state2 = new EntityState(); 104 MockComponent2 component2 = new MockComponent2(); 105 state2.add( typeid(MockComponent) ).withInstance!MockComponent( component1 ); 106 state2.add( typeid(MockComponent2) ).withInstance!MockComponent2( component2 ); 107 mFsm.addState( "test2", state2 ); 108 mFsm.changeState( "test2" ); 109 110 assert( mEntity.get!MockComponent() is component1 ); 111 } 112 113 public void enterSecondStateRemovesDifferentComponentsOfSameType() 114 { 115 EntityState state1 = new EntityState(); 116 MockComponent component1 = new MockComponent(); 117 state1.add( typeid(MockComponent) ).withInstance!MockComponent( component1 ); 118 mFsm.addState( "test1", state1 ); 119 mFsm.changeState( "test1" ); 120 121 EntityState state2 = new EntityState(); 122 MockComponent component3 = new MockComponent(); 123 MockComponent2 component2 = new MockComponent2(); 124 state2.add( typeid(MockComponent) ).withInstance!MockComponent( component3 ); 125 state2.add( typeid(MockComponent2) ).withInstance!MockComponent2( component2 ); 126 mFsm.addState( "test2", state2 ); 127 mFsm.changeState( "test2" ); 128 129 assert( mEntity.get!MockComponent() is component3 ); 130 } 131 } 132 133 class MockComponent: Component 134 { 135 public int value; 136 } 137 138 class MockComponent2: Component 139 { 140 public string value; 141 }