예제 #1
0
def test_creation(world):
    world.add_system(PerceiveRoom(), 0)

    room = world.create_entity(Room(), )
    actor = world.create_entity(RoomPresence(room=room._uid, ), )
    world.update()
    room_cmpt = room.get_component(Room)
    assert len(room_cmpt.presences) == 1
    assert room_cmpt.presences[0] == actor._uid
    assert len(room_cmpt.arrived) == 1
    assert room_cmpt.arrived[0] == actor._uid
    assert len(room_cmpt.continued) == 0
    assert len(room_cmpt.gone) == 0
    presence_cmpt = actor.get_component(RoomPresence)
    assert len(presence_cmpt.presences) == 1
    assert presence_cmpt.presences[0] == actor._uid

    world.update()
    room_cmpt = room.get_component(Room)
    assert len(room_cmpt.presences) == 1
    assert room_cmpt.presences[0] == actor._uid
    assert len(room_cmpt.arrived) == 0
    assert len(room_cmpt.continued) == 1
    assert room_cmpt.continued[0] == actor._uid
    assert len(room_cmpt.gone) == 0
    presence_cmpt = actor.get_component(RoomPresence)
    assert len(presence_cmpt.presences) == 1
    assert presence_cmpt.presences[0] == actor._uid
예제 #2
0
def test_is_in_room(world):
    world.add_system(PerceiveRoom(), 0)

    # A room, and an actor and an item in the roomless void.
    room = world.create_entity(Room(), )
    actor = world.create_entity()
    item = world.create_entity()

    assert not is_in_room(item, actor)
    with pytest.raises(EntityNotInARoom):
        is_in_room(item, actor, throw_exc=True)

    # Now the actor is in the room.
    actor.add_component(RoomPresence(room=room._uid, ), )
    world.update()

    assert not is_in_room(item, actor)
    with pytest.raises(ItemNotInARoom):
        is_in_room(item, actor, throw_exc=True)

    # And now the item is there, too.
    item.add_component(RoomPresence(room=room._uid, ), )
    world.update()

    assert is_in_room(item, actor)
예제 #3
0
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
예제 #4
0
def test_change_room(world):
    world.add_system(ChangeRoom(), 0)
    world.add_system(PerceiveRoom(), 1)
    room = world.create_entity()
    other_room = world.create_entity()

    room.add_component(Room(adjacent=[other_room._uid]), )
    other_room.add_component(Room(adjacent=[room._uid]), )
    actor = world.create_entity(RoomPresence(room=room._uid, ), )
    world.update()
    actor.add_component(ChangeRoomAction(room=other_room._uid, ), )
    world.update()

    room_cmpt = room.get_component(Room)
    assert len(room_cmpt.presences) == 0
    assert len(room_cmpt.arrived) == 0
    assert len(room_cmpt.continued) == 0
    assert len(room_cmpt.gone) == 1
    assert room_cmpt.gone[0] == actor._uid

    other_room_cmpt = other_room.get_component(Room)
    assert len(other_room_cmpt.presences) == 1
    assert other_room_cmpt.presences[0] == actor._uid
    assert len(other_room_cmpt.arrived) == 1
    assert other_room_cmpt.arrived[0] == actor._uid
    assert len(other_room_cmpt.continued) == 0
    assert len(other_room_cmpt.gone) == 0

    presence_cmpt = actor.get_component(RoomPresence)
    assert presence_cmpt.room == other_room._uid
    assert len(presence_cmpt.presences) == 1
    assert presence_cmpt.presences[0] == actor._uid
