-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathworld_test.go
141 lines (125 loc) · 2.82 KB
/
world_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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package ecs
import (
"testing"
)
func TestWorld_AddSystemInterface(t *testing.T) {
type foo interface {
a() string
}
var fooInstance *foo
type args struct {
sys SystemAddByInterfacer
in interface{}
ex interface{}
}
tests := []struct {
name string
args args
}{
// {"adds individual interface", args{}},
{"adds multiple interfaces", args{nil, fooInstance, nil}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := new(World)
w.AddSystemInterface(tt.args.sys, tt.args.in, tt.args.ex)
})
}
}
type simpleEntity struct {
BasicEntity
}
type entent struct {
*BasicEntity
}
type simpleSystem struct {
entities []entent
}
func (s *simpleSystem) Add(b *BasicEntity) {
s.entities = append(s.entities, entent{b})
}
func (s *simpleSystem) AddByInterface(i Identifier) {
obj, ok := i.(BasicFace)
if ok {
s.Add(obj.GetBasicEntity())
}
}
func (s *simpleSystem) Remove(b BasicEntity) {}
func (s *simpleSystem) Update(dt float32) {}
func TestWorld_AddEntity(t *testing.T) {
type args struct {
systems []SystemAddByInterfacer
e Identifier
}
tests := []struct {
name string
args args
}{
{"works with multiple interfaces", args{
systems: []SystemAddByInterfacer{&simpleSystem{}},
e: &simpleEntity{NewBasic()},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := new(World)
sys := new(simpleSystem)
var face *BasicFace
w.AddSystemInterface(sys, []interface{}{face}, nil)
w.AddEntity(tt.args.e)
if len(sys.entities) == 0 {
t.Error(len(sys.entities))
}
})
}
}
type priorityChangeSystem struct {
Rank int
}
func (s *priorityChangeSystem) Priority() int {
return s.Rank
}
func (s *priorityChangeSystem) Add(b *BasicEntity) {}
func (s *priorityChangeSystem) AddByInterface(i Identifier) {
obj, ok := i.(BasicFace)
if ok {
s.Add(obj.GetBasicEntity())
}
}
func (s *priorityChangeSystem) Remove(b BasicEntity) {}
func (s *priorityChangeSystem) Update(dt float32) {}
func TestWorld_SortSystems(t *testing.T) {
w := new(World)
one := priorityChangeSystem{Rank: 1}
w.AddSystem(&one)
two := priorityChangeSystem{Rank: 2}
w.AddSystem(&two)
expected := []System{
&two,
&one,
}
for idx, sys := range w.Systems() {
p := sys.(Prioritizer)
exp := expected[idx].(Prioritizer)
if p.Priority() != exp.Priority() {
t.Error("Systems were not in the correct order")
}
}
one.Rank = 5
for idx, sys := range w.Systems() {
p := sys.(Prioritizer)
exp := expected[idx].(Prioritizer)
if p.Priority() != exp.Priority() {
t.Error("Systems were switched before sort wwas called")
}
}
w.SortSystems()
for idx, sys := range w.Systems() {
p := sys.(Prioritizer)
exp := expected[len(expected)-1-idx].(Prioritizer)
if p.Priority() != exp.Priority() {
t.Error("Systems were switched before sort wwas called")
}
}
}