mirror of
https://github.com/Crocmagnon/gopher-the-number.git
synced 2024-12-22 05:51: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
|
golangci-lint 1.51.2
|
||||||
|
|
2
lib.go
2
lib.go
|
@ -7,6 +7,8 @@ import (
|
||||||
"strconv"
|
"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) {
|
func CheckGuess(guess, random int) (string, bool) {
|
||||||
if guess == random {
|
if guess == random {
|
||||||
return "Good job!", true
|
return "Good job!", true
|
||||||
|
|
117
lib_test.go
117
lib_test.go
|
@ -19,69 +19,59 @@ Then, the app prompts the player again, etc.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func TestCheckGuess(t *testing.T) {
|
func TestCheckGuess(t *testing.T) {
|
||||||
t.Run("guess is lower than random", func(t *testing.T) {
|
tests := []struct {
|
||||||
random := 63
|
name string
|
||||||
guess := 50
|
random int
|
||||||
wantStatus := "Try higher."
|
guess int
|
||||||
|
wantStatus string
|
||||||
assertCheckGuess(t, guess, random, wantStatus, false)
|
}{
|
||||||
})
|
{"guess is lower than random", 50, 34, "Try higher."},
|
||||||
t.Run("guess is higher than random", func(t *testing.T) {
|
{"guess is higher than random", 50, 63, "Nope, lower."},
|
||||||
random := 34
|
{"guess is equal to random", 50, 50, "Good job!"},
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
if won != wantWon {
|
for _, tt := range tests {
|
||||||
t.Errorf("got %v want %v", won, wantWon)
|
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")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoopUntilFound(t *testing.T) {
|
func TestLoopUntilFound(t *testing.T) {
|
||||||
t.Run("found on first try", func(t *testing.T) {
|
tests := []struct {
|
||||||
inputs := []int{37}
|
name string
|
||||||
random := 37
|
inputs []int
|
||||||
want := []string{
|
random int
|
||||||
"your guess: ",
|
want []string
|
||||||
"Good job!",
|
}{
|
||||||
"You got it right in 1 try.",
|
{
|
||||||
}
|
"found on first try",
|
||||||
|
[]int{37},
|
||||||
assertLoopUntilFound(t, inputs, random, want)
|
37,
|
||||||
})
|
[]string{"your guess: ", "Good job!", "You got it right in 1 try."},
|
||||||
t.Run("found on second try", func(t *testing.T) {
|
},
|
||||||
inputs := []int{50, 37}
|
{
|
||||||
random := 37
|
"found on second try",
|
||||||
want := []string{
|
[]int{50, 37},
|
||||||
|
37,
|
||||||
|
[]string{
|
||||||
"your guess: ",
|
"your guess: ",
|
||||||
"Nope, lower.",
|
"Nope, lower.",
|
||||||
"your guess: ",
|
"your guess: ",
|
||||||
"Good job!",
|
"Good job!",
|
||||||
"You got it right in 2 tries.",
|
"You got it right in 2 tries.",
|
||||||
}
|
},
|
||||||
|
},
|
||||||
assertLoopUntilFound(t, inputs, random, want)
|
{
|
||||||
})
|
"found after many tries",
|
||||||
t.Run("found after many tries", func(t *testing.T) {
|
[]int{50, 25, 32, 37},
|
||||||
inputs := []int{50, 25, 32, 37}
|
37,
|
||||||
random := 37
|
[]string{
|
||||||
want := []string{
|
|
||||||
"your guess: ",
|
"your guess: ",
|
||||||
"Nope, lower.",
|
"Nope, lower.",
|
||||||
"your guess: ",
|
"your guess: ",
|
||||||
|
@ -91,30 +81,29 @@ 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.",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
assertLoopUntilFound(t, inputs, random, want)
|
for _, tt := range tests {
|
||||||
})
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
}
|
inputs := convertIntInputsToStrings(tt.inputs)
|
||||||
|
|
||||||
func assertLoopUntilFound(t testing.TB, intInputs []int, random int, outputs []string) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
inputs := convertIntInputsToStrings(intInputs)
|
|
||||||
inputs = append(inputs, "")
|
inputs = append(inputs, "")
|
||||||
inputBuffer := strings.NewReader(strings.Join(inputs, "\n"))
|
inputBuffer := strings.NewReader(strings.Join(inputs, "\n"))
|
||||||
|
|
||||||
outputs = append(outputs, "")
|
wants := append(tt.want, "")
|
||||||
want := strings.Join(outputs, "\n")
|
want := strings.Join(wants, "\n")
|
||||||
|
|
||||||
outputBuffer := bytes.Buffer{}
|
outputBuffer := bytes.Buffer{}
|
||||||
|
|
||||||
LoopUntilFound(&outputBuffer, inputBuffer, random)
|
LoopUntilFound(&outputBuffer, inputBuffer, tt.random)
|
||||||
|
|
||||||
got := outputBuffer.String()
|
got := outputBuffer.String()
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("got %q want %q", got, want)
|
t.Errorf("got %q want %q", got, want)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertIntInputsToStrings(inputs []int) []string {
|
func convertIntInputsToStrings(inputs []int) []string {
|
||||||
|
|
Loading…
Reference in a new issue