-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
123 lines (99 loc) · 2.49 KB
/
testing.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
package poker
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
type StubPlayerStore struct {
scores map[string]int
winCalls []string
league League
}
func (s *StubPlayerStore) GetPlayerScore(name string) int {
score := s.scores[name]
return score
}
func (s *StubPlayerStore) RecordWin(name string) {
s.winCalls = append(s.winCalls, name)
}
func (s *StubPlayerStore) GetLeague() League {
return s.league
}
func assertContentType(t testing.TB, response *httptest.ResponseRecorder, want string) {
t.Helper()
if response.Result().Header.Get("content-type") != want {
t.Errorf(
"response did not have content-type of %s, got %v",
want,
response.Result().Header,
)
}
}
func getLeagueFromResponse(t *testing.T, body io.Reader) (league []Player) {
t.Helper()
league, err := NewLeague(body)
if err != nil {
t.Fatalf(
"unable to parse response from server %q into slice of player, '%v'",
body,
err,
)
}
return league
}
func assertLeague(t testing.TB, got, want []Player) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d want %d", got, want)
}
}
func newLeagueRequest() *http.Request {
req, _ := http.NewRequest(http.MethodGet, "/league", nil)
return req
}
func newGetScoreRequest(name string) *http.Request {
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/players/%s", name), nil)
return req
}
func newPostWinRequest(name string) *http.Request {
req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("/players/%s", name), nil)
return req
}
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
}
}
func AssertPlayerWin(t testing.TB, store *StubPlayerStore, winner string) {
t.Helper()
if len(store.winCalls) != 1 {
t.Fatalf("got %d calls to RecordWin, want %d", len(store.winCalls), 1)
}
if store.winCalls[0] != winner {
t.Errorf("did not store correct winner, got %q want %q", store.winCalls[0], winner)
}
}
type ScheduleAlert struct {
At time.Duration
Amount int
}
func (s ScheduleAlert) String() string {
return fmt.Sprintf("%d chips at %v", s.Amount, s.At)
}
type SpyBlindAlerter struct {
Alerts []ScheduleAlert
}
func (s *SpyBlindAlerter) ScheduleAlertAt(at time.Duration, amount int) {
s.Alerts = append(s.Alerts, ScheduleAlert{at, amount})
}