1 /**
2  * Unit tests from componentMatchingFamily.d
3  *
4  */
5 module componentMatchingFamily_test;
6 
7 import ashd.core.componentMatchingFamily: ComponentMatchingFamily;
8 import ashd.core.component              : Component;
9 import ashd.core.entity                 : Entity;
10 import ashd.core.engine                 : Engine;
11 import ashd.core.ifamily                : IFamily;
12 import ashd.core.node                   : Node;
13 import ashd.core.nodelist               : NodeList;
14 
15 
16 int main()
17 {
18     (new ComponentMatchingFamilyTests).testNodeListIsInitiallyEmpty();
19     (new ComponentMatchingFamilyTests).testMatchingEntityIsAddedWhenAccessNodeListFirst();
20     (new ComponentMatchingFamilyTests).testMatchingEntityIsAddedWhenAccessNodeListSecond();
21     (new ComponentMatchingFamilyTests).testNodeContainsEntityProperties();
22     (new ComponentMatchingFamilyTests).testMatchingEntityIsAddedWhenComponentAdded();
23     (new ComponentMatchingFamilyTests).testNonMatchingEntityIsNotAdded();
24     (new ComponentMatchingFamilyTests).testNonMatchingEntityIsNotAddedWhenComponentAdded();
25     (new ComponentMatchingFamilyTests).testEntityIsRemovedWhenAccessNodeListFirst();
26     (new ComponentMatchingFamilyTests).testEntityIsRemovedWhenAccessNodeListSecond();
27     (new ComponentMatchingFamilyTests).testEntityIsRemovedWhenComponentRemoved();
28     (new ComponentMatchingFamilyTests).nodeListContainsOnlyMatchingEntities();
29     (new ComponentMatchingFamilyTests).nodeListContainsAllMatchingEntities();
30     (new ComponentMatchingFamilyTests).cleanUpEmptiesNodeList();
31     (new ComponentMatchingFamilyTests).cleanUpSetsNextNodeToNull();
32 
33     return 0;
34 }
35 
36 
37 public class ComponentMatchingFamilyTests
38 {
39     ComponentMatchingFamily!MockNode mFamily;
40     Engine                           mEngine;
41 
42 
43     public this()
44     {
45         mEngine = new Engine;
46         mFamily = new ComponentMatchingFamily!MockNode( mEngine );
47     }
48 
49     public void testNodeListIsInitiallyEmpty()
50     {
51         NodeList nodes = mFamily.nodeList();
52         assert( nodes.head is null );
53     }
54 
55 
56     public void testMatchingEntityIsAddedWhenAccessNodeListFirst()
57     {
58         NodeList nodes = mFamily.nodeList();
59         Entity entity = new Entity();
60         entity.add( new Point() );
61         mFamily.newEntity( entity );
62         assert( nodes.head.entity is entity );
63     }
64 
65 
66     public void testMatchingEntityIsAddedWhenAccessNodeListSecond()
67     {
68         Entity entity = new Entity();
69         entity.add( new Point() );
70         mFamily.newEntity( entity );
71         NodeList nodes = mFamily.nodeList;
72         assert( nodes.head.entity is entity );
73     }
74 
75     public void testNodeContainsEntityProperties()
76     {
77         Entity entity = new Entity();
78         Point point = new Point();
79         entity.add( point );
80         mFamily.newEntity( entity );
81         NodeList nodes = mFamily.nodeList;
82         assert( nodes.head.entity.get!Point is point );
83     }
84 
85     public void testMatchingEntityIsAddedWhenComponentAdded()
86     {
87         NodeList nodes = mFamily.nodeList;
88         Entity entity = new Entity();
89         entity.add( new Point() );
90         mFamily.componentAddedToEntity( entity, Point.classinfo );
91         assert( nodes.head.entity is entity );
92     }
93 
94     public void testNonMatchingEntityIsNotAdded()
95     {
96         Entity entity = new Entity();
97         mFamily.newEntity( entity );
98         NodeList nodes = mFamily.nodeList;
99         assert( nodes.head is null );
100     }
101  
102     public void testNonMatchingEntityIsNotAddedWhenComponentAdded()
103     {
104         Entity entity = new Entity();
105         entity.add( new Class2() );
106         mFamily.componentAddedToEntity( entity, Class2.classinfo );
107         NodeList nodes = mFamily.nodeList;
108         assert( nodes.head is null );
109     }
110 
111     public void testEntityIsRemovedWhenAccessNodeListFirst()
112     {
113         Entity entity = new Entity();
114         entity.add( new Point() );
115         mFamily.newEntity( entity );
116         NodeList nodes = mFamily.nodeList;
117         assert( nodes.head.entity is entity );
118         mFamily.removeEntity( entity );
119         assert( nodes.head is null );
120     }
121  
122     public void testEntityIsRemovedWhenAccessNodeListSecond()
123     {
124         Entity entity = new Entity();
125         entity.add( new Point() );
126         mFamily.newEntity( entity );
127         mFamily.removeEntity( entity );
128         NodeList nodes = mFamily.nodeList;
129         assert( nodes.head is null );
130     }
131 
132 
133     public void testEntityIsRemovedWhenComponentRemoved()
134     {
135         Entity entity = new Entity();
136         entity.add( new Point() );
137         mFamily.newEntity( entity );
138         entity.remove!Point();
139         mFamily.componentRemovedFromEntity( entity, Point.classinfo );
140         NodeList nodes = mFamily.nodeList;
141         assert( nodes.head is null );
142     }
143 
144     public void nodeListContainsOnlyMatchingEntities()
145     {
146         Entity[] entities;
147         for ( int i = 0; i < 5; ++i )
148         {
149             Entity entity = new Entity();
150             entity.add( new Point() );
151             entities ~= entity;
152             mFamily.newEntity( entity );
153             mFamily.newEntity( new Entity() );
154         }
155         
156         NodeList nodes = mFamily.nodeList;
157         Node node;
158         int i;
159         for ( node = nodes.head; node; node = node.next )
160         {
161             assert( entities[i++] is node.entity );
162         }
163     }
164 
165 
166     public void nodeListContainsAllMatchingEntities()
167     {
168         Entity[] entities;
169         for ( int i = 0; i < 5; ++i )
170         {
171             Entity entity = new Entity();
172             entity.add( new Point() );
173             entities ~= entity;
174             mFamily.newEntity( entity );
175             mFamily.newEntity( new Entity() );
176         }
177         NodeList nodes = mFamily.nodeList;
178         Node node;
179         int i;
180         for ( node = nodes.head; node; node = node.next )
181         {
182             assert( entities[i++] is node.entity );
183         }
184         assert( i==5 );
185     }
186 
187     public void cleanUpEmptiesNodeList()
188     {
189         Entity entity = new Entity();
190         entity.add( new Point() );
191         mFamily.newEntity( entity );
192         NodeList nodes = mFamily.nodeList;
193         mFamily.cleanUp();
194         assert( nodes.head is null );
195     }
196 
197     public void cleanUpSetsNextNodeToNull()
198     {
199         Entity[] entities;
200         for ( int i = 0; i < 5; ++i )
201         {
202             Entity entity = new Entity();
203             entity.add( new Point() );
204             entities ~= entity;
205             mFamily.newEntity( entity );
206         }
207         
208         NodeList nodes = mFamily.nodeList;
209         Node node = nodes.head.next;
210         mFamily.cleanUp();
211         assert( node.next is null );
212     }
213 
214 }
215 
216 class MockNode: Node
217 {
218     Point point;
219     int dummy;
220 }
221 
222 class Point: Component
223 { 
224     int x; int y; 
225 }
226 
227 class Class2: Component
228 {
229     int dummy;
230 }
231