-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_test.go
364 lines (351 loc) · 10.3 KB
/
player_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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"bytes"
"testing"
termbox "github.com/nsf/termbox-go"
)
const playerTestMapPath = "files/test/player/"
func TestMoveCross(t *testing.T) {
const initX, initY = 7, 5
cases := map[string]struct {
inputNum int
inputG bool
x int
y int
expectedX int
expectedY int
}{
"left": {0, false, -1, 0, initX - 1, initY},
"left with input number": {2, false, -1, 0, initX - 2, initY},
"left to the obstacle": {6, false, -1, 0, initX - 4, initY},
"right": {0, false, 1, 0, initX + 1, initY},
"right with input number": {2, false, 1, 0, initX + 2, initY},
"right to the obstacle": {6, false, 1, 0, initX + 4, initY},
"up": {0, false, 0, -1, initX, initY - 1},
"up with input number": {2, false, 0, -1, initX, initY - 2},
"up to the obstacle": {6, false, 0, -1, initX, initY - 2},
"down": {0, false, 0, 1, initX, initY + 1},
"down with input number": {2, false, 0, 1, initX, initY + 2},
"down to the obstacle": {6, false, 0, 1, initX, initY + 2},
// Check the input value is processed after inputNum and inputG are reset.
"with input number and G": {2, true, -1, 0, initX - 1, initY},
}
for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
p := &player{
x: initX,
y: initY,
inputNum: tt.inputNum,
inputG: tt.inputG,
}
_, offset, err := playerActionTestInit(t, playerTestMapPath+"move_cross.txt", p)
if err != nil {
t.Error(err)
}
p.x += offset
tt.expectedX += offset
p.moveCross(tt.x, tt.y)
if !(p.x == tt.expectedX && p.y == tt.expectedY) {
t.Errorf("expected %d %d but %d %d", tt.expectedX, tt.expectedY, p.x, p.y)
}
if !(p.inputNum == 0 && p.inputG == false) {
t.Errorf("expected %d %t but %d %t", 0, false, p.inputNum, p.inputG)
}
})
}
}
func TestMoveByWord(t *testing.T) {
cases := map[string]struct {
inputNum int
inputChar rune
inputG bool
initX int
initY int
expectedX int
expectedY int
expectedState int
}{
// Check if coordinates are as expected.
"w: from space": {0, 'w', false, 14, 1, 16, 1, continuing},
"w: from word": {0, 'w', false, 18, 1, 21, 1, continuing},
"b: from space": {0, 'b', false, 14, 1, 9, 1, continuing},
"b: from middle of word": {0, 'b', false, 11, 1, 9, 1, continuing},
"b: from beginning of word": {0, 'b', false, 9, 1, 5, 1, continuing},
"e: from space": {0, 'e', false, 14, 1, 19, 1, continuing},
"e: from middle of word": {0, 'e', false, 17, 1, 19, 1, continuing},
"e: from end of word": {0, 'e', false, 19, 1, 23, 1, continuing},
// Check the player doesn't cross the obstacle.
"w: to the obstacle": {0, 'w', false, 14, 4, 19, 4, continuing},
"b: to the obstacle": {0, 'b', false, 14, 4, 9, 4, continuing},
"e: to the obstacle": {0, 'e', false, 14, 4, 19, 4, continuing},
// Check the player doesn't cross the boundary.
"w: to the boundary": {4, 'w', false, 14, 1, 27, 1, continuing},
"b: to the boundary": {4, 'b', false, 14, 1, 1, 1, continuing},
"e: to the boundary": {4, 'e', false, 14, 1, 27, 1, continuing},
// Check poison is a string.
"w: beyond the poison": {2, 'w', false, 1, 2, 16, 2, lose},
"b: beyond the poison": {0, 'b', false, 14, 2, 2, 2, lose},
"e: beyond the poison": {0, 'e', false, 1, 2, 4, 2, lose},
// Check an enemy is not a string.
"w: beyond the enemy": {2, 'w', false, 14, 2, 18, 2, lose},
"b: beyond the enemy": {2, 'b', false, 27, 2, 16, 2, lose},
"e: beyond the enemy": {2, 'e', false, 14, 2, 18, 2, lose},
// Check the input value is processed after inputNum and inputG are reset.
"with input number and G": {4, 'w', true, 14, 1, 16, 1, continuing},
}
for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
p := &player{
x: tt.initX,
y: tt.initY,
inputNum: tt.inputNum,
inputG: tt.inputG,
state: continuing,
}
_, offset, err := playerActionTestInit(t, playerTestMapPath+"move_by_word.txt", p)
if err != nil {
t.Error(err)
}
p.x += offset
tt.expectedX += offset
switch tt.inputChar {
case 'w':
p.moveByWord(p.toBeginningOfNextWord)
case 'b':
p.moveByWord(p.toBeginningPrevWord)
case 'e':
p.moveByWord(p.toEndOfCurrentWord)
}
if !(p.x == tt.expectedX && p.y == tt.expectedY) {
t.Errorf("expected %d %d but %d %d", tt.expectedX, tt.expectedY, p.x, p.y)
}
if p.state != tt.expectedState {
t.Errorf("expected %d but %d", tt.expectedState, p.state)
}
if !(p.inputNum == 0 && p.inputG == false) {
t.Errorf("expected %d %t but %d %t", 0, false, p.inputNum, p.inputG)
}
})
}
}
func TestJumpOnCurrentLine(t *testing.T) {
const initX = 14
cases := map[string]struct {
initY int
toLeftEdgeX int
toRightEdgeX int
}{
"to the space": {1, 1, 27},
"to the apple": {2, 1, 27},
"to the obstacle": {3, 4, 24},
"to the enemy": {4, 1, 27},
"to the poison": {5, 1, 27},
}
for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
p := &player{
x: initX,
y: tt.initY,
}
_, offset, err := playerActionTestInit(t, playerTestMapPath+"jump_on_current_line.txt", p)
if err != nil {
t.Error(err)
}
p.x += offset
tt.toLeftEdgeX += offset
tt.toRightEdgeX += offset
p.jumpOnCurrentLine(p.toLeftEdge)
if !(p.x == tt.toLeftEdgeX && p.y == tt.initY) {
t.Errorf("expected %d %d but %d %d", tt.toLeftEdgeX, tt.initY, p.x, p.y)
}
if !(p.inputNum == 0 && p.inputG == false) {
t.Errorf("expected %d %t but %d %t", 0, false, p.inputNum, p.inputG)
}
p.jumpOnCurrentLine(p.toRightEdge)
if !(p.x == tt.toRightEdgeX && p.y == tt.initY) {
t.Errorf("expected %d %d but %d %d", tt.toRightEdgeX, tt.initY, p.x, p.y)
}
})
}
}
func TestJumpAcrossLine(t *testing.T) {
const initX, initY = 14, 3
cases := map[string]struct {
inputNum int
inputG bool
inputChar rune
expectedInputNum int
expectedInputG bool
expectedX int
expectedY int
mapFileName string
}{
"gg": {0, true, 'g', 0, false, 27, 1, "jump_across_line.txt"},
"Ngg": {2, true, 'g', 0, false, 27, 1, "jump_across_line.txt"},
"G": {0, false, 'G', 0, false, 27, 5, "jump_across_line.txt"},
"NG": {2, false, 'G', 0, false, 27, 1, "jump_across_line.txt"},
// Check player behavior when there is no apples in the target row.
"gg no apples": {0, true, 'g', 0, false, 1, 1, "jump_across_line_no_apples.txt"},
"Ngg no apples": {4, true, 'g', 0, false, 1, 3, "jump_across_line_no_apples.txt"},
"G no apples": {0, false, 'G', 0, false, 1, 5, "jump_across_line_no_apples.txt"},
"NG no apples": {4, false, 'G', 0, false, 1, 3, "jump_across_line_no_apples.txt"},
// Check that inputs are saved.
"Ng": {3, false, 'g', 3, true, 14, 3, "jump_across_line.txt"},
// Check that inputs are reset.
"Ng*": {3, true, 'r', 0, false, 14, 3, "jump_across_line.txt"},
}
for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
p := &player{
x: initX,
y: initY,
inputG: tt.inputG,
inputNum: tt.inputNum,
}
stage, offset, err := playerActionTestInit(t, playerTestMapPath+tt.mapFileName, p)
if err != nil {
t.Error(err)
}
p.x += offset
tt.expectedX += offset
switch tt.inputChar {
case 'g':
p.jumpAcrossLine(p.toFirstLine, stage, tt.inputChar)
case 'G':
p.jumpAcrossLine(p.toLastLine, stage, tt.inputChar)
}
if !(p.x == tt.expectedX && p.y == tt.expectedY) {
t.Errorf("expected %d %d but %d %d", tt.expectedX, tt.expectedY, p.x, p.y)
}
if !(p.inputG == tt.expectedInputG || p.inputNum != tt.expectedInputNum) {
t.Errorf("expected %t %d but %t %d", tt.expectedInputG, tt.expectedInputNum, p.inputG, p.inputNum)
}
})
}
}
func TestJudgeMoveResult(t *testing.T) {
const initX, initY = 7, 4
cases := map[string]struct {
x int
y int
expectedState int
}{
"win": {-1, 0, win},
"lost to the enemy(H)": {1, 0, lose},
"lost to the enemy(G)": {0, -1, lose},
"lost by poison": {0, 1, lose},
}
for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
p := &player{
x: initX,
y: initY,
state: continuing,
}
_, offset, err := playerActionTestInit(t, playerTestMapPath+"judge_move_result.txt", p)
if err != nil {
t.Error(err)
}
p.x += offset
p.moveCross(tt.x, tt.y)
if p.state != tt.expectedState {
t.Errorf("expected %d but %d", tt.expectedState, p.state)
}
})
}
}
func TestIsInputNum(t *testing.T) {
type expectedValues struct {
s string
b bool
}
cases := map[string]struct {
player player
arg rune
expected expectedValues
}{
"Argument cannot be converted to a number.": {
player: player{
inputNum: 0,
},
arg: 'k',
expected: expectedValues{
s: "k",
b: false,
},
},
"Argument can be converted to a number.": {
player: player{
inputNum: 0,
},
arg: '2',
expected: expectedValues{
s: "2",
b: true,
},
},
"0 is the number 0.": {
player: player{
inputNum: 1,
},
arg: '0',
expected: expectedValues{
s: "0",
b: true,
},
},
"0 is the special string 0": {
player: player{
inputNum: 0,
},
arg: '0',
expected: expectedValues{
s: "0",
b: false,
},
},
}
for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
s, b := tt.player.isInputNum(tt.arg)
if s != tt.expected.s || b != tt.expected.b {
t.Errorf("expected %s %t but %s %t", tt.expected.s, tt.expected.b, s, b)
}
})
}
}
func playerActionTestInit(t *testing.T, mapPath string, p *player) (stage, int, error) {
t.Helper()
if err := termbox.Init(); err != nil {
t.Error(err)
}
if err := termbox.Clear(termbox.ColorWhite, termbox.ColorBlack); err != nil {
t.Error(err)
}
t.Cleanup(func() {
termbox.Close()
})
s := stage{
mapPath: mapPath,
hunterBuilder: newEnemyBuilder().defaultHunter(),
ghostBuilder: newEnemyBuilder().defaultGhost(),
}
f, err := static.ReadFile(s.mapPath)
if err != nil {
return s, 0, err
}
b := createBuffer(bytes.NewReader(f))
w := createWindow(b)
if err = w.show(b); err != nil {
return s, b.offset, err
}
s.plot(b, p)
return s, b.offset, nil
}