1 /**
2  * Unit tests for engineStateMachine.d
3  */
4 module engineStateMachine_test;
5 
6 
7 import ashd.core.engine           : Engine;
8 import ashd.core.iengine          : IEngine;
9 import ashd.core.system           : System;
10 import ashd.fsm.engineState       : EngineState;
11 import ashd.fsm.engineStateMachine: EngineStateMachine;
12 
13 
14 int main()
15 {
16     (new EngineStateMachineTests).enterStateAddsStatesSystems();
17     (new EngineStateMachineTests).enterSecondStateAddsSecondStatesSystems();
18     (new EngineStateMachineTests).enterSecondStateRemovesFirstStatesSystems();
19     (new EngineStateMachineTests).enterSecondStateDoesNotRemoveOverlappingSystems();
20     (new EngineStateMachineTests).enterSecondStateRemovesDifferentSystemsOfSameType();
21 
22     return 0;
23 }
24 
25 
26 public class EngineStateMachineTests
27 {
28     private
29     {
30         Engine mEngine;
31         EngineStateMachine mFsm;
32     }
33 
34     this()
35     {
36         mEngine = new Engine();
37         mFsm = new EngineStateMachine( mEngine );
38     }
39 
40 
41     public void enterStateAddsStatesSystems()
42     {
43         EngineState state = new EngineState();
44         MockSystem system = new MockSystem();
45         state.addInstance( system );
46         mFsm.addState( "test", state );
47         mFsm.changeState( "test" );
48         assert( mEngine.getSystem!MockSystem() is system );
49     }
50 
51     public void enterSecondStateAddsSecondStatesSystems()
52     {
53         EngineState state1 = new EngineState();
54         MockSystem system1 = new MockSystem();
55         state1.addInstance( system1 );
56         mFsm.addState( "test1", state1 );
57         mFsm.changeState( "test1" );
58 
59         EngineState state2 = new EngineState();
60         MockSystem2 system2 = new MockSystem2();
61         state2.addInstance( system2 );
62         mFsm.addState( "test2", state2 );
63         mFsm.changeState( "test2" );
64 
65         assert( mEngine.getSystem!MockSystem2() is system2 );
66     }
67 
68     public void enterSecondStateRemovesFirstStatesSystems()
69     {
70         EngineState state1 = new EngineState();
71         MockSystem system1 = new MockSystem();
72         state1.addInstance( system1 );
73         mFsm.addState( "test1", state1 );
74         mFsm.changeState( "test1" );
75 
76         EngineState state2 = new EngineState();
77         MockSystem2 system2 = new MockSystem2();
78         state2.addInstance( system2 );
79         mFsm.addState( "test2", state2 );
80         mFsm.changeState( "test2" );
81 
82         assert( mEngine.getSystem!MockSystem is null );
83     }
84 
85     public void enterSecondStateDoesNotRemoveOverlappingSystems()
86     {
87         EngineState state1 = new EngineState();
88         MockSystem system1 = new MockSystem();
89         state1.addInstance( system1 );
90         mFsm.addState( "test1", state1 );
91         mFsm.changeState( "test1" );
92 
93         EngineState state2 = new EngineState();
94         MockSystem2 system2 = new MockSystem2();
95         state2.addInstance( system1 );
96         state2.addInstance( system2 );
97         mFsm.addState( "test2", state2 );
98         mFsm.changeState( "test2" );
99 
100         assert( system1.wasRemoved == false );
101         assert( mEngine.getSystem!MockSystem is system1 );
102     }
103 
104     public void enterSecondStateRemovesDifferentSystemsOfSameType()
105     {
106         EngineState state1 = new EngineState();
107         MockSystem system1 = new MockSystem();
108         state1.addInstance( system1 );
109         mFsm.addState( "test1", state1 );
110         mFsm.changeState( "test1" );
111 
112         EngineState state2 = new EngineState();
113         MockSystem system3 = new MockSystem();
114         MockSystem2 system2 = new MockSystem2();
115         state2.addInstance( system3 );
116         state2.addInstance( system2 );
117         mFsm.addState( "test2", state2 );
118         mFsm.changeState( "test2" );
119 
120         assert( mEngine.getSystem!MockSystem is system3 );
121     }
122 }
123 
124 
125 
126 class MockSystem: System
127 {
128     public bool wasRemoved = false;
129 
130     override public void removeFromEngine( IEngine engine_a )
131     {
132         wasRemoved = true;
133     }
134 
135 }
136 
137 class MockSystem2: System
138 {
139     public string value;
140 }