예제 #5
0
def test_mutual_perception(world):
    world.add_system(ChangeRoom(), 0)
    world.add_system(PerceiveRoom(), 1)

    # Two connected rooms, an actor in each
    room = world.create_entity()
    other_room = world.create_entity()
    room.add_component(Room(adjacent=[other_room._uid], ), )
    other_room.add_component(Room(adjacent=[room._uid], ), )
    actor = world.create_entity(RoomPresence(room=room._uid, ), )
    other_actor = world.create_entity(RoomPresence(room=other_room._uid, ), )

    world.update()

    # They only see themselves
    presence_cmpt = actor.get_component(RoomPresence)
    assert len(presence_cmpt.presences) == 1
    assert presence_cmpt.presences[0] == actor._uid
    other_presence_cmpt = actor.get_component(RoomPresence)
    assert len(other_presence_cmpt.presences) == 1
    assert other_presence_cmpt.presences[0] == actor._uid

    # Now let one actor change rooms
    actor.add_component(ChangeRoomAction(room=other_room._uid, ), )
    world.update()

    # Now they see each other.
    presence_cmpt = actor.get_component(RoomPresence)
    assert len(presence_cmpt.presences) == 2
    assert actor._uid in presence_cmpt.presences
    assert other_actor._uid in presence_cmpt.presences
    other_presence_cmpt = actor.get_component(RoomPresence)
    assert len(other_presence_cmpt.presences) == 2
    assert actor._uid in other_presence_cmpt.presences
    assert other_actor._uid in other_presence_cmpt.presences
예제 #6
0
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))
    world.destroy_entity(to_entity)
    world._flush_component_updates()
    with pytest.raises(NoSuchUID):
        world.get_entity(from_entity.get_component(Reference).uid)
예제 #7
0
def test_remembers_entities(world):
    entities = set()
    assert set(world.entities) == entities
    entities.add(world.create_entity())
    assert set(world.entities) == entities
    entities.add(world.create_entity())
    assert set(world.entities) == entities
    entities.add(world.create_entity())
    assert set(world.entities) == entities
예제 #8
0
def test_can_get_entities_with_component(world):
    a = world.create_entity()
    b = world.create_entity()
    c = world.create_entity()
    d = world.create_entity()
    a.add(SomeComponent())
    b.add(AnotherComponent())
    c.add(SomeComponent())
    c.add(AnotherComponent())
    assert list(world.entities_with(SomeComponent)) == [a, c]
    assert list(world.entities_with(AnotherComponent)) == [b, c]
예제 #9
0
def test_is_in_inventory(world, room):
    item = world.create_entity(
        Takeable(),
    )
    actor = world.create_entity(
        RoomPresence(room=room._uid),
        Inventory(contents=[item._uid]),
    )
    world._flush_component_updates()

    assert is_in_inventory(item, actor)
예제 #10
0
def can_not_drop_untakeable_item_exception(world, room):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(throw_exc=True), 1)

    item =  world.create_entity()
    actor = world.create_entity(
        RoomPresence(room=room._uid),
        Inventory(contents=[item._uid]),
        DropAction(item=item._uid),
    )
    with pytest.raises(NotTakeable):
        world.update()
예제 #11
0
def test_can_not_take_from_the_void_exception(world):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(throw_exc=True), 1)

    item = world.create_entity(
        Takeable(),
    )
    actor = world.create_entity(
        Inventory(contents=[item._uid]),
        DropAction(item=item._uid),
    )
    with pytest.raises(ActorNotInRoom):
        world.update()
예제 #12
0
def test_cant_change_to_non_adjacent_room(world):
    world.add_system(ChangeRoom(throw_exc=True), 0)
    world.add_system(PerceiveRoom(), 1)

    # Two connected rooms, an actor in each
    room = world.create_entity(Room(), )
    other_room = world.create_entity(Room(), )
    actor = world.create_entity(
        RoomPresence(room=room._uid, ),
        ChangeRoomAction(room=other_room._uid, ),
    )

    with pytest.raises(RoomsNotAdjacent):
        world.update()
예제 #13
0
def test_can_not_take_item_from_other_room_exception(world, room, item):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(throw_exc=True), 1)
    world.add_system(PerceiveRoom(), 2, add_duplicates=True)
    other_room = world.create_entity(
        Room(),
    )
    actor = world.create_entity(
        RoomPresence(room=other_room._uid),
        Inventory(),
        TakeAction(item=item._uid),
    )
    with pytest.raises(ItemNotInRoom):
        world.update()
예제 #14
0
def test_cant_change_to_current_room(world):
    # This test and the next are more of an informative nature. Since
    # a room is usually not adjacent to itself, you can't change from
    # it to it.
    world.add_system(ChangeRoom(throw_exc=True), 0)
    world.add_system(PerceiveRoom(), 1)
    room = world.create_entity(Room(), )
    actor = world.create_entity(
        RoomPresence(room=room._uid, ),
        ChangeRoomAction(room=room._uid, ),
    )

    with pytest.raises(RoomsNotAdjacent):
        world.update()
