gopher-the-number/lib_test.go

116 lines
2.4 KiB
Go
Raw Normal View History

2023-03-15 17:11:00 +01:00
package main
2023-03-15 17:09:26 +01:00
import (
"bytes"
2023-03-15 18:00:33 +01:00
"strconv"
"strings"
2023-03-15 17:09:26 +01:00
"testing"
)
/*
Requirements:
The app selects a random number between 1 and 100
The app then asks the player to guess the number.
If the guess is correct, then the player wins. The app displays
the number of guesses it took and exits.
If the guess is incorrect, the app prints whether the selected number
is higher or lower than the guess.
Then, the app prompts the player again, etc.
*/
func TestCheckGuess(t *testing.T) {
2024-02-17 14:31:47 +01:00
tests := []struct {
name string
random int
guess int
wantStatus string
}{
{"guess is lower than random", 50, 34, "Try higher."},
{"guess is higher than random", 50, 63, "Nope, lower."},
{"guess is equal to random", 50, 50, "Good job!"},
2023-03-15 17:09:26 +01:00
}
2024-02-17 14:31:47 +01:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
status, won := CheckGuess(tt.guess, tt.random)
if status != tt.wantStatus {
t.Errorf("got %q want %q", status, tt.wantStatus)
}
if tt.guess == tt.random && !won {
t.Error("want won")
}
})
2023-03-15 17:09:26 +01:00
}
}
func TestLoopUntilFound(t *testing.T) {
2024-02-17 14:31:47 +01:00
tests := []struct {
name string
inputs []int
random int
want []string
}{
{
"found on first try",
[]int{37},
37,
[]string{"your guess: ", "Good job!", "You got it right in 1 try."},
},
{
"found on second try",
[]int{50, 37},
37,
[]string{
"your guess: ",
"Nope, lower.",
"your guess: ",
"Good job!",
"You got it right in 2 tries.",
},
},
{
"found after many tries",
[]int{50, 25, 32, 37},
37,
[]string{
"your guess: ",
"Nope, lower.",
"your guess: ",
"Try higher.",
"your guess: ",
"Try higher.",
"your guess: ",
"Good job!",
"You got it right in 4 tries.",
},
},
}
2023-03-15 18:00:33 +01:00
2024-02-17 14:31:47 +01:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inputs := convertIntInputsToStrings(tt.inputs)
inputs = append(inputs, "")
inputBuffer := strings.NewReader(strings.Join(inputs, "\n"))
2023-03-15 18:00:33 +01:00
2024-02-17 14:31:47 +01:00
wants := append(tt.want, "")
want := strings.Join(wants, "\n")
2023-03-15 18:00:33 +01:00
2024-02-17 14:31:47 +01:00
outputBuffer := bytes.Buffer{}
2023-03-15 18:00:33 +01:00
2024-02-17 14:31:47 +01:00
LoopUntilFound(&outputBuffer, inputBuffer, tt.random)
2023-03-15 18:00:33 +01:00
2024-02-17 14:31:47 +01:00
got := outputBuffer.String()
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
2023-03-15 18:00:33 +01:00
}
}
func convertIntInputsToStrings(inputs []int) []string {
inputStrings := make([]string, len(inputs)+1)
for i, input := range inputs {
inputStrings[i] = strconv.Itoa(input)
}
return inputStrings
}