def test_resolving_reference(world): to_entity = world.create_entity() from_entity = world.create_entity() from_entity.add_component(Reference(uid=to_entity._uid)) world.flush_component_updates() reference = world.get_entity(from_entity.get_component(Reference).uid) assert reference is to_entity
def test_resolving_dangling_reference(world): to_entity = world.create_entity() from_entity = world.create_entity() from_entity.add_component(Reference(uid=to_entity._uid)) to_entity.destroy() world.flush_component_updates() with pytest.raises(NoSuchUID): world.get_entity(from_entity.get_component(Reference).uid)
def test_compound_filter_2(world, entity): sub_filter_1 = or_filter([ComponentA]) f = or_filter([sub_filter_1]) assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() assert f(entity)
def test_remove_aspect_from_entity(world): aspect = Aspect([Component_A]) entity = world.create_entity(*aspect()) world.flush_component_updates() assert aspect.in_entity(entity) components = aspect.remove(entity) world.flush_component_updates() assert not aspect.in_entity(entity) assert Component_A not in entity assert len(components) == 1 assert isinstance(components[0], Component_A)
def test_destroy_component_by_removing_component(world, entity, component, system, enabler): world.add_system(system, 0) entity.add_component(component) entity.add_component(enabler) world.flush_component_updates() assert system.destroy_called == 0 assert system.destroy_done == 0 assert component.inited entity.remove_component(type(component)) world.flush_component_updates() assert system.destroy_called == 2 # Both filters fail a once assert system.destroy_done == 1 # ...but only one destroyed the component assert not component.inited
def test_destroy_component_by_removing_enabler(world, entity, component, system, enabler): world.add_system(system, 0) entity.add_component(component) entity.add_component(enabler) world.flush_component_updates() assert system.destroy_called == 0 assert system.destroy_done == 0 assert component.inited entity.remove_component(type(enabler)) world.flush_component_updates() assert system.destroy_called == 1 assert system.destroy_done == 1 assert not component.inited
def test_basic_component_handling(world, entity): component = Counter(count=0, inited=False) assert entity.get_components() == set() assert not entity.has_component(Counter) entity.add_component(component) # flush should not be called directly. It's normally called by # world.update(). For testing purposes, however... world.flush_component_updates() assert entity.has_component(Counter) assert entity.get_component(Counter) is component entity.remove_component(Counter) world.flush_component_updates() assert entity.get_components() == set() assert not entity.has_component(Counter)
def test_init_component(world, entity, component, system, enabler): world.add_system(system, 0) assert system.init_called == 0 assert system.init_done == 0 assert not component.inited entity.add_component(component) world.flush_component_updates() assert system.init_called == 1 assert system.init_done == 0 assert not component.inited entity.add_component(enabler) world.flush_component_updates() assert system.init_called == 2 assert system.init_done == 1 assert component.inited
def test_or_filter(world, entity): f = or_filter([ComponentA, ComponentB]) assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() assert f(entity) entity.add_component(ComponentB()) world.flush_component_updates() assert f(entity) entity.remove_component(ComponentA) world.flush_component_updates() assert f(entity) entity.remove_component(ComponentB) world.flush_component_updates() assert not f(entity)
def test_compound_filter_3(world, entity): sub_filter_1 = and_filter([ComponentA]) sub_filter_2 = or_filter([ComponentB, ComponentC]) f = or_filter([sub_filter_1, sub_filter_2]) assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() assert f(entity) entity.remove_component(ComponentA) entity.add_component(ComponentB()) world.flush_component_updates() assert f(entity) entity.remove_component(ComponentB) entity.add_component(ComponentC()) world.flush_component_updates() assert f(entity) entity.remove_component(ComponentC) world.flush_component_updates()
def test_get(world, entity, component): entity.add_component(component) world.flush_component_updates() assert entity[Counter] is component
def test_del(world, entity, component): entity.add_component(component) world.flush_component_updates() del entity[Counter] world.flush_component_updates() assert not entity.has_component(Counter)
def test_contains(world, entity, component): entity.add_component(component) world.flush_component_updates() assert Counter in entity
def test_compound_filter_4(world, entity): sub_filter_1 = and_filter([ComponentA]) sub_filter_2 = or_filter([ComponentB, ComponentC]) f = and_filter([sub_filter_1, sub_filter_2]) # A and (B or C) assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() # A assert not f(entity) entity.remove_component(ComponentA) world.flush_component_updates() entity.add_component(ComponentB()) world.flush_component_updates() # B assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() # A, B assert f(entity) entity.remove_component(ComponentB) entity.add_component(ComponentC()) world.flush_component_updates() # A, C assert f(entity) entity.remove_component(ComponentA) world.flush_component_updates() # C assert not f(entity)