-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLI_test.go
156 lines (122 loc) · 3.57 KB
/
CLI_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package poker_test
import (
"bytes"
"io"
poker "learn-go-with-tests"
"strings"
"testing"
"time"
)
var dummyStdOut = &bytes.Buffer{}
var dummySpyAlerter = &poker.SpyBlindAlerter{}
var dummyPlayerStore = &poker.StubPlayerStore{}
type GameSpy struct {
StartCalled bool
StartCalledWith int
BlindAlert []byte
FinishCalled bool
FinishCalledWith string
}
func (g *GameSpy) Start(numberOfPlayers int, out io.Writer) {
g.StartCalled = true
g.StartCalledWith = numberOfPlayers
out.Write(g.BlindAlert)
}
func (g *GameSpy) Finish(winner string) {
g.FinishCalled = true
g.FinishCalledWith = winner
}
func TestCLI(t *testing.T) {
t.Run("start game with 3 players and finish game with 'Chris' as winner", func(t *testing.T) {
game := &GameSpy{}
stdout := &bytes.Buffer{}
in := userSends("3", "Chris wins")
cli := poker.NewCLI(in, stdout, game)
cli.PlayPoker()
assertMessagesSentToUser(t, stdout, poker.PlayerPrompt)
assertGameStartedWith(t, game, 3)
assertFinishCalledWith(t, game, "Chris")
})
t.Run("start game with 8 players and record 'Cleo' as winner", func(t *testing.T) {
game := &GameSpy{}
in := userSends("8", "Cleo wins")
cli := poker.NewCLI(in, dummyStdOut, game)
cli.PlayPoker()
assertGameStartedWith(t, game, 8)
assertFinishCalledWith(t, game, "Cleo")
})
t.Run("it prints an error when a non numeric value is entered and does not start the game", func(t *testing.T) {
game := &GameSpy{}
stdout := &bytes.Buffer{}
in := userSends("Pie")
cli := poker.NewCLI(in, stdout, game)
cli.PlayPoker()
assertGameNotStarted(t, game)
assertMessagesSentToUser(t, stdout, poker.PlayerPrompt, poker.BadPlayerInputErrMsg)
})
t.Run("it prints an error when the winner is declared incorrectly", func(t *testing.T) {
game := &GameSpy{}
stdout := &bytes.Buffer{}
in := userSends("3", "Lloyd is a killer")
cli := poker.NewCLI(in, stdout, game)
cli.PlayPoker()
assertNotFinished(t, game)
assertMessagesSentToUser(t, stdout, poker.PlayerPrompt, poker.BadWinnerInputErrMsg)
})
}
func userSends(messages ...string) io.Reader {
return strings.NewReader(strings.Join(messages, "\n"))
}
func retryUntil(d time.Duration, f func() bool) bool {
deadline := time.Now().Add(d)
for time.Now().Before(deadline) {
if f() {
return true
}
}
return false
}
func assertScheduledAlert(t testing.TB, got, want poker.ScheduledAlert) {
t.Helper()
if got != want {
t.Errorf("got %+v want %+v", got, want)
}
}
func assertMessagesSentToUser(t testing.TB, stdout *bytes.Buffer, messages ...string) {
t.Helper()
got := stdout.String()
want := strings.Join(messages, "")
if got != want {
t.Errorf("got %q sent to stdout but expected %+v", got, messages)
}
}
func assertGameStartedWith(t testing.TB, game *GameSpy, numberOfPlayersWanted int) {
t.Helper()
passed := retryUntil(500*time.Millisecond, func() bool {
return game.StartCalledWith == numberOfPlayersWanted
})
if !passed {
t.Errorf("wanted Start called with %d but got %d", numberOfPlayersWanted, game.StartCalledWith)
}
}
func assertFinishCalledWith(t testing.TB, game *GameSpy, winner string) {
t.Helper()
passed := retryUntil(500*time.Millisecond, func() bool {
return game.FinishCalledWith == winner
})
if !passed {
t.Errorf("wanted Finish called with %q but got %q", winner, game.FinishCalledWith)
}
}
func assertGameNotStarted(t testing.TB, game *GameSpy) {
t.Helper()
if game.StartCalled {
t.Error("game should not have started")
}
}
func assertNotFinished(t testing.TB, game *GameSpy) {
t.Helper()
if game.FinishCalled {
t.Error("game should not have finished")
}
}