/
testing.go
64 lines (52 loc) · 1.51 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
package poker
import (
"fmt"
"io"
"testing"
"time"
)
// StubPlayerStore implements PlayerStore for testing purposes
type StubPlayerStore struct {
Scores map[string]int
WinCalls []string
League []Player
}
// GetPlayerScore returns a score from Scores
func (s *StubPlayerStore) GetPlayerScore(name string) int {
score := s.Scores[name]
return score
}
// RecordWin will record a win to WinCalls
func (s *StubPlayerStore) RecordWin(name string) {
s.WinCalls = append(s.WinCalls, name)
}
// GetLeague returns League
func (s *StubPlayerStore) GetLeague() League {
return s.League
}
// AssertPlayerWin allows you to spy on the store's calls to RecordWin
func AssertPlayerWin(t *testing.T, 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)
}
}
// ScheduledAlert holds information about when an alert is scheduled
type ScheduledAlert struct {
At time.Duration
Amount int
}
func (s ScheduledAlert) String() string {
return fmt.Sprintf("%d chips at %v", s.Amount, s.At)
}
// SpyBlindAlerter allows you to spy on ScheduleAlertAt calls
type SpyBlindAlerter struct {
Alerts []ScheduledAlert
}
// ScheduleAlertAt records alerts that have been scheduled
func (s *SpyBlindAlerter) ScheduleAlertAt(at time.Duration, amount int, to io.Writer) {
s.Alerts = append(s.Alerts, ScheduledAlert{at, amount})
}