<commit_before>import esper
import types
import pytest


@pytest.fixture
def world():
    return esper.CachedWorld()


@pytest.fixture
def populated_world():
    pop_world = esper.CachedWorld()
    create_entities(pop_world, 2000)
    return pop_world


def test_world_instantiation(world):
    assert type(world) == esper.CachedWorld
    assert type(world._next_entity_id) == int
    assert type(world._entities) == dict
    assert type(world._components) == dict
    assert type(world._processors) == list


def test_create_entity(world):
    entity1 = world.create_entity()
    entity2 = world.create_entity()
    assert type(entity1) and type(entity2) == int
    assert entity1 < entity2


def test_delete_entity(world):
    # TODO: handle case where entity has never been assigned components
    entity1 = world.create_entity()
    world.add_component(entity1, ComponentC())
    entity2 = world.create_entity()
    world.add_component(entity2, ComponentD())
    entity3 = world.create_entity()
    world.add_component(entity3, ComponentE())

    assert entity3 == 3
    world.delete_entity(entity3, immediate=True)
    with pytest.raises(KeyError):
        world.components_for_entity(entity3)
    with pytest.raises(KeyError):
        world.delete_entity(999, immediate=True)


def test_component_for_entity(world):
    entity = world.create_entity()
    world.add_component(entity, ComponentC())
    assert isinstance(world.component_for_entity(entity, ComponentC), ComponentC)
    with pytest.raises(KeyError):
        world.component_for_entity(entity, ComponentD)


def test_components_for_entity(world):
    entity = world.create_entity()
    world.add_component(entity, ComponentA())
    world.add_component(entity, ComponentD())
    world.add_component(entity, ComponentE())
    all_components = world.components_for_entity(entity)
    assert type(all_components) == tuple
    assert len(all_components) == 3
    with pytest.raises(KeyError):
        world.components_for_entity(999)


def test_get_component(populated_world):
    assert isinstance(populated_world.get_component(ComponentA), types.GeneratorType)

    for ent, comp in populated_world.get_component(ComponentA):
        assert type(ent) == int
        assert type(comp) == ComponentA


def test_has_component(world):
    entity1 = world.create_entity()
    entity2 = world.create_entity()
    world.add_component(entity1, ComponentA())
    world.add_component(entity2, ComponentB())
    assert world.has_component(entity1, ComponentA) == True
    assert world.has_component(entity1, ComponentB) == False
    assert world.has_component(entity2, ComponentA) == False
    assert world.has_component(entity2, ComponentB) == True


def test_get_two_components(populated_world):
    assert isinstance(populated_world.get_components(ComponentD, ComponentE),
                      types.GeneratorType)

    for ent, comps in populated_world.get_components(ComponentD, ComponentE):
        assert type(ent) == int<commit_msg>Add tests for cached world<commit_after>import esper
import types
import pytest


@pytest.fixture
def world():
    return esper.CachedWorld()


@pytest.fixture
def populated_world():
    pop_world = esper.CachedWorld()
    create_entities(pop_world, 2000)
    return pop_world


def test_world_instantiation(world):
    assert type(world) == esper.CachedWorld
    assert type(world._next_entity_id) == int
    assert type(world._entities) == dict
    assert type(world._components) == dict
    assert type(world._processors) == list


def test_create_entity(world):
    entity1 = world.create_entity()
    entity2 = world.create_entity()
    assert type(entity1) and type(entity2) == int
    assert entity1 < entity2


def test_create_entity_with_components(world):
    entity1 = world.create_entity(ComponentA())
    entity2 = world.create_entity(ComponentB())
    assert world.has_component(entity1, ComponentA) is True
    assert world.has_component(entity1, ComponentB) is False
    assert world.has_component(entity2, ComponentA) is False
    assert world.has_component(entity2, ComponentB) is True


def test_delete_entity(world):
    # TODO: handle case where entity has never been assigned components
    entity1 = world.create_entity()
    world.add_component(entity1, ComponentC())
    entity2 = world.create_entity()
    world.add_component(entity2, ComponentD())
    entity3 = world.create_entity()
    world.add_component(entity3, ComponentE())

    assert entity3 == 3
    world.delete_entity(entity3, immediate=True)
    with pytest.raises(KeyError):
        world.components_for_entity(entity3)
    with pytest.raises(KeyError):
        world.delete_entity(999, immediate=True)


def test_component_for_entity(world):
    entity = world.create_entity()
    world.add_component(entity, ComponentC())
    assert isinstance(world.component_for_entity(entity, ComponentC), ComponentC)
    with pytest.raises(KeyError):
        world.component_for_entity(entity, ComponentD)


def test_components_for_entity(world):
    entity = world.create_entity()
    world.add_component(entity, ComponentA())
    world.add_component(entity, ComponentD())
    world.add_component(entity, ComponentE())
    all_components = world.components_for_entity(entity)
    assert type(all_components) == tuple
    assert len(all_components) == 3
    with pytest.raises(KeyError):
        world.components_for_entity(999)


def test_get_component(populated_world):
    assert isinstance(populated_world.get_component(ComponentA), types.GeneratorType)

    for ent, comp in populated_world.get_component(ComponentA):
        assert type(ent) == int
        assert type(comp) == ComponentA


def test_has_component(world):
    entity1 = world.create_entity()
    entity2 = world.create_entity()
    world.add_component(entity1, ComponentA())
    world.add_component(entity2, ComponentB())
    assert world.has_component(entity1, ComponentA) is True
    assert world.has_component(entity1, ComponentB) is False
    assert world.has_component(entity2, ComponentA) is False
    assert world.has_component(entity2, ComponentB) is True


def test_get_two_components(populated_world):
    assert isinstance(populated_world.get_components(ComponentD, ComponentE),
                      types.GeneratorType)

    for ent, comps in populated_world.get_components(ComponentD, ComponentE):
        assert type(ent) == int
{'fraction_non_alphanumeric': 0.09436619718309859, 'fraction_numerical': 0.01189358372456964, 'mean_word_length': 4.50473729543497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1751715', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1873, 'n_words': 391}