go-by-test/walk/walk_test.go
2024-09-20 20:27:53 +02:00

80 lines
1.3 KiB
Go

package walk
import (
"reflect"
"testing"
)
type Profile struct {
Age int
City string
}
type Person struct {
Name string
Profile Profile
}
func TestWalk(t *testing.T) {
cases := []struct {
Name string
Input interface{}
ExpectedCalls []string
}{
{
"struct with one string field",
struct {
Name string
}{"Chris"},
[]string{"Chris"},
},
{
"struct with two string fields",
struct {
Name string
City string
}{"Chris", "London"},
[]string{"Chris", "London"},
},
{
"struct with non string field",
struct {
Name string
Age int
}{"Chris", 29},
[]string{"Chris"},
},
{
"nested fields",
Person{"Chris", Profile{29, "London"}},
[]string{"Chris", "London"},
},
{
"pointer to things",
&Person{"Chris", Profile{29, "London"}},
[]string{"Chris", "London"},
},
{
"slices",
[]Profile{
{29, "London"},
{33, "Paris"},
},
[]string{"London", "Paris"},
},
}
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 !reflect.DeepEqual(got, test.ExpectedCalls) {
t.Errorf("got %v want %v", got, test.ExpectedCalls)
}
})
}
}