-
Notifications
You must be signed in to change notification settings - Fork 10
/
entity_test.go
76 lines (65 loc) · 1.89 KB
/
entity_test.go
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
package ecs_test
import (
"testing"
"github.com/andygeiss/ecs"
)
func TestEntity_NewEntity_Should_Create_A_Correct_Mask(t *testing.T) {
entity := ecs.NewEntity("e", []ecs.Component{
&mockComponent{name: "position", mask: 1},
})
if entity.Mask() != 1 {
t.Errorf("Entity mask should be 1, but got %d", entity.Mask())
}
}
func TestEntity_Add_Should_Work_With_Multiple_Components(t *testing.T) {
entity := ecs.NewEntity("e", []ecs.Component{
&mockComponent{name: "position", mask: 1},
})
entity.Add(&mockComponent{name: "velocity", mask: 2})
if entity.Mask() != 3 {
t.Errorf("Entity mask should be 3, but got %d", entity.Mask())
}
}
func TestEntity_Add_Should_Not_Add_Existing_Component(t *testing.T) {
entity := ecs.NewEntity("e", []ecs.Component{
&mockComponent{name: "position", mask: 1},
&mockComponent{name: "velocity", mask: 2},
})
entity.Add(&mockComponent{name: "velocity", mask: 2})
if len(entity.Components) != 2 {
t.Errorf("Component count should be 2, but got %d", len(entity.Components))
}
}
func TestEntity_Get_Should_Return_Component(t *testing.T) {
entity := ecs.NewEntity("e", []ecs.Component{
&mockComponent{name: "position", mask: 1},
})
component := entity.Get(1)
if component == nil {
t.Error("Component should not be nil")
}
}
func TestEntity_Remove_Should_Work_With_Multiple_Components(t *testing.T) {
entity := ecs.NewEntity("e", []ecs.Component{
&mockComponent{name: "position", mask: 1},
&mockComponent{name: "size", mask: 2},
&mockComponent{name: "velocity", mask: 4},
})
entity.Remove(4)
if entity.Mask() != 3 {
t.Errorf("Entity mask should be 1, but got %d", entity.Mask())
}
}
/*
_ _ _
_ _| |_(_) |___
| | | | __| | / __|
| |_| | |_| | \__ \
\__,_|\__|_|_|___/
*/
type mockComponent struct {
mask uint64
name string
}
func (c *mockComponent) Mask() uint64 { return c.mask }
func (c *mockComponent) Name() string { return c.name }