@@ -2,36 +2,52 @@ package rockpaperscissors
22
33import (
44 "fmt"
5+ "strings"
56 "testing"
67)
78
89func TestNewGame (t * testing.T ) {
910 tests := []struct {
1011 name string
1112 bestOf int
13+ secretMode bool
1214 wantBestOf int
1315 wantGameOver bool
1416 wantGamesPlayed int
17+ wantSecretMode bool
1518 }{
1619 {
1720 name : "New game with odd number of rounds" ,
1821 bestOf : 3 ,
22+ secretMode : false ,
1923 wantBestOf : 3 ,
2024 wantGameOver : false ,
2125 wantGamesPlayed : 0 ,
26+ wantSecretMode : false ,
2227 },
2328 {
2429 name : "New game with even number of rounds should increment to odd" ,
2530 bestOf : 4 ,
31+ secretMode : false ,
2632 wantBestOf : 5 ,
2733 wantGameOver : false ,
2834 wantGamesPlayed : 0 ,
35+ wantSecretMode : false ,
36+ },
37+ {
38+ name : "New game with secret mode enabled" ,
39+ bestOf : 3 ,
40+ secretMode : true ,
41+ wantBestOf : 3 ,
42+ wantGameOver : false ,
43+ wantGamesPlayed : 0 ,
44+ wantSecretMode : true ,
2945 },
3046 }
3147
3248 for _ , tt := range tests {
3349 t .Run (tt .name , func (t * testing.T ) {
34- game := NewGame (tt .bestOf )
50+ game := NewGame (tt .bestOf , tt . secretMode )
3551 if game .BestOf != tt .wantBestOf {
3652 t .Errorf ("NewGame() BestOf = %v, want %v" , game .BestOf , tt .wantBestOf )
3753 }
@@ -40,6 +56,9 @@ func TestNewGame(t *testing.T) {
4056 }
4157 if game .GamesPlayed != tt .wantGamesPlayed {
4258 t .Errorf ("NewGame() GamesPlayed = %v, want %v" , game .GamesPlayed , tt .wantGamesPlayed )
59+ if game .SecretMode != tt .wantSecretMode {
60+ t .Errorf ("NewGame() SecretMode = %v, want %v" , game .SecretMode , tt .wantSecretMode )
61+ }
4362 }
4463 })
4564 }
@@ -50,69 +69,94 @@ func TestGame_getWinner(t *testing.T) {
5069 name string
5170 playerChoice string
5271 computerChoice string
72+ secretMode bool
5373 want string
5474 }{
5575 {
5676 name : "Player wins with rock vs scissors" ,
5777 playerChoice : "rock" ,
5878 computerChoice : "scissors" ,
79+ secretMode : false ,
5980 want : "player" ,
6081 },
6182 {
6283 name : "Player wins with paper vs rock" ,
6384 playerChoice : "paper" ,
6485 computerChoice : "rock" ,
86+ secretMode : false ,
6587 want : "player" ,
6688 },
6789 {
6890 name : "Player wins with scissors vs paper" ,
6991 playerChoice : "scissors" ,
7092 computerChoice : "paper" ,
93+ secretMode : false ,
7194 want : "player" ,
7295 },
7396 {
7497 name : "Player loses with rock vs paper" ,
7598 playerChoice : "rock" ,
7699 computerChoice : "paper" ,
100+ secretMode : false ,
77101 want : "computer" ,
78102 },
79103 {
80104 name : "Player loses with paper vs scissors" ,
81105 playerChoice : "paper" ,
82106 computerChoice : "scissors" ,
107+ secretMode : false ,
83108 want : "computer" ,
84109 },
85110 {
86111 name : "Player loses with scissors vs rock" ,
87112 playerChoice : "scissors" ,
88113 computerChoice : "rock" ,
114+ secretMode : false ,
89115 want : "computer" ,
90116 },
91117 {
92118 name : "Draw with same choices (rock)" ,
93119 playerChoice : "rock" ,
94120 computerChoice : "rock" ,
121+ secretMode : false ,
95122 want : "draw" ,
96123 },
97124 {
98125 name : "Draw with same choices (paper)" ,
99126 playerChoice : "paper" ,
100127 computerChoice : "paper" ,
128+ secretMode : false ,
101129 want : "draw" ,
102130 },
103131 {
104132 name : "Draw with same choices (scissors)" ,
105133 playerChoice : "scissors" ,
106134 computerChoice : "scissors" ,
135+ secretMode : false ,
107136 want : "draw" ,
108137 },
138+ {
139+ name : "Secret mode - Player wins - rock beats lizard" ,
140+ playerChoice : "rock" ,
141+ computerChoice : "lizard" ,
142+ secretMode : true ,
143+ want : "player" ,
144+ },
145+ {
146+ name : "Secret mode - Computer wins - spock beats rock" ,
147+ playerChoice : "rock" ,
148+ computerChoice : "spock" ,
149+ secretMode : true ,
150+ want : "computer" ,
151+ },
109152 }
110153
111154 for _ , tt := range tests {
112155 t .Run (tt .name , func (t * testing.T ) {
113156 g := & Game {
114157 PlayerChoice : tt .playerChoice ,
115158 ComputerChoice : tt .computerChoice ,
159+ SecretMode : tt .secretMode ,
116160 }
117161 if got := g .getWinner (); got != tt .want {
118162 t .Errorf ("Game.getWinner() = %v, want %v" , got , tt .want )
@@ -126,6 +170,7 @@ func TestGame_Play(t *testing.T) {
126170 name string
127171 bestOf int
128172 moves []string
173+ secretMode bool
129174 wantGameOver bool
130175 wantPlayerScore int
131176 wantComputerScore int
@@ -134,6 +179,7 @@ func TestGame_Play(t *testing.T) {
134179 name : "Game ends when player chooses exit" ,
135180 bestOf : 3 ,
136181 moves : []string {"exit" },
182+ secretMode : false ,
137183 wantGameOver : true ,
138184 wantPlayerScore : 0 ,
139185 wantComputerScore : 0 ,
@@ -142,6 +188,7 @@ func TestGame_Play(t *testing.T) {
142188 name : "Game continues for valid moves" ,
143189 bestOf : 3 ,
144190 moves : []string {"rock" },
191+ secretMode : false ,
145192 wantGameOver : false ,
146193 wantPlayerScore : 0 , // Score will depend on random computer choice
147194 wantComputerScore : 0 , // Score will depend on random computer choice
@@ -150,7 +197,7 @@ func TestGame_Play(t *testing.T) {
150197
151198 for _ , tt := range tests {
152199 t .Run (tt .name , func (t * testing.T ) {
153- g := NewGame (tt .bestOf )
200+ g := NewGame (tt .bestOf , tt . secretMode )
154201 for _ , move := range tt .moves {
155202 g .Play (move )
156203 }
@@ -302,7 +349,7 @@ func TestGame_getGameOverMessage(t *testing.T) {
302349 if got == "" {
303350 t .Error ("getGameOverMessage() returned empty string" )
304351 }
305- if ! contains (got , tt .wantMsgContains ) {
352+ if ! strings . Contains (got , tt .wantMsgContains ) {
306353 t .Errorf ("getGameOverMessage() = %v, want it to contain %v" , got , tt .wantMsgContains )
307354 }
308355 })
@@ -337,43 +384,47 @@ func (m *mockPromptSequence) Select(prompt, defaultValue string, options []strin
337384
338385func TestPlayGame (t * testing.T ) {
339386 tests := []struct {
340- name string
341- prompter Prompter
342- wantGameOver bool
387+ name string
388+ prompter Prompter
389+ secretMode bool
343390 }{
344391 {
345- name : "Complete game sequence" ,
392+ name : "Complete game sequence - standard mode" ,
393+ prompter : & mockPromptSequence {
394+ returns : []int {1 , 0 , 0 , 0 }, // Select 5 rounds, then rock three times
395+ errors : []error {nil , nil , nil , nil },
396+ },
397+ secretMode : false ,
398+ },
399+ {
400+ name : "Complete game sequence - secret mode" ,
346401 prompter : & mockPromptSequence {
347402 returns : []int {1 , 0 , 0 , 0 }, // Select 5 rounds, then rock three times
348403 errors : []error {nil , nil , nil , nil },
349404 },
405+ secretMode : true ,
350406 },
351407 {
352408 name : "Error on rounds selection" ,
353409 prompter : & mockPromptSequence {
354410 returns : []int {0 },
355411 errors : []error {fmt .Errorf ("mock error" )},
356412 },
413+ secretMode : false ,
357414 },
358415 {
359416 name : "Error on move selection" ,
360417 prompter : & mockPromptSequence {
361418 returns : []int {0 , 0 },
362419 errors : []error {nil , fmt .Errorf ("mock error" )},
363420 },
364- },
365- {
366- name : "Invalid round index" ,
367- prompter : & mockPromptSequence {
368- returns : []int {99 , 3 }, // Invalid round index, then exit
369- errors : []error {nil , nil },
370- },
421+ secretMode : false ,
371422 },
372423 }
373424
374425 for _ , tt := range tests {
375426 t .Run (tt .name , func (t * testing.T ) {
376- PlayGame (tt .prompter )
427+ PlayGame (tt .prompter , tt . secretMode )
377428 })
378429 }
379430}
0 commit comments