From 55633706a2a7a85dcf1129a162061559faa6128b Mon Sep 17 00:00:00 2001 From: Sam <61994039+fourthDimensional@users.noreply.github.com> Date: Tue, 3 Jun 2025 20:19:54 -0500 Subject: [PATCH] Add tests for World class object management and querying functionality --- .gitea/workflows/run_tests.yml | 4 +- tests/test_world.py | 87 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tests/test_world.py diff --git a/.gitea/workflows/run_tests.yml b/.gitea/workflows/run_tests.yml index 68a0261..213cfd7 100644 --- a/.gitea/workflows/run_tests.yml +++ b/.gitea/workflows/run_tests.yml @@ -22,7 +22,7 @@ jobs: run: uv sync --locked --all-extras --dev - name: Run tests - run: uv run pytest tests --junit-xml=pytest-results.xml + run: PYTHONPATH=$(pwd) uv run pytest tests --junit-xml=pytest-results.xml - name: Upload test results if: always() @@ -34,4 +34,4 @@ jobs: - name: Print test summary if: always() - run: uv run pytest tests --tb=short || true \ No newline at end of file + run: PYTHONPATH=$(pwd) uv run pytest tests --tb=short || true \ No newline at end of file diff --git a/tests/test_world.py b/tests/test_world.py new file mode 100644 index 0000000..1e6adb5 --- /dev/null +++ b/tests/test_world.py @@ -0,0 +1,87 @@ +import pytest +from world.world import World, Position, BaseEntity + + +class DummyEntity(BaseEntity): + def __init__(self, position): + super().__init__(position) + self.ticked = False + self.rendered = False + + def tick(self, interactable=None): + self.ticked = True + return self + + def render(self, camera, screen): + self.rendered = True + + +@pytest.fixture +def world(): + return World(partition_size=10, world_size=(100, 100)) + + +def test_add_object_and_get_objects(world): + entity = DummyEntity(Position(x=0, y=0)) + world.add_object(entity) + assert entity in world.get_objects() + + +def test_query_objects_within_radius(world): + e1 = DummyEntity(Position(x=0, y=0)) + e2 = DummyEntity(Position(x=5, y=0)) + e3 = DummyEntity(Position(x=20, y=0)) + world.add_object(e1) + world.add_object(e2) + world.add_object(e3) + found = world.query_objects_within_radius(0, 0, 10) + assert e1 in found + assert e2 in found + assert e3 not in found + + +def test_query_objects_in_range(world): + e1 = DummyEntity(Position(x=1, y=1)) + e2 = DummyEntity(Position(x=5, y=5)) + e3 = DummyEntity(Position(x=20, y=20)) + world.add_object(e1) + world.add_object(e2) + world.add_object(e3) + found = world.query_objects_in_range(0, 0, 10, 10) + assert e1 in found + assert e2 in found + assert e3 not in found + + +def test_query_closest_object(world): + e1 = DummyEntity(Position(x=0, y=0)) + e2 = DummyEntity(Position(x=10, y=0)) + world.add_object(e1) + world.add_object(e2) + closest = world.query_closest_object(1, 0) + assert closest == e1 + + +def test_tick_all_removes_dead(world): + e1 = DummyEntity(Position(x=0, y=0)) + e2 = DummyEntity(Position(x=10, y=0)) + e2.flags["death"] = True + world.add_object(e1) + world.add_object(e2) + world.tick_all() + objs = world.get_objects() + assert e1 in objs + assert e2 not in objs + + +def test_tick_all_calls_tick(world): + e1 = DummyEntity(Position(x=0, y=0)) + world.add_object(e1) + world.tick_all() + assert e1.ticked + + +def test_add_object_out_of_bounds(world): + entity = DummyEntity(Position(x=1000, y=1000)) + with pytest.raises(ValueError): + world.add_object(entity)