add starting tests

This commit is contained in:
stryan 2022-03-18 14:20:54 -04:00
parent 7558da5c8e
commit 48ea6e62a1
2 changed files with 132 additions and 7 deletions

17
api.go
View File

@ -82,14 +82,17 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
log.Println("sending game state")
rotatedBoard := s.getBoard(p)
i := 0
temp := rotatedBoard[0]
for ; i < len(rotatedBoard)-1; i++ {
rotatedBoard[i] = rotatedBoard[i+1]
}
rotate := req.Header.Get("Rotate")
if rotate == "true" {
i := 0
temp := rotatedBoard[0]
for ; i < len(rotatedBoard)-1; i++ {
rotatedBoard[i] = rotatedBoard[i+1]
}
rotatedBoard[i] = temp
respondWithJSON(res, http.StatusOK, rotatedBoard)
rotatedBoard[i] = temp
}
respondWithJSON(res, http.StatusOK, gameResp{rotatedBoard})
return
}

122
api_test.go Normal file
View File

@ -0,0 +1,122 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gorilla/mux"
)
func TestNewAPI(t *testing.T) {
a := NewAPI()
if len(a.games) != 0 {
t.Fatalf("games list not empty")
}
if a.nextInt != 1 {
t.Fatalf("nextInt somehow already implemented")
}
}
func dummyGame(a *API) int {
i := a.nextInt
a.games[i] = NewSession()
a.games[i].redPlayer.Ready = true
a.games[i].bluePlayer.Ready = true
a.games[i].simulator.Setup()
initDummy(a.games[i].simulator)
return i
}
func TestNewGame(t *testing.T) {
t.Parallel()
a := NewAPI()
var tests = []struct {
gid int
pid string
}{
{1, "red"},
{1, "blue"},
{2, "red"},
}
for i, tt := range tests {
tname := fmt.Sprintf("/game %v", i)
t.Run(tname, func(t *testing.T) {
r, _ := http.NewRequest("POST", "/game", nil)
w := httptest.NewRecorder()
a.NewGame(w, r)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Fatal("failed to create new game")
}
_, ok := a.games[tt.gid]
if !ok {
t.Fatalf("API thinks it created a game but it didn't")
}
var respStruct newGameResp
err := json.NewDecoder(resp.Body).Decode(&respStruct)
if err != nil {
t.Errorf("/game returned bad response body: %v", err)
}
if respStruct.GameID != tt.gid {
t.Errorf("Expected game %v, got %v", tt.gid, respStruct.GameID)
}
if respStruct.PlayerID != tt.pid {
t.Errorf("wrong playerID returned")
}
})
}
}
func TestGetGame(t *testing.T) {
t.Parallel()
a := NewAPI()
gid := dummyGame(a)
var tests = []struct {
pid string
code int
}{
{"red", http.StatusOK},
{"blue", http.StatusOK},
{"green", http.StatusBadRequest},
}
for _, tt := range tests {
tname := fmt.Sprintf("/game from player %v", tt.pid)
t.Run(tname, func(t *testing.T) {
r, _ := http.NewRequest("GET", "/game", nil)
w := httptest.NewRecorder()
r = mux.SetURLVars(r, map[string]string{"id": strconv.Itoa(gid)})
r.Header.Add("Player-id", tt.pid)
r.Header.Add("Rotate", "false")
a.GetGame(w, r)
resp := w.Result()
if resp.StatusCode != tt.code {
t.Fatalf("failed to get game: %v", resp.Status)
}
if resp.StatusCode == http.StatusOK {
var respStruct gameResp
err := json.NewDecoder(resp.Body).Decode(&respStruct)
if err != nil {
t.Fatalf("/game returned bad response body: %v", err)
}
if len(respStruct.GameBoard) == 0 {
t.Errorf("bad game board returned")
}
for j := range respStruct.GameBoard {
for i, vt := range respStruct.GameBoard[j] {
curr, err := a.games[gid].simulator.Board.GetPiece(j, i)
if err != nil {
t.Fatalf("Strange board position: %v", err)
}
if curr != nil && !vt.Hidden && curr.Owner.String() != tt.pid && curr.Hidden {
t.Errorf("/game returned a piece that should be hidden but isn't")
}
}
}
}
})
}
}