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 package walk
import ( import (
"reflect"
"testing" "testing"
) )
func TestWalk(t *testing.T) { func TestWalk(t *testing.T) {
t.Run("walk function test", func(t *testing.T) { cases := []struct {
expected := "Chris"
var got []string
x := struct {
Name string 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) got = append(got, input)
}) })
if len(got) != 1 { if !reflect.DeepEqual(got, test.ExpectedCalls) {
t.Errorf("wrong number of function calls, got %d want %d", len(got), 1) t.Errorf("got %v want %v", got, test.ExpectedCalls)
}
if got[0] != expected {
t.Errorf("got %q want %q", got[0], expected)
} }
}) })
}
} }