1 /** 2 * Unit tests for Entity.d 3 * 4 */ 5 module entity_test; 6 7 import ashd.core.component: Component; 8 import ashd.core.entity : Entity; 9 10 11 int main() 12 { 13 (new EntityTests).addReturnsReferenceToEntity(); 14 (new EntityTests).canStoreAndRetrieveComponent(); 15 (new EntityTests).canStoreAndRetrieveMultipleComponents(); 16 (new EntityTests).canReplaceComponent(); 17 (new EntityTests).canStoreBaseAndExtendedComponents(); 18 (new EntityTests).canStoreExtendedComponentAsBaseType(); 19 (new EntityTests).getReturnNullIfNoComponent(); 20 (new EntityTests).willRetrieveAllComponents(); 21 (new EntityTests).hasComponentIsFalseIfComponentTypeNotPresent(); 22 (new EntityTests).hasComponentIsTrueIfComponentTypeIsPresent(); 23 (new EntityTests).canRemoveComponent(); 24 (new EntityTests).storingComponentTriggersAddedSignal(); 25 (new EntityTests).removingComponentTriggersRemovedSignal(); 26 (new EntityTests).componentAddedSignalContainsCorrectParameters(); 27 (new EntityTests).componentRemovedSignalContainsCorrectParameters(); 28 (new EntityTests).testEntityHasNameByDefault(); 29 (new EntityTests).testEntityNameStoredAndReturned(); 30 (new EntityTests).testEntityNameCanBeChanged(); 31 (new EntityTests).testChangingEntityNameDispatchesSignal(); 32 33 return 0; 34 } 35 36 37 class EntityTests 38 { 39 void addReturnsReferenceToEntity() 40 { 41 Entity testEntity = new Entity(); 42 MockComponent component = new MockComponent; 43 auto e = testEntity.add( component ); 44 assert( e is testEntity, "Test FAIL: addReturnsReferenceToEntity()" ); 45 } 46 47 void canStoreAndRetrieveComponent() 48 { 49 Entity testEntity = new Entity(); 50 MockComponent component = new MockComponent(); 51 auto e = testEntity.add( component ); 52 assert( testEntity.get!MockComponent() is component, "Test FAIL: canStoreAndRetrieveComponent()" ); 53 } 54 55 void canStoreAndRetrieveMultipleComponents() 56 { 57 Entity testEntity = new Entity(); 58 MockComponent component1 = new MockComponent(); 59 MockComponent2 component2 = new MockComponent2(); 60 testEntity.add( component1 ); 61 testEntity.add( component2 ); 62 assert( testEntity.get!MockComponent() is component1 ); 63 assert( testEntity.get!MockComponent2() is component2 ); 64 } 65 66 void canReplaceComponent() 67 { 68 Entity testEntity = new Entity(); 69 MockComponent component1 = new MockComponent(); 70 MockComponent component2 = new MockComponent(); 71 testEntity.add( component1 ); 72 testEntity.add( component2 ); 73 assert( testEntity.get!MockComponent() is component2 ); 74 } 75 76 void canStoreBaseAndExtendedComponents() 77 { 78 Entity testEntity = new Entity(); 79 MockComponent component1 = new MockComponent(); 80 testEntity.add( component1 ); 81 MockComponentExtended component2 = new MockComponentExtended(); 82 testEntity.add( component2 ); 83 84 assert( testEntity.get!MockComponent() is component1 ); 85 assert( testEntity.get!MockComponentExtended() is component2 ); 86 } 87 88 89 void canStoreExtendedComponentAsBaseType() 90 { 91 Entity testEntity = new Entity(); 92 MockComponentExtended component = new MockComponentExtended; 93 testEntity.add!MockComponent( component ); 94 assert( testEntity.get!MockComponent() is component ); 95 } 96 97 void getReturnNullIfNoComponent() 98 { 99 Entity testEntity = new Entity(); 100 assert( testEntity.get!MockComponent() is null ); 101 } 102 103 void willRetrieveAllComponents() 104 { 105 Entity testEntity = new Entity(); 106 MockComponent component1 = new MockComponent(); 107 MockComponent2 component2 = new MockComponent2(); 108 testEntity.add( component1 ); 109 testEntity.add( component2 ); 110 111 auto all = testEntity.getAll(); 112 assert( all.length == 2 ); 113 bool[Object] tmp; 114 foreach ( cpt; all ) 115 tmp[cpt] = true; 116 assert( component1 in tmp ); 117 assert( component2 in tmp ); 118 } 119 120 void hasComponentIsFalseIfComponentTypeNotPresent() 121 { 122 Entity testEntity = new Entity(); 123 testEntity.add( new MockComponent2() ); 124 assert( testEntity.has!MockComponent() == false ); 125 assert( testEntity.has(MockComponent.classinfo) == false ); 126 } 127 128 void hasComponentIsTrueIfComponentTypeIsPresent() 129 { 130 Entity testEntity = new Entity(); 131 testEntity.add( new MockComponent() ); 132 assert( testEntity.has!MockComponent() == true ); 133 assert( testEntity.has(MockComponent.classinfo) == true ); 134 } 135 136 void canRemoveComponent() 137 { 138 Entity testEntity = new Entity(); 139 MockComponent component1 = new MockComponent(); 140 testEntity.add( component1 ); 141 testEntity.remove!MockComponent(); 142 assert( testEntity.has!MockComponent() == false ); 143 } 144 145 void storingComponentTriggersAddedSignal() 146 { 147 bool bCalled = false; 148 class Watcher { void watch( Entity e, ClassInfo c ) { bCalled = true; } } 149 Entity testEntity = new Entity(); 150 MockComponent component1 = new MockComponent(); 151 152 Watcher testWatch = new Watcher(); 153 testEntity.componentAdded.connect( &testWatch.watch ); 154 testEntity.add!MockComponent( component1 ); 155 assert( bCalled == true ); 156 } 157 158 void removingComponentTriggersRemovedSignal() 159 { 160 Entity testEntity = new Entity(); 161 MockComponent component1 = new MockComponent(); 162 testEntity.add( component1 ); 163 164 bool bCalled = false; 165 class Watcher { void watch( Entity e, ClassInfo c ) { bCalled = true; } } 166 Watcher testWatch = new Watcher(); 167 testEntity.componentRemoved.connect( &testWatch.watch ); 168 testEntity.remove!MockComponent(); 169 assert( bCalled == true ); 170 } 171 172 void componentAddedSignalContainsCorrectParameters() 173 { 174 Entity testEntity = new Entity(); 175 MockComponent component1 = new MockComponent(); 176 177 class Watcher 178 { 179 void watch( Entity e, ClassInfo c ) 180 { 181 assert( e is testEntity ); 182 assert( c == MockComponent.classinfo ); 183 } 184 } 185 Watcher testWatch = new Watcher(); 186 187 testEntity.componentAdded.connect( &testWatch.watch ); 188 testEntity.add( component1 ); 189 } 190 191 void componentRemovedSignalContainsCorrectParameters() 192 { 193 Entity testEntity = new Entity(); 194 MockComponent component1 = new MockComponent(); 195 196 class Watcher 197 { 198 void watch( Entity e, ClassInfo c ) 199 { 200 assert( e is testEntity ); 201 assert( c == MockComponent.classinfo ); 202 } 203 } 204 Watcher testWatch = new Watcher(); 205 206 testEntity.add( component1 ); 207 testEntity.componentRemoved.connect( &testWatch.watch ); 208 testEntity.remove!MockComponent(); 209 } 210 211 void testEntityHasNameByDefault() 212 { 213 Entity testEntity = new Entity(); 214 assert( testEntity.name.length > 0 ); 215 } 216 217 void testEntityNameStoredAndReturned() 218 { 219 string name = "anything"; 220 Entity testEntity = new Entity( name ); 221 assert( testEntity.name == name ); 222 } 223 224 void testEntityNameCanBeChanged() 225 { 226 Entity testEntity = new Entity( "anything" ); 227 testEntity.name = "otherThing"; 228 assert( testEntity.name == "otherThing" ); 229 } 230 231 void testChangingEntityNameDispatchesSignal() 232 { 233 Entity testEntity = new Entity( "anything" ); 234 class Watcher 235 { 236 void watch( Entity e, string oldName ) 237 { 238 assert( e is testEntity ); 239 assert( e.name == "otherThing" ); 240 assert( oldName == "anything" ); 241 } 242 } 243 Watcher testWatch = new Watcher(); 244 245 testEntity.nameChanged.connect( &testWatch.watch ); 246 } 247 248 } 249 250 251 class MockComponent: Component 252 { 253 int value1; 254 } 255 256 class MockComponent2: Component 257 { 258 int value2; 259 } 260 261 class MockComponentExtended: MockComponent 262 { 263 int other; 264 }