Mi2dRPGamEng/tests/test_world_creation.py

75 lines
2.4 KiB
Python

from pathlib import Path
import pytest
from engine.defs import DefRegistry
from engine.map_loader import MapLoader
from engine.world_creation import AVAILABLE_WORLD_TEMPLATES, WorldCreationMenu, WorldTemplate, build_world_from_template
DEFS_DIR = Path(__file__).resolve().parent.parent / "resources" / "defs"
@pytest.fixture()
def registry():
return DefRegistry.load(DEFS_DIR)
@pytest.fixture()
def map_loader():
return MapLoader(DEFS_DIR / "maps")
# --- WorldCreationMenu -----------------------------------------------------------------------
def test_menu_starts_with_the_available_templates():
menu = WorldCreationMenu()
assert [t.id for t in menu.templates] == [t.id for t in AVAILABLE_WORLD_TEMPLATES]
assert menu.selected_index is None
def test_currently_only_one_template_is_available():
# campaign/survival/creative (procedurally generated) modes are planned follow-ups, not yet
# implemented - this pins down the "just the test map for now" scope explicitly
assert [t.id for t in AVAILABLE_WORLD_TEMPLATES] == ["test_map"]
def test_select_sets_selected_index():
menu = WorldCreationMenu()
menu.select(0)
assert menu.selected_index == 0
def test_select_out_of_range_is_ignored():
menu = WorldCreationMenu()
menu.select(99)
assert menu.selected_index is None
def test_selected_template_reads_back_the_right_one():
menu = WorldCreationMenu()
menu.select(0)
assert menu.selected_template() is AVAILABLE_WORLD_TEMPLATES[0]
def test_selected_template_none_before_any_selection():
menu = WorldCreationMenu()
assert menu.selected_template() is None
# --- build_world_from_template ------------------------------------------------------------------
def test_build_world_from_template_loads_the_map_and_its_embeddings(registry, map_loader):
template = AVAILABLE_WORLD_TEMPLATES[0]
world = build_world_from_template(template, registry, map_loader)
assert "harbor_world" in world.maps
assert "ship_test" in world.maps # embedded child map also gets added
assert world.entities == {} # world creation alone spawns no one
def test_build_world_from_template_raises_for_a_generator_only_template(registry, map_loader):
generator_template = WorldTemplate(id="campaign", name="Campaign", map_file=None)
with pytest.raises(NotImplementedError):
build_world_from_template(generator_template, registry, map_loader)