@@ -2,6 +2,7 @@ package cointoss
22
33import (
44 "errors"
5+ "strings"
56 "testing"
67)
78
@@ -41,7 +42,7 @@ func TestTossCoin(t *testing.T) {
4142 }
4243}
4344
44- func TestGetNextGuessWithPrompter (t * testing.T ) {
45+ func TestGetPlayerGuess (t * testing.T ) {
4546 tests := []struct {
4647 name string
4748 selectAnswer int
@@ -77,13 +78,6 @@ func TestGetNextGuessWithPrompter(t *testing.T) {
7778 expectedGuess : "" ,
7879 expectedCont : false ,
7980 },
80- {
81- name : "error during selection prints error message" ,
82- selectAnswer : 0 ,
83- selectError : errors .New ("test error" ),
84- expectedGuess : "" ,
85- expectedCont : false ,
86- },
8781 }
8882
8983 for _ , tt := range tests {
@@ -92,15 +86,143 @@ func TestGetNextGuessWithPrompter(t *testing.T) {
9286 selectAnswer : tt .selectAnswer ,
9387 selectError : tt .selectError ,
9488 }
95-
96- guess , cont := GetNextGuessWithPrompter (mockP )
97-
89+ guess , cont := GetPlayerGuess (mockP )
9890 if guess != tt .expectedGuess {
99- t .Errorf ("GetNextGuessWithPrompter () guess = %v, want %v" , guess , tt .expectedGuess )
91+ t .Errorf ("GetPlayerGuess () guess = %v, want %v" , guess , tt .expectedGuess )
10092 }
10193 if cont != tt .expectedCont {
102- t .Errorf ("GetNextGuessWithPrompter() cont = %v, want %v" , cont , tt .expectedCont )
94+ t .Errorf ("GetPlayerGuess() cont = %v, want %v" , cont , tt .expectedCont )
95+ }
96+ })
97+ }
98+ }
99+
100+ func TestGame_Play (t * testing.T ) {
101+ game := NewGame ()
102+
103+ // Test initial state
104+ if game .IsOver {
105+ t .Error ("New game should not be over" )
106+ }
107+
108+ // Play a round
109+ game .Play ("heads" )
110+
111+ // Test that game state is updated
112+ if ! game .IsOver {
113+ t .Error ("Game should be over after playing" )
114+ }
115+ if game .PlayerGuess != "heads" {
116+ t .Errorf ("PlayerGuess = %v, want heads" , game .PlayerGuess )
117+ }
118+ if game .Result != "heads" && game .Result != "tails" {
119+ t .Errorf ("Result = %v, want either heads or tails" , game .Result )
120+ }
121+ }
122+
123+ func TestGame_GetResult (t * testing.T ) {
124+ tests := []struct {
125+ name string
126+ playerGuess string
127+ result string
128+ wantWin bool
129+ }{
130+ {
131+ name : "player wins with heads" ,
132+ playerGuess : "heads" ,
133+ result : "heads" ,
134+ wantWin : true ,
135+ },
136+ {
137+ name : "player wins with tails" ,
138+ playerGuess : "tails" ,
139+ result : "tails" ,
140+ wantWin : true ,
141+ },
142+ {
143+ name : "player loses with heads" ,
144+ playerGuess : "heads" ,
145+ result : "tails" ,
146+ wantWin : false ,
147+ },
148+ {
149+ name : "player loses with tails" ,
150+ playerGuess : "tails" ,
151+ result : "heads" ,
152+ wantWin : false ,
153+ },
154+ }
155+
156+ for _ , tt := range tests {
157+ t .Run (tt .name , func (t * testing.T ) {
158+ game := & Game {
159+ PlayerGuess : tt .playerGuess ,
160+ Result : tt .result ,
161+ IsOver : true ,
103162 }
163+ got := game .GetResult ()
164+ if tt .wantWin && ! contains (got , "You win!" ) {
165+ t .Errorf ("GetResult() = %v, want win message" , got )
166+ }
167+ if ! tt .wantWin && ! contains (got , "You lose!" ) {
168+ t .Errorf ("GetResult() = %v, want lose message" , got )
169+ }
170+ })
171+ }
172+ }
173+
174+ // Helper function to check if a string contains a substring
175+ func contains (s , substr string ) bool {
176+ return strings .Contains (s , substr )
177+ }
178+
179+ func TestPlayGame (t * testing.T ) {
180+ tests := []struct {
181+ name string
182+ selectAnswer int
183+ selectError error
184+ initialGuess string
185+ results []string // sequence of coin flip results to test
186+ }{
187+ {
188+ name : "win first round then quit" ,
189+ selectAnswer : 2 , // quit
190+ initialGuess : "heads" ,
191+ results : []string {"heads" },
192+ },
193+ {
194+ name : "lose first round" ,
195+ initialGuess : "heads" ,
196+ results : []string {"tails" },
197+ },
198+ {
199+ name : "win twice then lose" ,
200+ selectAnswer : 0 , // heads
201+ initialGuess : "heads" ,
202+ results : []string {"heads" , "heads" , "tails" },
203+ },
204+ }
205+
206+ for _ , tt := range tests {
207+ t .Run (tt .name , func (t * testing.T ) {
208+ mockP := & mockPrompter {
209+ selectAnswer : tt .selectAnswer ,
210+ selectError : tt .selectError ,
211+ }
212+
213+ // Override TossCoin for deterministic testing
214+ resultIndex := 0
215+ oldTossCoin := TossCoin
216+ TossCoin = func () string {
217+ result := tt .results [resultIndex ]
218+ if resultIndex < len (tt .results )- 1 {
219+ resultIndex ++
220+ }
221+ return result
222+ }
223+ defer func () { TossCoin = oldTossCoin }()
224+
225+ PlayGame (mockP , tt .initialGuess )
104226 })
105227 }
106228}
0 commit comments