This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl_test.go
133 lines (115 loc) · 3.05 KB
/
impl_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
package splenda
import (
"os"
"os/exec"
"testing"
)
func TestTwoPlayers(t *testing.T) {
url := os.Getenv("DATABASE_URL")
defer cleanup(url)
impl, err := setup(url)
if err != nil {
t.Fatal(err)
}
id, err := impl.NewGame("user1", []string{"user1", "user2"})
if err != nil {
t.Fatal(err)
}
game, err := impl.GetGame(id, "user1", "0")
if err != nil {
t.Fatal(err)
}
assertGameState(t, game, id, play, "user2")
assertCoins(t, game, map[string]int{red: 4, green: 4, blue: 4, black: 4, white: 4, wild: 5})
assertNobles(t, game, []string{"macchiavelli", "charles_v", "catherine_of_medici"})
assertCards(t, game.Table.Cards[2], []string{"3_7_3_0", "3_6_23_1", "3_7_3_1", "3_5_33_4"})
assertCards(t, game.Table.Cards[1], []string{"2_6_2", "2_3_22_1", "2_23_2_4", "2_5_3_2"})
assertCards(t, game.Table.Cards[0], []string{"1_4_3", "1_22_1", "1_22_0", "1_2_31_3"})
// TODO: Make some moves.
}
func assertGameState(t *testing.T, game *Game, id string, state string, current string) {
if game.ID != id {
t.Errorf("bad ID: expected %v, got %v", id, game.ID)
}
if game.State != state {
t.Errorf("bad state: expected %v, got %v", state, game.State)
}
if game.Current != current {
t.Errorf("bad current player: expected %v, got %v", current, game.Current)
}
}
func assertCoins(t *testing.T, game *Game, coins map[string]int) {
if len(coins) != len(game.Table.Coins) {
t.Errorf("bad table coins: expected %v, got %v", coins, game.Table.Coins)
return
}
for k, v := range coins {
if game.Table.Coins[k] != v {
t.Errorf("bad table coins: expected %v, got %v", coins, game.Table.Coins)
return
}
}
}
func assertNobles(t *testing.T, game *Game, nobles []string) {
actual := []string{}
for _, n := range game.Table.Nobles {
actual = append(actual, n.ID)
}
if len(nobles) != len(actual) {
t.Errorf("bad table nobles: expected %v, got %v", nobles, actual)
return
}
for i, n := range actual {
if n != nobles[i] {
t.Errorf("bad table nobles: expected %v, got %v", nobles, actual)
return
}
}
}
func assertCards(t *testing.T, cards []*Card, expected []string) {
actual := []string{}
for _, c := range cards {
actual = append(actual, c.ID)
}
if len(expected) != len(actual) {
t.Errorf("bad cards: expected %v, got %v", expected, actual)
return
}
for i, a := range actual {
if a != expected[i] {
t.Errorf("bad cards: expected %v, got %v", expected, actual)
return
}
}
}
func setup(url string) (*Impl, error) {
if url == "" {
cleanup(url)
cmd := exec.Command("createdb", "splenda-test")
cmd.Run()
url = "postgres://localhost/splenda-test"
}
url += "?sslmode=disable"
db := NewDB(url)
if err := db.ApplySchema(); err != nil {
return nil, err
}
auth, err := NewAuth(db, "aaa=")
if err != nil {
return nil, err
}
if err := auth.NewUser("user1", "abc"); err != nil {
return nil, err
}
if err := auth.NewUser("user2", "def"); err != nil {
return nil, err
}
impl := NewImplSeed(db, 1)
return impl, nil
}
func cleanup(url string) {
if url == "" {
cmd := exec.Command("dropdb", "splenda-test")
cmd.Run()
}
}