-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_game_test.go
108 lines (99 loc) · 3.59 KB
/
start_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
package main
import "testing"
func TestStartGame_Basic(t *testing.T) {
serverState := NewServer().state
request := StartGameRequest{2, "test_game"}
response := StartGame(&serverState, &request).(*StartGameResponse)
if response.Status == "error" {
t.Errorf("Expected status ok but was error: %v", response.Reason)
}
game, ok := serverState.Games["test_game"]
if !ok {
t.Errorf("The game was not added to the server's state")
}
if game.Name != "test_game" {
t.Errorf("The game's name should be \"test_game\" but is %v", game.Name)
}
if game.NumPlayers != 2 {
t.Errorf("The game should have 2 players but has %v", game.NumPlayers)
}
correctNumCards := 5 * (3 + 2 + 2 + 2 + 1)
if len(game.deck) != correctNumCards {
t.Errorf("The deck should have %v cards but has %v", correctNumCards, len(game.deck))
}
if len(game.cardsByID) != correctNumCards {
t.Errorf("cardsByID should have %v cards but has %v", correctNumCards, len(game.cardsByID))
}
if game.whoseTurn != 0 {
t.Errorf("The turn should start at 0 but is %v", game.whoseTurn)
}
if game.bombs != 3 {
t.Errorf("There should be 3 bombs but there are %v", game.bombs)
}
if game.hints != 8 {
t.Errorf("There should be 8 hints to start with but there are %v", game.hints)
}
}
func TestStartGame_WrongTypeRequest(t *testing.T) {
serverState := NewServer().state
request := JoinGameRequest{"test_game", "test_player"}
response := StartGame(&serverState, &request).(*StartGameResponse)
if response.Status != "error" {
t.Errorf("Expected State=error for wrong request type but was %v", response.Status)
}
if len(serverState.Games) > 0 {
t.Errorf("Expected that there are still no games in the server state")
}
}
func TestStartGame_BadParams(t *testing.T) {
serverState := NewServer().state
request := StartGameRequest{0, "test_game"}
response := StartGame(&serverState, &request).(*StartGameResponse)
if response.Status != "error" {
t.Errorf("Expected State=error when NumPlayers is 0")
}
if len(serverState.Games) > 0 {
t.Errorf("Expected that there are still no games in the server state")
}
request = StartGameRequest{1, "test_game"}
response = StartGame(&serverState, &request).(*StartGameResponse)
if response.Status != "error" {
t.Errorf("Expected State=error when NumPlayers is 1")
}
if len(serverState.Games) > 0 {
t.Errorf("Expected that there are still no games in the server state")
}
request = StartGameRequest{6, "test_game"}
response = StartGame(&serverState, &request).(*StartGameResponse)
if response.Status != "error" {
t.Errorf("Expected State=error when NumPlayers is 6")
}
if len(serverState.Games) > 0 {
t.Errorf("Expected that there are still no games in the server state")
}
request = StartGameRequest{5, ""}
response = StartGame(&serverState, &request).(*StartGameResponse)
if response.Status != "error" {
t.Errorf("Expected State=error when Name is empty")
}
if len(serverState.Games) > 0 {
t.Errorf("Expected that there are still no games in the server state")
}
// Valid game
request = StartGameRequest{5, "test_game"}
response = StartGame(&serverState, &request).(*StartGameResponse)
if response.Status == "error" {
t.Errorf("Expected %v to succeed", request)
}
if len(serverState.Games) != 1 {
t.Errorf("Expected that there is now one game in the server state")
}
request = StartGameRequest{3, "test_game"}
response = StartGame(&serverState, &request).(*StartGameResponse)
if response.Status != "error" {
t.Errorf("Expected State=error when adding a duplicate game")
}
if len(serverState.Games) > 1 {
t.Errorf("Expected that there is still only one game in the server state")
}
}