gopher-the-number/lib_test.go

127 lines
2.8 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) {
t.Run("guess is lower than random", func(t *testing.T) {
random := 63
guess := 50
wantStatus := "Try higher."
assertCheckGuess(t, guess, random, wantStatus, false)
})
t.Run("guess is higher than random", func(t *testing.T) {
random := 34
guess := 50
wantStatus := "Nope, lower."
assertCheckGuess(t, guess, random, wantStatus, false)
})
t.Run("guess is equal to random", func(t *testing.T) {
random := 50
guess := 50
wantStatus := "Good job!"
assertCheckGuess(t, guess, random, wantStatus, true)
})
}
2023-03-15 17:13:24 +01:00
func assertCheckGuess(t testing.TB, guess, random int, wantStatus string, wantWon bool) {
t.Helper()
2023-03-15 17:09:26 +01:00
status, won := CheckGuess(guess, random)
if status != wantStatus {
t.Errorf("got %q want %q", status, wantStatus)
}
if won != wantWon {
t.Errorf("got %v want %v", won, wantWon)
}
}
func TestLoopUntilFound(t *testing.T) {
t.Run("found on first try", func(t *testing.T) {
2023-03-15 18:00:33 +01:00
inputs := []int{37}
random := 37
2023-03-15 18:00:33 +01:00
want := []string{
"your guess: ",
"Good job!",
"You got it right in 1 try.",
}
2023-03-15 18:00:33 +01:00
assertLoopUntilFound(t, inputs, random, want)
})
t.Run("found on second try", func(t *testing.T) {
2023-03-15 18:00:33 +01:00
inputs := []int{50, 37}
random := 37
2023-03-15 18:00:33 +01:00
want := []string{
"your guess: ",
"Nope, lower.",
"your guess: ",
"Good job!",
"You got it right in 2 tries.",
}
2023-03-15 18:00:33 +01:00
assertLoopUntilFound(t, inputs, random, want)
})
t.Run("found after many tries", func(t *testing.T) {
2023-03-15 18:00:33 +01:00
inputs := []int{50, 25, 32, 37}
random := 37
2023-03-15 18:00:33 +01:00
want := []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
assertLoopUntilFound(t, inputs, random, want)
})
}
2023-03-15 18:00:33 +01:00
func assertLoopUntilFound(t testing.TB, intInputs []int, random int, outputs []string) {
t.Helper()
inputs := convertIntInputsToStrings(intInputs)
inputs = append(inputs, "")
inputBuffer := strings.NewReader(strings.Join(inputs, "\n"))
outputs = append(outputs, "")
want := strings.Join(outputs, "\n")
outputBuffer := bytes.Buffer{}
LoopUntilFound(&outputBuffer, inputBuffer, random)
got := outputBuffer.String()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
func convertIntInputsToStrings(inputs []int) []string {
inputStrings := make([]string, len(inputs)+1)
for i, input := range inputs {
inputStrings[i] = strconv.Itoa(input)
}
return inputStrings
}