From 64f825465de9fa30c4dfe2707067efdb96110db8 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 23 Oct 2025 21:59:37 -0700 Subject: Init --- .../commonTest/kotlin/coffee/liz/ecs/EntityTest.kt | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 composeApp/src/commonTest/kotlin/coffee/liz/ecs/EntityTest.kt (limited to 'composeApp/src/commonTest/kotlin/coffee/liz/ecs/EntityTest.kt') diff --git a/composeApp/src/commonTest/kotlin/coffee/liz/ecs/EntityTest.kt b/composeApp/src/commonTest/kotlin/coffee/liz/ecs/EntityTest.kt new file mode 100644 index 0000000..e807bd2 --- /dev/null +++ b/composeApp/src/commonTest/kotlin/coffee/liz/ecs/EntityTest.kt @@ -0,0 +1,116 @@ +package coffee.liz.ecs + +import kotlin.test.* + +// Test components +data class Position(val x: Float, val y: Float) : Component +data class Velocity(val dx: Float, val dy: Float) : Component +data class Health(val value: Int) : Component + +class EntityTest { + + @Test + fun `entity has unique id`() { + val entity1 = Entity(1) + val entity2 = Entity(2) + + assertNotEquals(entity1.id, entity2.id) + } + + @Test + fun `can add component to entity`() { + val entity = Entity(0) + val position = Position(10f, 20f) + + entity.add(position) + + assertTrue(entity.has()) + assertEquals(position, entity.get()) + } + + @Test + fun `can remove component from entity`() { + val entity = Entity(0) + entity.add(Position(10f, 20f)) + + entity.remove() + + assertFalse(entity.has()) + assertNull(entity.get()) + } + + @Test + fun `add returns entity for chaining`() { + val entity = Entity(0) + + val result = entity + .add(Position(10f, 20f)) + .add(Velocity(1f, 2f)) + + assertSame(entity, result) + assertTrue(entity.has()) + assertTrue(entity.has()) + } + + @Test + fun `can replace component by adding same type`() { + val entity = Entity(0) + entity.add(Position(10f, 20f)) + entity.add(Position(30f, 40f)) + + val position = entity.get() + + assertNotNull(position) + assertEquals(30f, position.x) + assertEquals(40f, position.y) + } + + @Test + fun `hasAll returns true when entity has all components`() { + val entity = Entity(0) + entity.add(Position(10f, 20f)) + entity.add(Velocity(1f, 2f)) + + assertTrue(entity.hasAll(Position::class, Velocity::class)) + } + + @Test + fun `hasAll returns false when entity missing component`() { + val entity = Entity(0) + entity.add(Position(10f, 20f)) + + assertFalse(entity.hasAll(Position::class, Velocity::class)) + } + + @Test + fun `componentTypes returns all component types`() { + val entity = Entity(0) + entity.add(Position(10f, 20f)) + entity.add(Velocity(1f, 2f)) + entity.add(Health(100)) + + val types = entity.componentTypes() + + assertEquals(3, types.size) + assertTrue(types.contains(Position::class)) + assertTrue(types.contains(Velocity::class)) + assertTrue(types.contains(Health::class)) + } + + @Test + fun `entities with same id are equal`() { + val entity1 = Entity(1) + val entity2 = Entity(1) + + assertEquals(entity1, entity2) + assertEquals(entity1.hashCode(), entity2.hashCode()) + } + + @Test + fun `entities with different id are not equal`() { + val entity1 = Entity(1) + val entity2 = Entity(2) + + assertNotEquals(entity1, entity2) + } +} -- cgit v1.2.3-70-g09d2