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" Name string
var got []string Input interface{}
ExpectedCalls []string
}{
{
"struct with one string field",
struct {
Name string
}{"Chris"},
[]string{"Chris"},
},
}
x := struct { for _, test := range cases {
Name string t.Run(test.Name, func(t *testing.T) {
}{expected} var got []string
Walk(test.Input, func(input string) {
got = append(got, input)
})
Walk(x, func(input string) { if !reflect.DeepEqual(got, test.ExpectedCalls) {
got = append(got, input) t.Errorf("got %v want %v", got, test.ExpectedCalls)
}
}) })
}
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)
}
})
} }