예제 #15
0
def test_can_not_take_from_the_void(world):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(), 1)
    world.add_system(PerceiveRoom(), 2, add_duplicates=True)

    item =  world.create_entity(
        Takeable(),
    )
    actor = world.create_entity(
        Inventory(),
        TakeAction(item=item._uid),
    )
    world.update()

    assert actor.get_component(Inventory).contents == []
    assert not actor.has_component(TakeAction)
예제 #16
0
def can_not_drop_untakeable_item(world, room):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(), 1)
    world.add_system(PerceiveRoom(), 2, add_duplicates=True)

    item =  world.create_entity()
    actor = world.create_entity(
        RoomPresence(room=room._uid),
        Inventory(contents=[item._uid]),
        DropAction(item=item._uid),
    )
    world.update()

    assert actor.get_component(Inventory).contents == [item._uid]
    assert not actor.has_component(DropAction)
    assert not item.has_component(RoomPresence)
예제 #17
0
def test_proxying_system__field_lookup(world):
    token = '123'
    global token_out
    token_out = None

    @Component()
    class TestComponent:
        foo: str = None

    class BareTypeProxy(System):
        entity_filters = {
            'test': Proxy('proxy'),
        }
        proxies = {
            'proxy': ProxyType(TestComponent, 'foo'),
        }

        def update(self, entity_by_filters):
            for entity in entity_by_filters['test']:
                proxy = self.proxies['proxy']
                # test = entity[proxy.component_type]

                token = proxy.field(entity)

                global token_out
                token_out = token

    system = BareTypeProxy()
    world.add_system(system, 0)
    entity = world.create_entity(TestComponent(foo=token))
    world.update()

    assert token == token_out
예제 #18
0
def test_can_add_components_to_entities(world):
    entity = world.create_entity()
    component = Component()
    assert not world.has_component(entity, Component)
    world.add_component(entity, component)
    assert world.has_component(entity, Component)
    assert world.get_component(entity, Component) is component
예제 #19
0
def test_addition_to_system__entity_first(world, null_system):
    entity = world.create_entity(NullComponent())
    world._flush_component_updates()
    world.add_system(null_system, 0)
    assert entity in null_system.entities['null']
    assert len(null_system.entries) == 1
    assert null_system.entries[0] == (['null'], entity)
예제 #20
0
def test_can_remove_components(world):
    entity = world.create_entity()
    component = Component()
    world.add_component(entity, component)
    assert world.has_component(entity, Component)
    world.remove_component(entity, Component)
    assert not world.has_component(entity, Component)
예제 #21
0
def test_can_not_take_item_from_other_room(world, room, item):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(), 1)
    world.add_system(PerceiveRoom(), 2, add_duplicates=True)
    other_room = world.create_entity(
        Room(),
    )
    actor = world.create_entity(
        RoomPresence(room=other_room._uid),
        Inventory(),
        TakeAction(item=item._uid),
    )
    world.update()

    assert actor.get_component(Inventory).contents == []
    assert not actor.has_component(TakeAction)
    assert item.has_component(RoomPresence)
예제 #22
0
def test_is_not_in_inventory(world, room, item):
    actor = world.create_entity(
        RoomPresence(room=room._uid),
        Inventory(),
    )
    world._flush_component_updates()

    assert not is_in_inventory(item, actor)
예제 #23
0
def test_destroyed_entities_are_forgotten(world):
    entity = world.create_entity()
    component = Component()
    entity.add(component)
    world.destroy_entitiy(entity)
    assert entity not in world.entities
    assert entity not in world.entities_with(Component)
    assert entity.get(Component, None) is None
예제 #24
0
def test_basic_clock(world):
    world.add_system(DetermineTimestep(), sort=0)
    dt = 0.01
    clock = SettableClock(dt)
    entity = world.create_entity(Clock(clock=clock))
    assert dt < entity[Clock].max_timestep

    world.update()
    assert entity[Clock].timestep == dt
