-
Notifications
You must be signed in to change notification settings - Fork 0
/
arena_test.go
146 lines (110 loc) · 3.03 KB
/
arena_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
package snake
import "testing"
var pointsDouble = make(chan int)
func newDoubleArenaWithFoodFinder(h, w int, f func(*arena, coord) bool) *arena {
a := newDoubleArena(h, w)
a.hasFood = f
return a
}
func newDoubleArena(h, w int) *arena {
s := newSnake(RIGHT, []coord{
coord{x: 1, y: 0},
coord{x: 1, y: 1},
coord{x: 1, y: 2},
coord{x: 1, y: 3},
coord{x: 1, y: 4},
})
return newArena(s, pointsDouble, h, w)
}
func TestArenaHaveFoodPlaced(t *testing.T) {
if a := newDoubleArena(20, 20); a.food == nil {
t.Fatal("Arena expected to have food placed")
}
}
func TestMoveSnakeOutOfArenaHeightLimit(t *testing.T) {
a := newDoubleArena(4, 10)
a.snake.changeDirection(UP)
if err := a.moveSnake(); err == nil || err.Error() != "Died" {
t.Fatal("Expected Snake to die when moving outside the Arena height limits")
}
}
func TestMoveSnakeOutOfArenaWidthLimit(t *testing.T) {
a := newDoubleArena(10, 1)
a.snake.changeDirection(LEFT)
if err := a.moveSnake(); err == nil || err.Error() != "Died" {
t.Fatal("Expected Snake to die when moving outside the Arena height limits")
}
}
func TestPlaceNewFoodWhenEatFood(t *testing.T) {
a := newDoubleArenaWithFoodFinder(10, 10, func(*arena, coord) bool {
return true
})
f := a.food
a.moveSnake()
if a.food.x == f.x && a.food.y == f.y {
t.Fatal("Expected new food to have been placed on Arena")
}
}
func TestIncreaseSnakeLengthWhenEatFood(t *testing.T) {
a := newDoubleArenaWithFoodFinder(10, 10, func(*arena, coord) bool {
return true
})
l := a.snake.length
a.moveSnake()
if a.snake.length != l+1 {
t.Fatal("Expected Snake to have grown")
}
}
func TestAddPointsWhenEatFood(t *testing.T) {
a := newDoubleArenaWithFoodFinder(10, 10, func(*arena, coord) bool {
return true
})
if p, ok := <-pointsDouble; ok && p != a.food.points {
t.Fatalf("Value %d was expected but got %d", a.food.points, p)
}
a.moveSnake()
}
func TestDoesNotAddPointsWhenFoodNotFound(t *testing.T) {
a := newDoubleArenaWithFoodFinder(10, 10, func(*arena, coord) bool {
return false
})
select {
case p, _ := <-pointsChan:
t.Fatalf("No point was expected to be received but received %d", p)
default:
close(pointsChan)
}
a.moveSnake()
}
func TestDoesNotPlaceNewFoodWhenFoodNotFound(t *testing.T) {
a := newDoubleArenaWithFoodFinder(10, 10, func(*arena, coord) bool {
return false
})
f := a.food
a.moveSnake()
if a.food.x != f.x || a.food.y != f.y {
t.Fatal("Food in Arena expected not to have changed")
}
}
func TestDoesNotIncreaseSnakeLengthWhenFoodNotFound(t *testing.T) {
a := newDoubleArenaWithFoodFinder(10, 10, func(*arena, coord) bool {
return false
})
l := a.snake.length
a.moveSnake()
if a.snake.length != l {
t.Fatal("Expected Snake not to have grown")
}
}
func TestHasFood(t *testing.T) {
a := newDoubleArena(20, 20)
if !hasFood(a, coord{x: a.food.x, y: a.food.y}) {
t.Fatal("Food expected to be found")
}
}
func TestHasNotFood(t *testing.T) {
a := newDoubleArena(20, 20)
if hasFood(a, coord{x: a.food.x - 1, y: a.food.y}) {
t.Fatal("No food expected to be found")
}
}