1+ package tests
2+
3+ import " core:testing"
4+ import " ../ecs"
5+
6+ Position :: struct {
7+ x, y: f32 ,
8+ }
9+
10+ Tag :: struct {}
11+
12+ @(test)
13+ test_entity_lifecycle :: proc (t: ^testing.T) {
14+ world := ecs.create_world ()
15+ defer ecs.destroy_world (world)
16+
17+ e := ecs.create_entity (world)
18+ testing.expect (t, ecs.is_alive (world, e), " Entity must be alive immediately after creation" )
19+
20+ ecs.destroy_entity (world, e)
21+ testing.expect (t, !ecs.is_alive (world, e), " Entity must be dead after destruction" )
22+ }
23+
24+ @(test)
25+ test_entity_generation :: proc (t: ^testing.T) {
26+ world := ecs.create_world ()
27+ defer ecs.destroy_world (world)
28+
29+ e1 := ecs.create_entity (world)
30+ ecs.destroy_entity (world, e1)
31+
32+ e2 := ecs.create_entity (world)
33+
34+ testing.expect (t, e1 != e2, " New entity must have a different ID (generation) even if reusing index" )
35+ testing.expect (t, !ecs.is_alive (world, e1), " Old entity handle must remain invalid" )
36+ testing.expect (t, ecs.is_alive (world, e2), " New entity handle must be valid" )
37+ }
38+
39+ @(test)
40+ test_component_add_get :: proc (t: ^testing.T) {
41+ world := ecs.create_world ()
42+ defer ecs.destroy_world (world)
43+
44+ e := ecs.create_entity (world)
45+ ecs.add (world, e, Position{10 , 20 })
46+
47+ testing.expect (t, ecs.has (world, e, Position), " Entity must have Position component" )
48+
49+ pos := ecs.get (world, e, Position)
50+ testing.expect (t, pos != nil , " Get should return a valid pointer" )
51+ testing.expect (t, pos.x == 10 && pos.y == 20 , " Component data must match initial value" )
52+ }
53+
54+ @(test)
55+ test_component_set :: proc (t: ^testing.T) {
56+ world := ecs.create_world ()
57+ defer ecs.destroy_world (world)
58+
59+ e := ecs.create_entity (world)
60+ ecs.add (world, e, Position{10 , 20 })
61+
62+ ecs.set (world, e, Position{30 , 40 })
63+
64+ pos := ecs.get (world, e, Position)
65+ testing.expect (t, pos.x == 30 && pos.y == 40 , " Component data must be updated after set" )
66+ }
67+
68+ @(test)
69+ test_component_remove :: proc (t: ^testing.T) {
70+ world := ecs.create_world ()
71+ defer ecs.destroy_world (world)
72+
73+ e := ecs.create_entity (world)
74+ ecs.add (world, e, Position{10 , 20 })
75+
76+ ecs.remove (world, e, Position)
77+
78+ testing.expect (t, !ecs.has (world, e, Position), " Entity must not have Position component after remove" )
79+
80+ record := world.entity_index[u32 (e)]
81+ testing.expect (t, record.archetype == nil , " Entity archetype must be nil when empty" )
82+ }
83+
84+ @(test)
85+ test_add_tag_type :: proc (t: ^testing.T) {
86+ world := ecs.create_world ()
87+ defer ecs.destroy_world (world)
88+
89+ e := ecs.create_entity (world)
90+
91+ ecs.add (world, e, Tag)
92+
93+ val := ecs.get (world, e, Tag)
94+ testing.expect (t, val == nil , " Get must return nil for tags (zero-sized components)" )
95+
96+ testing.expect (t, ecs.has (world, e, Tag), " Has must return true for tag" )
97+ }
98+
99+ @(test)
100+ test_add_zero_sized_value :: proc (t: ^testing.T) {
101+ world := ecs.create_world ()
102+ defer ecs.destroy_world (world)
103+
104+ e := ecs.create_entity (world)
105+
106+ ecs.add (world, e, Tag{})
107+
108+ val := ecs.get (world, e, Tag)
109+ testing.expect (t, val == nil , " Get must return nil for zero-sized value" )
110+
111+ testing.expect (t, ecs.has (world, e, Tag), " Has must return true for zero-sized value" )
112+ }
113+
114+ @(test)
115+ test_remove_tag :: proc (t: ^testing.T) {
116+ world := ecs.create_world ()
117+ defer ecs.destroy_world (world)
118+
119+ e := ecs.create_entity (world)
120+ ecs.add (world, e, Tag)
121+
122+ testing.expect (t, ecs.has (world, e, Tag), " Entity must have tag initially" )
123+
124+ ecs.remove (world, e, Tag)
125+
126+ testing.expect (t, !ecs.has (world, e, Tag), " Has must return false after removing tag" )
127+ }
0 commit comments