walk: refactor test to table test

This commit is contained in:
Muyao CHEN 2024-09-20 19:56:28 +02:00
parent b332088638
commit 766d7449e8

View File

@ -1,27 +1,35 @@
package walk
import (
"reflect"
"testing"
)
func TestWalk(t *testing.T) {
t.Run("walk function test", func(t *testing.T) {
expected := "Chris"
var got []string
x := struct {
cases := []struct {
Name string
}{expected}
Input interface{}
ExpectedCalls []string
}{
{
"struct with one string field",
struct {
Name string
}{"Chris"},
[]string{"Chris"},
},
}
Walk(x, func(input string) {
for _, test := range cases {
t.Run(test.Name, func(t *testing.T) {
var got []string
Walk(test.Input, func(input string) {
got = append(got, input)
})
if len(got) != 1 {
t.Errorf("wrong number of function calls, got %d want %d", len(got), 1)
}
if got[0] != expected {
t.Errorf("got %q want %q", got[0], expected)
if !reflect.DeepEqual(got, test.ExpectedCalls) {
t.Errorf("got %v want %v", got, test.ExpectedCalls)
}
})
}
}