summaryrefslogtreecommitdiff
path: root/composeApp/src/commonTest/kotlin/coffee/liz/ecs/EntityTest.kt
blob: e807bd21d24e776b72a610678907fd356aaa2e40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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<Position>())
        assertEquals(position, entity.get<Position>())
    }

    @Test
    fun `can remove component from entity`() {
        val entity = Entity(0)
        entity.add(Position(10f, 20f))

        entity.remove<Position>()

        assertFalse(entity.has<Position>())
        assertNull(entity.get<Position>())
    }

    @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<Position>())
        assertTrue(entity.has<Velocity>())
    }

    @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<Position>()

        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)
    }
}