Refactor tests

This commit is contained in:
Gabriel Augendre 2023-03-15 18:00:33 +01:00
parent ffc92dc861
commit 1d85612173

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"strconv"
"strings" "strings"
"testing" "testing"
) )
@ -54,40 +55,33 @@ func assertCheckGuess(t testing.TB, guess, random int, wantStatus string, wantWo
func TestLoopUntilFound(t *testing.T) { func TestLoopUntilFound(t *testing.T) {
t.Run("found on first try", func(t *testing.T) { t.Run("found on first try", func(t *testing.T) {
output := bytes.Buffer{} inputs := []int{37}
input := strings.NewReader("37\n")
random := 37 random := 37
LoopUntilFound(&output, input, random) want := []string{
"your guess: ",
got := output.String() "Good job!",
want := "your guess: \nGood job!\nYou got it right in 1 try.\n" "You got it right in 1 try.",
if got != want {
t.Errorf("got %q want %q", got, want)
} }
assertLoopUntilFound(t, inputs, random, want)
}) })
t.Run("found on second try", func(t *testing.T) { t.Run("found on second try", func(t *testing.T) {
output := bytes.Buffer{} inputs := []int{50, 37}
input := strings.NewReader("50\n37\n")
random := 37 random := 37
LoopUntilFound(&output, input, random) want := []string{
"your guess: ",
got := output.String() "Nope, lower.",
want := "your guess: \nNope, lower.\nyour guess: \nGood job!\nYou got it right in 2 tries.\n" "your guess: ",
"Good job!",
if got != want { "You got it right in 2 tries.",
t.Errorf("got %q want %q", got, want)
} }
assertLoopUntilFound(t, inputs, random, want)
}) })
t.Run("found after many tries", func(t *testing.T) { t.Run("found after many tries", func(t *testing.T) {
output := bytes.Buffer{} inputs := []int{50, 25, 32, 37}
inputs := []string{"50", "25", "32", "37", ""}
input := strings.NewReader(strings.Join(inputs, "\n"))
random := 37 random := 37
LoopUntilFound(&output, input, random) want := []string{
got := output.String()
wants := []string{
"your guess: ", "your guess: ",
"Nope, lower.", "Nope, lower.",
"your guess: ", "your guess: ",
@ -97,12 +91,36 @@ func TestLoopUntilFound(t *testing.T) {
"your guess: ", "your guess: ",
"Good job!", "Good job!",
"You got it right in 4 tries.", "You got it right in 4 tries.",
"",
} }
want := strings.Join(wants, "\n")
if got != want { assertLoopUntilFound(t, inputs, random, want)
t.Errorf("got %q want %q", got, want)
}
}) })
} }
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
}