mirror of
https://github.com/Crocmagnon/gopher-the-number.git
synced 2024-12-21 21:41:50 +01:00
rework using table tests
This commit is contained in:
parent
aef101baef
commit
92ef1ac876
3 changed files with 76 additions and 86 deletions
|
@ -1,2 +1 @@
|
|||
golang 1.20.2
|
||||
golangci-lint 1.51.2
|
||||
|
|
2
lib.go
2
lib.go
|
@ -7,6 +7,8 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
// CheckGuess checks the guess against the random value and returns a status message alongside
|
||||
// a win boolean.
|
||||
func CheckGuess(guess, random int) (string, bool) {
|
||||
if guess == random {
|
||||
return "Good job!", true
|
||||
|
|
115
lib_test.go
115
lib_test.go
|
@ -19,69 +19,59 @@ 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)
|
||||
})
|
||||
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!"},
|
||||
}
|
||||
|
||||
func assertCheckGuess(t testing.TB, guess, random int, wantStatus string, wantWon bool) {
|
||||
t.Helper()
|
||||
status, won := CheckGuess(guess, random)
|
||||
if status != wantStatus {
|
||||
t.Errorf("got %q want %q", status, wantStatus)
|
||||
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 won != wantWon {
|
||||
t.Errorf("got %v want %v", won, wantWon)
|
||||
if tt.guess == tt.random && !won {
|
||||
t.Error("want won")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoopUntilFound(t *testing.T) {
|
||||
t.Run("found on first try", func(t *testing.T) {
|
||||
inputs := []int{37}
|
||||
random := 37
|
||||
want := []string{
|
||||
"your guess: ",
|
||||
"Good job!",
|
||||
"You got it right in 1 try.",
|
||||
}
|
||||
|
||||
assertLoopUntilFound(t, inputs, random, want)
|
||||
})
|
||||
t.Run("found on second try", func(t *testing.T) {
|
||||
inputs := []int{50, 37}
|
||||
random := 37
|
||||
want := []string{
|
||||
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.",
|
||||
}
|
||||
|
||||
assertLoopUntilFound(t, inputs, random, want)
|
||||
})
|
||||
t.Run("found after many tries", func(t *testing.T) {
|
||||
inputs := []int{50, 25, 32, 37}
|
||||
random := 37
|
||||
want := []string{
|
||||
},
|
||||
},
|
||||
{
|
||||
"found after many tries",
|
||||
[]int{50, 25, 32, 37},
|
||||
37,
|
||||
[]string{
|
||||
"your guess: ",
|
||||
"Nope, lower.",
|
||||
"your guess: ",
|
||||
|
@ -91,30 +81,29 @@ func TestLoopUntilFound(t *testing.T) {
|
|||
"your guess: ",
|
||||
"Good job!",
|
||||
"You got it right in 4 tries.",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
assertLoopUntilFound(t, inputs, random, want)
|
||||
})
|
||||
}
|
||||
|
||||
func assertLoopUntilFound(t testing.TB, intInputs []int, random int, outputs []string) {
|
||||
t.Helper()
|
||||
|
||||
inputs := convertIntInputsToStrings(intInputs)
|
||||
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"))
|
||||
|
||||
outputs = append(outputs, "")
|
||||
want := strings.Join(outputs, "\n")
|
||||
wants := append(tt.want, "")
|
||||
want := strings.Join(wants, "\n")
|
||||
|
||||
outputBuffer := bytes.Buffer{}
|
||||
|
||||
LoopUntilFound(&outputBuffer, inputBuffer, random)
|
||||
LoopUntilFound(&outputBuffer, inputBuffer, tt.random)
|
||||
|
||||
got := outputBuffer.String()
|
||||
if got != want {
|
||||
t.Errorf("got %q want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func convertIntInputsToStrings(inputs []int) []string {
|
||||
|
|
Loading…
Reference in a new issue