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_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_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)