-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_test.go
More file actions
75 lines (53 loc) · 1.68 KB
/
snake_test.go
File metadata and controls
75 lines (53 loc) · 1.68 KB
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
package main
import (
"testing"
)
func Test_Snake(t *testing.T) {
snake := NewSnake(1, 1, 'x', &defaultController{})
snake.ChangeDirection(Down)
if got := snake.controller.Get(); got != Down {
t.Errorf("got = %v, want = %v", got, Down)
}
// TODO: test result
snake.Move(0, 5, 0, 5)
snake.CoordinateList()
snake.IsBumpSelf()
}
func Test_Controller(t *testing.T) {
ctl := &defaultController{last: Left, accepted: []Direction{Right}}
assertGetEqual(t, ctl, Left, "init state 1")
assertGetEqual(t, ctl, Left, "init state 2")
ctl.Change(Down)
ctl.Change(Down)
ctl.Change(Down)
assertGetEqual(t, ctl, Down, "turn down")
assertGetEqual(t, ctl, Down, "no changing")
ctl.Change(Up)
assertGetEqual(t, ctl, Down, "negtive direction")
ctl.Change(Right)
assertGetEqual(t, ctl, Right, "normal change")
assertGetEqual(t, ctl, Right, "normal change")
assertGetEqual(t, ctl, Right, "normal change")
assertGetEqual(t, ctl, Right, "normal change")
assertGetEqual(t, ctl, Right, "after normal change")
ctl.Change(Left)
assertGetEqual(t, ctl, Right, "negtive change after normal change")
// irregular turn back
ctl.Change(Up)
ctl.Change(Down)
ctl.Change(Down)
ctl.Change(Left)
ctl.Change(Left)
assertGetEqual(t, ctl, Down, "irregular turn back step 1")
assertGetEqual(t, ctl, Left, "irregular turn back step 2")
assertGetEqual(t, ctl, Left, "irregular turn back step 3")
ctl.Change(Down)
ctl.Change(Left)
assertGetEqual(t, ctl, Down, "change track step 1")
assertGetEqual(t, ctl, Left, "change track step 2")
}
func assertGetEqual(t *testing.T, ctl Controller, d Direction, cas string) {
if got := ctl.Get(); got != d {
t.Errorf("case: %s got = %v, want = %v", cas, got, d)
}
}