/
Game_test.go
127 lines (111 loc) · 3.18 KB
/
Game_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
package main
import (
"testing"
sf "bitbucket.org/krepa098/gosfml2"
)
func TestHandleInput(t *testing.T) {
var g *Game
g = MockNewGame()
pos := g.player.Position
vec := g.player.PosVector
// ey, ex = Expected; cy,cx = Current/Actual coords, ox,oy = Original Coords before moving.
for r := '1'; r <= '9'; r++ {
x, y := pos().X, pos().Y
g.handleInput(r)
assertMove := func(ex, ey int) {
ss := readSettings().SpriteSize
if cx, cy, vx, vy := pos().X, pos().Y, vec().X, vec().Y; cx != ex || cy != ey || vx != float32(ex*ss) || vy != float32(ey*ss) {
t.Errorf("Expected %v,%v;%v,%v; Got: %v,%v;%v,%v", ex, ey, ex*ss, ey*ss, cx, cy, vx, vy)
}
}
switch r {
case '1':
assertMove(x-1, y+1)
case '2':
assertMove(x, y+1)
case '3':
assertMove(x+1, y+1)
case '4':
assertMove(x-1, y)
case '5':
assertMove(x, y)
case '6':
assertMove(x+1, y)
case '7':
assertMove(x-1, y-1)
case '8':
assertMove(x, y-1)
case '9':
assertMove(x+1, y-1)
}
}
}
func TestSettings(t *testing.T) {
if actual := readSettings().resW; actual != 1024 {
t.Errorf("resW = %v, expected: %v", actual, 840)
}
if actual := readSettings().resH; actual != 720 {
t.Errorf("resH = %v, expected: %v", actual, 780)
}
}
func TestStateCommands(t *testing.T) {
g := MockNewGame()
g.handleInput('x')
if state := g.state; state != LOOK {
t.Errorf("Expected: %v, Got: %v", state, LOOK)
}
g.handleInput('2')
ePPos := sf.Vector2i{12, 12}
eCPos := sf.Vector2i{12, 13}
if pPos, cPos := g.player.Position(), g.cursor.Position(); pPos != ePPos || cPos != eCPos {
t.Errorf("PlayerPos: %v,%v. CursorPos: %v,%v. Player should be 12,12 Cursor should be 12,13", pPos.X, pPos.Y, cPos.X, cPos.Y)
}
g.handleInput(27)
if state := g.state; state != PLAY {
t.Errorf("Expected: %v, Got: %v", state, PLAY)
}
}
func TestLookCommand(t *testing.T) {
g := MockNewGame()
orc := NewEntityFromFile("orc", 12, 13, g.area)
g.mobs = append(g.mobs, orc)
g.handleInput('x')
g.handleInput('2')
if actual := g.lookText.GetString(); actual != "orc" {
t.Errorf("Expected: \"orc\", Got: \"%v\"", actual)
}
g.handleInput('8') // Go back up
newOrc := NewEntityFromFile("orc", 12, 13, g.area)
g.mobs = append(g.mobs, newOrc)
if l := len(g.mobs); l != 2 {
t.Errorf("Length of g.mobs should be 2. Got: %v", l)
}
// This tests that more than one ent is described, and also that the text is
//cleared after describing ents in one tile.
g.handleInput('2')
if actual := g.lookText.GetString(); actual != "orc\norc" {
t.Errorf("Expected: \"orc\norc\", Got:\n \"%v\"", actual)
}
g.handleInput('8')
if actual := g.lookText.GetString(); actual != "" {
t.Errorf("Expected empty string. Got:\n \"%v\"", actual)
}
g.items = append(g.items, NewEntityFromFile("potion", 13, 12, g.area))
g.handleInput('6')
if actual := g.lookText.GetString(); actual != "potion" {
t.Errorf("Expected \"potion\". Got:\n \"%v\"", actual)
}
}
func MockNewGame() *Game {
g := new(Game)
g.area = NewArea()
g.player = NewEntity("player", 0, 0, 12, 12, g.area)
g.cursor = NewEntity("cursor", 0, 0, 12, 12, g.area)
g.gameView = sf.NewView()
var err error
g.lookText, err = sf.NewText(Font)
if err != nil {
panic(err)
}
return g
}