1 /**
2  * Unit tests for engine.d
3  *
4  */
5 module engine_test;
6 
7 import ashd.core.component : Component;
8 import ashd.core.entity    : Entity;
9 import ashd.core.engine    : Engine;
10 import ashd.core.ifamily   : IFamily;
11 import ashd.core.node      : Node;
12 import ashd.core.nodelist  : NodeList;
13 
14 
15 
16 int main()
17 {
18     (new EngineTests).entitiesGetterReturnsAllTheEntities();
19     (new EngineTests).getEntityByNameReturnsCorrectEntity();
20     (new EngineTests).getEntityByNameReturnsNullIfNoEntity();
21     (new EngineTests).addEntityChecksWithAllFamilies();
22     (new EngineTests).removeEntityChecksWithAllFamilies();
23     (new EngineTests).removeEntityChecksWithAllFamilies2();
24     (new EngineTests).removeAllEntitiesChecksWithAllFamilies();
25 
26     (new EngineTests).componentAddedChecksWithAllFamilies();
27     (new EngineTests).componentRemovedChecksWithAllFamilies();
28     (new EngineTests).getNodeListCreatesFamily();
29 
30     (new EngineTests).getNodeListChecksAllEntities();
31     (new EngineTests).entityCanBeObtainedByName();
32     (new EngineTests).getEntityByInvalidNameReturnsNull();
33     (new EngineTests).entityCanBeObtainedByNameAfterRenaming();
34     (new EngineTests).entityCannotBeObtainedByOldNameAfterRenaming();
35 
36     return 0;
37 }
38 
39 class EngineTests
40 {
41     Engine mEngine;
42 
43     IFamily createNewMockFamily() { return new MockFamily; }
44 
45     public this()
46     {
47         mEngine = new Engine();
48     }
49 
50     public void entitiesGetterReturnsAllTheEntities()
51     {
52         Entity entity1 = new Entity();
53         mEngine.addEntity( entity1 );
54         Entity entity2 = new Entity();
55         mEngine.addEntity( entity2 );
56         assert( mEngine.entities.length == 2 );
57         assert( mEngine.entities == [ entity1, entity2 ] );
58     }
59 
60     public void getEntityByNameReturnsCorrectEntity()
61     {
62         Entity entity1 = new Entity();
63         entity1.name = "otherEntity";
64         mEngine.addEntity( entity1 );
65         Entity entity2 = new Entity();
66         entity2.name = "myEntity";
67         mEngine.addEntity( entity2 );
68 
69         assert( mEngine.getEntityByName( "myEntity" ) is entity2 );
70     }
71 
72     public void getEntityByNameReturnsNullIfNoEntity()
73     {
74         Entity entity1 = new Entity();
75         entity1.name = "otherEntity";
76         mEngine.addEntity( entity1 );
77         Entity entity2 = new Entity();
78         entity2.name = "myEntity";
79         mEngine.addEntity( entity2 );
80 
81         assert( mEngine.getEntityByName( "wrongName" ) is null );
82     }
83 
84     public void addEntityChecksWithAllFamilies()
85     {
86         MockFamily.reset();
87         auto family1=new MockFamily;
88         mEngine.registerFamily!MockNode( family1 );
89         auto family2=new MockFamily;
90         mEngine.registerFamily!MockNode2( family2 );
91         mEngine.getNodeList!(MockNode)( );
92         //mEngine.getNodeList!(MockNode)( &createNewMockFamily );
93         //mEngine.getNodeList!(MockNode2)( &createNewMockFamily );
94         mEngine.getNodeList!(MockNode2)( );
95         Entity entity = new Entity();
96         mEngine.addEntity( entity );
97 
98         assert( MockFamily.mInstances[0].mNewEntityCalls == 1);
99         assert( MockFamily.mInstances[1].mNewEntityCalls == 1 );
100     }
101 
102     public void removeEntityChecksWithAllFamilies()
103     {
104         MockFamily.reset();
105         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
106         mEngine.getNodeList!(MockNode2)( &createNewMockFamily );
107 
108         Entity entity = new Entity();
109         mEngine.addEntity( entity );
110 
111         mEngine.removeEntity( entity );
112 
113         assert( MockFamily.mInstances[0].mRemoveEntityCalls == 1 );
114         assert( MockFamily.mInstances[1].mRemoveEntityCalls == 1 );
115     }
116 
117     public void removeEntityChecksWithAllFamilies2()
118     {
119         MockFamily.reset();
120         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
121         mEngine.getNodeList!(MockNode2)( &createNewMockFamily );
122 
123         Entity entity = new Entity();
124         Entity entity2 = new Entity();
125         mEngine.addEntity( entity );
126         mEngine.addEntity( entity2 );
127 
128         mEngine.removeEntity( entity );
129         mEngine.removeEntity( entity2 );
130 
131         assert( MockFamily.mInstances[0].mRemoveEntityCalls == 2 );
132         assert( MockFamily.mInstances[1].mRemoveEntityCalls == 2 );
133     }
134 
135 
136     public void removeAllEntitiesChecksWithAllFamilies()
137     {
138         MockFamily.reset();
139         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
140         mEngine.getNodeList!(MockNode2)( &createNewMockFamily );
141 
142         Entity entity  = new Entity();
143         Entity entity2 = new Entity();
144 
145         mEngine.addEntity( entity );
146         mEngine.addEntity( entity2 );
147 
148         mEngine.removeAllEntities();
149 
150         assert( MockFamily.mInstances[0].mRemoveEntityCalls == 2 );
151         assert( MockFamily.mInstances[1].mRemoveEntityCalls == 2 );
152     }
153 
154     public void componentAddedChecksWithAllFamilies()
155     {
156         MockFamily.reset();
157         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
158         mEngine.getNodeList!(MockNode2)( &createNewMockFamily );
159 
160         Entity entity  = new Entity();
161         mEngine.addEntity( entity );
162         entity.add!Point( new Point() );
163 
164         assert( MockFamily.mInstances[0].mComponentAddedCalls == 1 );
165         assert( MockFamily.mInstances[1].mComponentAddedCalls == 1 );
166     }
167 
168     public void componentRemovedChecksWithAllFamilies()
169     {
170         MockFamily.reset();
171         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
172         mEngine.getNodeList!(MockNode2)( &createNewMockFamily );
173 
174         Entity entity  = new Entity();
175         mEngine.addEntity( entity );
176         entity.add!Point( new Point() );
177         entity.remove!Point();
178 
179         assert( MockFamily.mInstances[0].mComponentRemovedCalls == 1 );
180         assert( MockFamily.mInstances[1].mComponentRemovedCalls == 1 );
181     }
182 
183     public void getNodeListCreatesFamily()
184     {
185         MockFamily.reset();
186         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
187 
188         assert( MockFamily.mInstances.length == 1 );
189     }
190 
191     public void getNodeListChecksAllEntities()
192     {
193         MockFamily.reset();
194         mEngine.addEntity( new Entity() );
195         mEngine.addEntity( new Entity() );
196         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
197         assert( MockFamily.mInstances[0].mNewEntityCalls == 2 );
198     }
199 
200     public void releaseNodeListCallsCleanUp()
201     {
202         MockFamily.reset();
203         mEngine.getNodeList!(MockNode)( &createNewMockFamily );
204         mEngine.releaseNodeList!MockNode( );
205         assert( MockFamily.mInstances[0].mCleanUpCalls == 1 );
206     }
207 
208     public void entityCanBeObtainedByName()
209     {
210         MockFamily.reset();
211         Entity entity  = new Entity( "anything" );
212         mEngine.addEntity( entity );
213 
214         Entity other = mEngine.getEntityByName( "anything" );
215         assert( other is entity );
216     }
217 
218     public void getEntityByInvalidNameReturnsNull()
219     {
220         MockFamily.reset();
221         Entity entity = mEngine.getEntityByName( "anything" );
222         assert( entity is null );
223     }
224 
225     public void entityCanBeObtainedByNameAfterRenaming()
226     {
227         MockFamily.reset();
228         Entity entity  = new Entity( "anything" );
229         mEngine.addEntity( entity );
230         entity.name = "otherName";
231         Entity other = mEngine.getEntityByName( "otherName" );
232         assert( other is entity );
233     }
234 
235     public void entityCannotBeObtainedByOldNameAfterRenaming()
236     {
237         MockFamily.reset();
238         Entity entity  = new Entity( "anything" );
239         mEngine.addEntity( entity );
240         entity.name = "otherName";
241         Entity other = mEngine.getEntityByName( "anything" );
242         assert( other is null );
243     }
244 
245 }
246 
247 class Point: Component
248 {
249     int x; int y;
250 }
251 
252 class MockNode: Node
253 {
254     public Point point;
255 }
256 
257 class MockNode2: Node
258 {
259     public int foo;
260 }
261 
262 
263 class MockFamily: IFamily
264 {
265     private
266     {
267         static MockFamily[] mInstances;
268         int mNewEntityCalls;
269         int mRemoveEntityCalls;
270         int mComponentAddedCalls;
271         int mComponentRemovedCalls;
272         int mCleanUpCalls;
273     }
274 
275     public this()
276     {
277         mInstances ~= this;
278     }
279 
280     static public void reset() { mInstances.length = 0; }
281 
282     public NodeList nodeList()
283     {
284         return null;
285     }
286 
287     public void newEntity( Entity entity_a )
288     {
289         mNewEntityCalls++;
290     }
291 
292     public void removeEntity( Entity entity_a )
293     {
294         mRemoveEntityCalls++;
295     }
296 
297     public void componentAddedToEntity( Entity entity_a, ClassInfo class_a )
298     {
299         mComponentAddedCalls++;
300     }
301 
302     public void componentRemovedFromEntity( Entity entity_a, ClassInfo class_a )
303     {
304         mComponentRemovedCalls++;
305     }
306 
307     public void cleanUp()
308     {
309         mCleanUpCalls++;
310     }
311 
312     public void resetNode( Node node_a ) {}
313 }
314 
315 
316