-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_common_test.go
62 lines (51 loc) · 1.72 KB
/
test_common_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
package main
import (
"fmt"
"net/http/httptest"
"os"
"testing"
"github.com/gin-gonic/gin"
)
//////////////////////////////////////////////////////////////////////////////////////////
// Test Initialization
//////////////////////////////////////////////////////////////////////////////////////////
var tableIndex int = 0
func TestMain(m *testing.M) {
isTestMode = true
initializeGameServer()
resetTestMode()
os.Exit(m.Run())
}
//////////////////////////////////////////////////////////////////////////////////////////
// Test Helper Functions
//////////////////////////////////////////////////////////////////////////////////////////
// Call - used to call api* functions directly
// Greatly reduces extra code around calling different functions
func c(path string, f func(*gin.Context), opt_params ...[]gin.Param) any {
c := createCall(path, opt_params)
f(c)
r, _ := c.Get("testResult")
return r
}
// This just makes it easier to step into a call above with a single step over
func createCall(path string, opt_params [][]gin.Param) *gin.Context {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request = httptest.NewRequest("GET", path, nil)
if len(opt_params) > 0 {
c.Params = opt_params[0]
}
return c
}
// Helper function - creates a uniquely named table with the specified number of bots and human players
func createTestTable(bots int, humans int) (string, []string) {
resetTestMode()
tableIndex++
table := fmt.Sprintf("t%d", tableIndex)
createTable(table, table, bots, true)
table = "&table=" + table
players := make([]string, humans)
for i := 0; i < humans; i++ {
players[i] = fmt.Sprintf("/?player=p%d", i+1) + table
}
return table, players
}