-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLI_test.go
141 lines (109 loc) · 3.02 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
package poker
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"time"
)
var (
dummyGame = &GameSpy{}
dummyBlindAlerter = &SpyBlindAlerter{}
dummyPlayerStore = &StubPlayerStore{}
dummyStdIn = &bytes.Buffer{}
dummyStdOut = &bytes.Buffer{}
)
type ScheduledAlert struct {
At time.Duration
Amount int
}
func (s ScheduledAlert) String() string {
return fmt.Sprintf("%d chips at %v", s.Amount, s.At)
}
type SpyBlindAlerter struct {
alerts []ScheduledAlert
}
func (s *SpyBlindAlerter) ScheduleAlertAt(at time.Duration, amount int, to io.Writer) {
s.alerts = append(s.alerts, ScheduledAlert{at, amount})
}
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 := NewCLI(in, stdout, game)
cli.PlayPoker()
assertMessagesSentToUser(t, stdout, PlayerPrompt)
assertGameStartedWith(t, game, 3)
assertFinishCalledWith(t, game, "Chris")
})
t.Run("Start game with 8 players and record 'Cleo' as the winner", func(t *testing.T) {
game := &GameSpy{}
in := userSends("8", "Cleo wins")
cli := 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("pies")
cli := NewCLI(in, stdout, game)
cli.PlayPoker()
assertGameNotStarted(t, game)
assertMessagesSentToUser(t, stdout, PlayerPrompt, BadPlayerInputErrMsg)
})
t.Run("It prints error when given the wrong winner string", func(t *testing.T) {
game := &GameSpy{}
stdout := &bytes.Buffer{}
in := userSends("9", "Lloyd is a killer")
cli := NewCLI(in, stdout, game)
err := cli.PlayPoker()
if err == nil {
t.Error("Expected to run into an error")
}
})
}
func userSends(vars ...string) io.Reader {
var resString string
for i := range vars {
resString += (vars[i] + "\n")
}
return strings.NewReader(resString)
}
func assertGameStartedWith(t *testing.T, game *GameSpy, want int) {
t.Helper()
got := game.StartedWith
if got != want {
t.Errorf("got %d, want %d", got, want)
}
}
func assertGameNotStarted(t *testing.T, game *GameSpy) {
t.Helper()
if game.StartCalled {
t.Errorf("Game was called when it shouldn't have been.")
}
}
func assertScheduledAlert(t *testing.T, got, want ScheduledAlert) {
t.Helper()
if got.Amount != want.Amount {
t.Errorf("got amount %d, want %d", got.Amount, want.Amount)
}
if got.At != want.At {
t.Errorf("got scheduled time of %v, want %v", got.At, want.At)
}
}
func checkSchedulingCases(cases []ScheduledAlert, t *testing.T, gots []ScheduledAlert) {
t.Helper()
for i, want := range cases {
t.Run(fmt.Sprint(want), func(t *testing.T) {
if len(gots) <= i {
t.Fatalf("alert %d want not scheduled %v", i, gots)
}
got := gots[i]
assertScheduledAlert(t, got, want)
})
}
}