-
Notifications
You must be signed in to change notification settings - Fork 4
/
message_test.go
95 lines (74 loc) · 2.27 KB
/
message_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
package main
import (
"net"
"testing"
)
// TODO Move sections to game package
func TestParse(t *testing.T) {
serverConn, clientConn := net.Pipe()
client := newClient(serverConn)
// Valid events
for _, msg := range []message{
{Event: "JOIN "},
{Event: " Move "},
{Event: " dig"},
{Event: "list"},
} {
msg.parse(client)
receiveMsg(t, clientConn, message{
"error", []byte(`"unexpected end of JSON input"`),
})
}
// TODO Test the list command
// Invalid event
new(message).parse(client)
receiveMsg(t, clientConn, message{"error", []byte(`"invalid event"`)})
}
func TestParseJoin(t *testing.T) {
conn, _ := net.Pipe()
spectator, runner := newClient(conn), newClient(conn)
if _, ok := rooms.Load("test"); ok {
t.Error("room already exists")
return
}
// New room
parseJoin([]byte(`{"room": "test", "role": 42}`), spectator)
if r, ok := rooms.Load("test"); !ok {
t.Error("room doesn't exist")
return
} else if _, ok := r.(*room).clients[spectator]; !ok {
t.Error("spectator not in room")
}
// Existing room
parseJoin([]byte(`{"name": "runner", "room": "test", "level": 1}`), runner)
if r, ok := rooms.Load("test"); !ok {
t.Error("room doesn't exist")
return
} else if _, ok := r.(*room).clients[runner]; !ok {
t.Error("runner not in room")
}
}
// TODO Move to game package
func TestParseMove(t *testing.T) {
serverConn, clientConn := net.Pipe()
spectator := newClient(serverConn)
parseMove([]byte(`{"direction": 0, "room": ""}`), spectator)
receiveMsg(t, clientConn, message{"error", []byte(`"not in a room"`)})
newRoom("test").clients[spectator] = nil
parseMove([]byte(`{"direction": 0, "room": ""}`), spectator)
receiveMsg(t, clientConn, message{"error", []byte(`"game not started"`)})
// TODO Not a player
// TODO Verify player action
}
// TODO Move to game package
func TestParseDig(t *testing.T) {
serverConn, clientConn := net.Pipe()
spectator := newClient(serverConn)
parseDig([]byte(`{"direction": 0, "room": ""}`), spectator)
receiveMsg(t, clientConn, message{"error", []byte(`"not in a room"`)})
newRoom("test").clients[spectator] = nil
parseDig([]byte(`{"direction": 0, "room": ""}`), spectator)
receiveMsg(t, clientConn, message{"error", []byte(`"game not started"`)})
// TODO Not a runner
// TODO Verify runner action
}