forked from EngoEngine/engo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engo_test.go
300 lines (256 loc) · 7.21 KB
/
engo_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package engo
import (
"bytes"
"log"
"strings"
"testing"
"time"
"engo.io/ecs"
)
type testScene struct{}
func (*testScene) Preload() {}
func (t *testScene) Setup(u Updater) {}
func (*testScene) Type() string { return "testScene" }
type testScene2 struct{}
func (*testScene2) Preload() {}
func (*testScene2) Setup(u Updater) {}
func (*testScene2) Type() string { return "testScene2" }
func (*testScene2) Hide() {
log.Println("Hiding testScene2.")
}
func (*testScene2) Show() {
log.Println("Showing testScene2.")
}
// The tests for engo.go all have to use the headless option. Non-headless stuff is not
// testable via the cl only, and those are taken care of by building the demos via Travis CI
func TestRunHeadlessNoRunDefaults(t *testing.T) {
Run(RunOptions{
NoRun: true,
HeadlessMode: true,
}, &testScene{})
if opts.FPSLimit != 60 {
t.Error("FPSLimit was not defaulted to 60")
}
if opts.MSAA != 1 {
t.Error("MSAA was not defaulted to 1")
}
if opts.AssetsRoot != "assets" {
t.Error("AssetsRoot was not defaulted to assets")
}
if opts.Width != 800 {
t.Error("Width was not defaulted to 800")
}
if opts.Height != 800 {
t.Error("Height was not defaulted to 800")
}
}
func TestSetScaleOnResize(t *testing.T) {
Run(RunOptions{
HeadlessMode: true,
NoRun: true,
}, &testScene{})
if opts.ScaleOnResize {
t.Error("ScaleOnResize didn't default to false.")
}
SetScaleOnResize(true)
if !opts.ScaleOnResize {
t.Error("SetScaleOnResize didn't set properly.")
}
}
func TestSetOverrideCloseAction(t *testing.T) {
Run(RunOptions{
HeadlessMode: true,
NoRun: true,
}, &testScene{})
if opts.OverrideCloseAction {
t.Error("OverrideCloseAction didn't default to false.")
}
SetOverrideCloseAction(true)
if !opts.OverrideCloseAction {
t.Error("SetOverrideCloseAction didn't set properly.")
}
}
func TestHeadless(t *testing.T) {
Run(RunOptions{
HeadlessMode: true,
NoRun: true,
}, &testScene{})
if Headless() != opts.HeadlessMode {
t.Error("Headless didn't return the proper value.")
}
}
func TestScaleOnResize(t *testing.T) {
Run(RunOptions{
HeadlessMode: true,
NoRun: true,
}, &testScene{})
if ScaleOnResize() != opts.ScaleOnResize {
t.Error("ScaleOnResize didn't return the proper value.")
}
}
func TestGameWidthHeight(t *testing.T) {
Run(RunOptions{
HeadlessMode: true,
NoRun: true,
Width: 100,
Height: 50,
}, &testScene{})
if GameWidth() != 100 {
t.Error("Width didn't return the proper value.")
}
if GameHeight() != 50 {
t.Error("Height didn't return the proper value.")
}
}
func TestSetFPSLimit(t *testing.T) {
Run(RunOptions{
HeadlessMode: true,
NoRun: true,
}, &testScene{})
SetFPSLimit(5)
if opts.FPSLimit != 5 {
t.Error("SetFPSLimit didn't set properly.")
}
expected := "FPS Limit out of bounds. Requires > 0"
if err := SetFPSLimit(-5); err == nil {
t.Error("Error wasn't recieved when SetFPSLimit was set to a negative number.")
} else if err.Error() != expected {
t.Errorf("Wrong error recieved when SetFPSLimit was set to a negative number. want %v, got %v", expected, err.Error())
}
}
func TestRunNegativeMSAAPanic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Error("No panic when MSAA was set to -5")
}
if r != "MSAA has to be greater or equal to 0" {
t.Errorf("Wrong panic when MSAA was set to -5, got: %v", r)
}
}()
Run(RunOptions{
NoRun: true,
HeadlessMode: true,
MSAA: -5,
}, &testScene{})
}
func TestRunStandardInputs(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
Run(RunOptions{
NoRun: true,
HeadlessMode: true,
StandardInputs: true,
}, &testScene{})
expected := "Using standard inputs\n"
if !strings.HasSuffix(buf.String(), expected) {
t.Error("setting standard inputs did not write expected output to log")
}
}
type testRunScene struct {
updates int
}
func (*testRunScene) Preload() {}
func (t *testRunScene) Setup(u Updater) {
w, _ := u.(*ecs.World)
w.AddSystem(&testUpdate{updates: t.updates})
}
func (*testRunScene) Type() string { return "testRunScene" }
type testUpdate struct {
updates, current int
}
func (*testUpdate) Remove(ecs.BasicEntity) {}
func (t *testUpdate) Update(float32) {
t.current++
if t.current >= t.updates {
Exit()
}
}
// This test tests running headless but also letting it go into the runloop
func TestRunHeadless(t *testing.T) {
testChan := make(chan struct{})
go func() {
Run(RunOptions{
HeadlessMode: true,
}, &testRunScene{1})
testChan <- struct{}{}
}()
select {
case <-testChan:
return
case <-time.After(1 * time.Second):
t.Error("Timed out while waiting for Headless Run to return from loop. Exit wasn't called within 1 second.")
}
}
func TestOverrideCloseAction(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
opts.OverrideCloseAction = true
expected := "[WARNING] default close action set to false, please make sure you manually handle this\n"
closeEvent()
if !strings.HasSuffix(buf.String(), expected) {
t.Error("calling closeEvent with Override set did not write expected output to log")
}
}
func TestSetGlobalScale(t *testing.T) {
data := []struct {
in Point
exp Point
}{
{Point{X: 5, Y: 5}, Point{X: 5, Y: 5}},
{Point{X: -5, Y: 5}, Point{X: 1, Y: 1}},
{Point{X: 5, Y: -5}, Point{X: 1, Y: 1}},
{Point{X: -5, Y: -5}, Point{X: 1, Y: 1}},
}
for _, d := range data {
SetGlobalScale(d.in)
if opts.GlobalScale.X != d.exp.X || opts.GlobalScale.Y != d.exp.Y {
t.Errorf("SetGlobalScale did not set properly. was: %v, expected: %v", opts.GlobalScale, d.exp)
}
}
}
func TestSceneSwitching(t *testing.T) {
RegisterScene(&testScene2{})
var buf bytes.Buffer
log.SetOutput(&buf)
Run(RunOptions{
NoRun: true,
HeadlessMode: true,
}, &testScene{})
SetSceneByName("testScene2", false)
if CurrentScene().Type() != "testScene2" {
t.Errorf("CurrentScene got wrong scene. was: %v, expected: %v", CurrentScene().Type(), "testScene2")
}
SetSceneByName("testScene", false)
expected := "Hiding testScene2.\n"
if !strings.HasSuffix(buf.String(), expected) {
t.Errorf("Did not properly set testScene1 and hide testScene2. was %v, expected: %v", buf.String(), expected)
}
buf.Reset()
SetSceneByName("testScene2", false)
expected = "Showing testScene2.\n"
if !strings.HasSuffix(buf.String(), expected) {
t.Errorf("Did not properly set and show testScene2. was: %v, expected: %v", buf.String(), expected)
}
err := SetSceneByName("doesNotExistScene", true)
if err == nil {
t.Error("No error when setting scene that doesn't exist.")
}
expected = "scene not registered:"
if !strings.HasPrefix(err.Error(), expected) {
t.Errorf("Did not recieve correct error for setting a scene that doesn't exist. was: %v, expected:%v", err.Error(), expected)
}
}
func TestUtils(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
expectedImpl := "is not yet implemented on this platform\n"
expectedType := "type not supported\n"
if notImplemented("testing "); !strings.HasSuffix(buf.String(), expectedImpl) {
t.Errorf("Did not properly log notImplemented. got: %v", buf.String())
}
buf.Reset()
if unsupportedType(); !strings.HasSuffix(buf.String(), expectedType) {
t.Errorf("Did not properly log unsupportedType. got: %v", buf.String())
}
}