def test_entity_id(self): from grease import Entity world = TestWorld() entity1 = Entity(world) entity2 = Entity(world) self.assertTrue(entity1.entity_id > 0) self.assertTrue(entity2.entity_id > 0) self.assertNotEqual(entity1.entity_id, entity2.entity_id)
def test_create_entities_in_world(self): from grease import World, Entity world = World() self.assertFalse(world.entities) e1 = Entity(world) e2 = Entity(world) self.assertEqual(len(world.entities), 2) self.assertTrue(e1 in world.entities) self.assertTrue(e1.world is world) self.assertTrue(e2 in world.entities) self.assertTrue(e2.world is world) self.assertNotEqual(e1, e2)
def test_entity_extent_component_access(self): from grease import World, Entity from grease.entity import ComponentEntitySet world = World() comp = world.components.test = TestComponent() e1 = Entity(world) e2 = Entity(world) comp.add(e1) comp.add(e2) extent = world[Entity] comp_set = extent.test self.assertEqual(comp_set, set([e1, e2])) self.assertRaises(AttributeError, getattr, extent, "hummina")
def test_getattr(self): from grease.entity import EntityComponentAccessor from grease import Entity world = TestWorld() entity = Entity(world) component = {entity: TestData(foo=5)} accessor = EntityComponentAccessor(component, entity) self.assertEqual(accessor.foo, 5) self.assertRaises(AttributeError, getattr, accessor, 'bar') entity2 = Entity(world) accessor = EntityComponentAccessor(component, entity2) self.assertRaises(AttributeError, getattr, accessor, 'foo') self.assertRaises(AttributeError, getattr, accessor, 'bar')
def test_eq(self): from grease import Entity world = TestWorld() e1 = Entity(world) e2 = Entity(world) self.assertNotEqual(e1, e2) e2.entity_id = e1.entity_id self.assertEqual(e1, e2) otherworld = TestWorld() e3 = Entity(otherworld) self.assertNotEqual(e1, e3) self.assertNotEqual(e2, e3) e3.entity_id = e1.entity_id self.assertNotEqual(e1, e3) self.assertNotEqual(e2, e3)
def test_repr(self): from grease import Entity entity = Entity(TestWorld()) self.assertTrue( repr(entity).startswith('<Entity id: %s of TestWorld' % entity.entity_id), ('<Entity id: %s of TestWorld' % entity.entity_id, repr(entity)))
def test_accessor_getattr_for_nonexistant_component(self): from grease import Entity comp = TestComponent() world = TestWorld(test=comp) entity = Entity(world) self.assertTrue(entity not in comp) self.assertRaises(AttributeError, getattr, entity, 'foo')
def test_worlds_disjoint(self): from grease import World, Entity world1 = World() world2 = World() self.assertTrue(world1 is not world2) e1 = Entity(world1) e2 = Entity(world2) self.assertEqual(len(world1.entities), 1) self.assertEqual(len(world2.entities), 1) self.assertTrue(e1.world is world1) self.assertTrue(e2.world is world2) self.assertTrue(e1 in world1.entities) self.assertFalse(e2 in world1.entities) self.assertFalse(e1 in world2.entities) self.assertTrue(e2 in world2.entities) self.assertNotEqual(e1, e2)
def test_delete_exists(self): from grease import Entity world = TestWorld() self.assertEqual(world.entities, set()) entity1 = Entity(world) entity2 = Entity(world) self.assertEqual(world.entities, set([entity1, entity2])) self.assertTrue(entity1.exists) self.assertTrue(entity2.exists) entity1.delete() self.assertEqual(world.entities, set([entity2])) self.assertFalse(entity1.exists) self.assertTrue(entity2.exists) entity2.delete() self.assertEqual(world.entities, set()) self.assertFalse(entity1.exists) self.assertFalse(entity2.exists)
def test_accessor_getattr_for_member_entity(self): from grease import Entity comp = TestComponent() world = TestWorld(test=comp) entity = Entity(world) comp.set(entity) self.assertTrue(entity in comp) self.assertEqual(entity.test.attr, 'deadbeef')
def test_accessor_getattr_for_non_member_entity(self): from grease import Entity comp = TestComponent() world = TestWorld(test=comp) entity = Entity(world) accessor = entity.test self.assertFalse(entity in comp) self.assertRaises(AttributeError, getattr, accessor, 'attr')
def test_delattr(self): from grease import Entity comp = TestComponent() world = TestWorld(test=comp) entity = Entity(world) comp.set(entity) self.assertTrue(entity in comp) del entity.test self.assertFalse(entity in comp)
def test_accessor_setattr_adds_non_member_entity(self): from grease import Entity comp = TestComponent() world = TestWorld(test=comp) entity = Entity(world) self.assertFalse(entity in comp) entity.test.attr = 'foobar' self.assertEqual(entity.test.attr, 'foobar') self.assertTrue(entity in comp)
def test_delattr(self): from grease import Entity comp = TestComponent() world = TestWorld(test=comp) entity = Entity(world) comp.set(entity) self.failUnless(entity in comp) del entity.test self.failIf(entity in comp)
def test_truthiness(self): from grease.entity import EntityComponentAccessor from grease import Entity world = TestWorld() entity = Entity(world) component = TestComponent() accessor = EntityComponentAccessor(component, entity) self.assertFalse(accessor) component[entity] = 456 self.assertTrue(accessor)
def test_setattr_member_entity(self): from grease.entity import EntityComponentAccessor from grease import Entity world = TestWorld() entity = Entity(world) data = TestData(foo=5) accessor = EntityComponentAccessor({entity: data}, entity) self.assertEqual(data.foo, 5) accessor.foo = 66 self.assertEqual(data.foo, 66) accessor.bar = '!!' self.assertEqual(data.bar, '!!')
def test_setattr_nonmember_entity(self): from grease.entity import EntityComponentAccessor from grease import Entity world = TestWorld() entity = Entity(world) component = TestComponent() accessor = EntityComponentAccessor(component, entity) self.assertRaises(AttributeError, getattr, entity, 'baz') self.assertTrue(entity not in component) accessor.baz = 1000 self.assertTrue(entity in component) self.assertEqual(accessor.baz, 1000) self.assertEqual(component[entity].baz, 1000)
def test_step_components(self): from grease import World, Entity world = World() comp1 = world.components.one = TestComponent() comp2 = world.components.two = TestComponent() entity = Entity(world) self.assertTrue(comp1.runtime == comp2.runtime == 0, comp1.runtime) world.step(0.05) self.assertEqual(comp1.runtime, 0.05) self.assertEqual(comp2.runtime, 0.05) world.step(0.06) self.assertEqual(comp1.runtime, 0.11) self.assertEqual(comp2.runtime, 0.11)
def test_remove_entity(self): from grease import World, Entity world = World() comp1 = world.components.one = TestComponent() comp2 = world.components.two = TestComponent() comp3 = world.components.three = TestComponent() entity = Entity(world) comp1.add(entity) comp2.add(entity) self.assertTrue(entity in world.entities) self.assertTrue(entity in comp1) self.assertTrue(entity in comp2) self.assertFalse(entity in comp3) world.entities.remove(entity) self.assertFalse(entity in world.entities) self.assertFalse(entity in comp1) self.assertFalse(entity in comp2) self.assertFalse(entity in comp3) self.assertRaises(KeyError, world.entities.remove, entity)
def test_join_components(self): from grease import World, Entity world = World() comp1 = world.components.foo = TestComponent() comp2 = world.components.bar = TestComponent() comp3 = world.components.baz = TestComponent() entity = Entity(world) for i in range(20): entity = object() comp1.add(entity, i) if i < 5: comp2.add(entity, i * 10) if i < 3: comp3.add(entity, i * 100) self.assertEqual(sorted(world.components.join('baz', 'bar', 'foo')), [(0, 0, 0), (100, 10, 1), (200, 20, 2)]) self.assertEqual(sorted(world.components.join('foo', 'bar')), [(0, 0), (1, 10), (2, 20), (3, 30), (4, 40)]) self.assertEqual(sorted(world.components.join('baz')), [(0, ), (100, ), (200, )])
def test_discard_entity(self): from grease import World, Entity world = World() comp1 = world.components.one = TestComponent() comp2 = world.components.two = TestComponent() comp3 = world.components.three = TestComponent() entity = Entity(world) comp1.add(entity) comp2.add(entity) self.assertTrue(entity in world.entities) self.assertTrue(entity in comp1) self.assertTrue(entity in comp2) self.assertFalse(entity in comp3) world.entities.discard(entity) self.assertFalse(entity in world.entities) self.assertFalse(entity in comp1) self.assertFalse(entity in comp2) self.assertFalse(entity in comp3) world.entities.discard(entity) self.assertFalse(entity in world.entities) self.assertFalse(entity in comp1) self.assertFalse(entity in comp2) self.assertFalse(entity in comp3)