def test_get(world, entity, null_component):
    entity.add_component(null_component)
    world._flush_component_updates()
    assert entity[NullComponent] is null_component
    assert entity.get(NullComponent) is null_component
    assert entity.get("missing") is None
    assert entity.get("missing", "default_value") is "default_value"
Exemple #2
0
def test_update_world_when_system_was_added_first(world, entity, component,
                                                  system):
    world.add_system(system, 0)
    entity.add_component(component)
    assert component.count == 0

    world.update()
    assert component.count == 1
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
def test_compound_filter_1(world, entity):
    sub_filter_1 = and_filter([ComponentA])
    f = and_filter([sub_filter_1])

    s = set()
    assert not f(s)

    assert not f(entity)

    s = set([ComponentA])
    assert f(s)

    entity.add_component(ComponentA())
    world._flush_component_updates()
    assert f(entity)
Exemple #6
0
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
Exemple #7
0
def test_and_filter_with_entity(world, entity):
    f = and_filter([ComponentA, ComponentB])
    assert not f(entity)

    entity.add_component(ComponentA())
    world._flush_component_updates()
    assert not f(entity)

    entity.add_component(ComponentB())
    world._flush_component_updates()
    assert f(entity)

    entity.remove_component(ComponentA)
    world._flush_component_updates()
    assert not f(entity)
Exemple #8
0
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
Exemple #9
0
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)
Exemple #10
0
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
Exemple #11
0
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)

    # Empty
    s = set()
    assert not f(s)

    assert not f(entity)

    # A
    s = set([ComponentA])
    assert not f(s)

    entity.add_component(ComponentA())
    world._flush_component_updates()
    assert not f(entity)
    entity.remove_component(ComponentA)
    world._flush_component_updates()

    # B
    s = set([ComponentB])
    assert not f(s)

    entity.add_component(ComponentB())
    world._flush_component_updates()
    assert not f(entity)

    # A, B
    s = set([ComponentA, ComponentB])
    assert f(s)

    entity.add_component(ComponentA())
    world._flush_component_updates()
    assert f(entity)

    # A, C
    s = set([ComponentA, ComponentC])
    assert f(s)

    entity.remove_component(ComponentB)
    entity.add_component(ComponentC())
    world._flush_component_updates()
    assert f(entity)

    # C
    s = set([ComponentC])
    assert not f(s)

    entity.remove_component(ComponentA)
    world._flush_component_updates()
    assert not f(entity)
Exemple #12
0
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)
Exemple #13
0
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])

    s = set()
    assert not f(s)

    assert not f(entity)

    s = set([ComponentA])
    assert f(s)

    entity.add_component(ComponentA())
    world._flush_component_updates()
    assert f(entity)

    s = set([ComponentB])
    assert f(s)

    entity.remove_component(ComponentA)
    entity.add_component(ComponentB())
    world._flush_component_updates()
    assert f(entity)

    s = set([ComponentC])
    assert f(s)

    entity.remove_component(ComponentB)
    entity.add_component(ComponentC())
    world._flush_component_updates()
    assert f(entity)
Exemple #14
0
def test_contains(world, entity, null_component):
    entity.add_component(null_component)
    assert NullComponent not in entity

    world._flush_component_updates()
    assert NullComponent in entity
Exemple #15
0
def test_get(world, entity, null_component):
    entity.add_component(null_component)
    world._flush_component_updates()
    assert entity[NullComponent] is null_component
Exemple #16
0
def test_unflushed_reads(entity, component):
    entity.add_component(component)
    assert entity.has_component(Counter)
    assert entity.get_component(Counter) is component
Exemple #17
0
def test_can_not_add_component_type_multiple_times(entity, null_component):
    entity.add_component(null_component)
    with pytest.raises(KeyError):
        entity.add_component(null_component)
Exemple #18
0
def test_contains(world, entity, component):
    entity.add_component(component)
    world.flush_component_updates()
    assert Counter in entity
Exemple #19
0
def test_deferred_removal_of_components(world, entity):
    world.add_system(RemoveFooComponent(), 0)
    entity.add_component(FooComponent())
    world.update()
Exemple #20
0
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)
Exemple #21
0
def test_get(world, entity, component):
    entity.add_component(component)
    world.flush_component_updates()
    assert entity[Counter] is component