예제 #25
0
def test_can_not_take_nonexistant_item_exception(world, room):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(throw_exc=True), 1)
    actor = world.create_entity(
        RoomPresence(room=room._uid),
        Inventory(),
        TakeAction(item=UID()),
    )
    with pytest.raises(NoSuchUID):
        world.update()
예제 #26
0
def test_can_not_take_without_an_inventory_exception(world, room, item):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(throw_exc=True), 1)
    world.add_system(PerceiveRoom(), 2, add_duplicates=True)
    actor = world.create_entity(
        RoomPresence(room=room._uid),
        TakeAction(item=item._uid),
    )
    with pytest.raises(ActorHasNoInventory):
        world.update()
예제 #27
0
def test_can_not_drop_item_that_actor_does_not_have_exception(world, room, item):
    world.add_system(PerceiveRoom(), 0)
    world.add_system(TakeOrDrop(throw_exc=True), 1)
    actor = world.create_entity(
        RoomPresence(room=room._uid),
        Inventory(),
        DropAction(item=item._uid),
    )
    with pytest.raises(ItemNotInInventory):
        world.update()
예제 #28
0
def test_remove_system(world, null_system):
    entity = world.create_entity(NullComponent())
    world._flush_component_updates()
    world.add_system(null_system, 0)

    world.remove_system(NullSystem)
    assert null_system.entities['null'] == set()
    assert len(null_system.exits) == 1
    assert null_system.entries[0] == (['null'], entity)
    assert world.systems == {}
    assert not world.has_system(NullSystem)
예제 #29
0
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)
예제 #30
0
def test_can_change_to_current_room(world):
    # But we *can* make rooms circular in nature.
    # I don't know what the point of this is supposed to be. Maybe
    # someone in the future will have a use case for this.
    world.add_system(ChangeRoom(throw_exc=True), 0)
    world.add_system(PerceiveRoom(), 1)
    room = world.create_entity()
    room.add_component(Room(adjacent=[room._uid], ), )
    actor = world.create_entity(RoomPresence(room=room._uid, ), )
    # Let's update to have a clean room state.
    world.update()

    actor.add_component(ChangeRoomAction(room=room._uid, ), )
    world.update()
    room_cmpt = room.get_component(Room)
    assert len(room_cmpt.presences) == 1
    assert actor._uid in room_cmpt.presences
    assert len(room_cmpt.arrived) == 0
    assert len(room_cmpt.continued) == 1
    assert actor._uid in room_cmpt.continued
    assert len(room_cmpt.gone) == 0
예제 #31
0
def test_entity_shorthand(world):
    entity = world.create_entity()
    assert not entity.has(Component)
    assert entity.get(Component, None) is None
    component = Component()
    entity.add(component)
    assert entity.has(Component)
    assert entity.get(Component) is component
    entity.remove(Component)
    assert not entity.has(Component)
    assert entity.get(Component, None) is None
    entity.destroy()
    assert entity not in world.entities
예제 #32
0
def test_can_create_entity(world):
    entity = world.create_entity()
    entity.uid
예제 #33
0
def test_entities_ids_are_unique(world):
    a = world.create_entity()
    b = world.create_entity()
    assert a.uid != b.uid
예제 #34
0
def test_adding_a_duplicate_component_is_an_error(world):
    entity = world.create_entity()
    world.add_component(entity, Component())
    with pytest.raises(DuplicateComponentError):
        world.add_component(entity, Component())
예제 #35
0
def test_getting_a_non_existent_component_is_an_error(world):
    entity = world.create_entity()
    with pytest.raises(NoSuchComponentError):
        world.get_component(entity, Component)
예제 #36
0
def test_can_pass_a_default_when_getting_a_component(world):
    entity = world.create_entity()
    assert world.get_component(entity, Component, missing="foo") == "foo"
예제 #37
0
def test_fails_on_unregistered_components(world):
    e = world.create_entity()
    with pytest.raises(UnregisteredComponentError):
        e.component
예제 #38
0
def test_shortcut_for_registered_components(world):
    world.register('component', Component)
    c = Component()
    e = world.create_entity()
    e.add(c)
    assert e.component == c