go-by-test/walk/walk_test.go
2024-09-20 20:41:50 +02:00

96 lines
1.5 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"},
},
{
"arrays",
[2]Profile{
{29, "London"},
{33, "Paris"},
},
[]string{"London", "Paris"},
},
{
"maps",
map[string]string{
"Cow": "meuh",
"Sheep": "meh",
},
[]string{"meuh", "meh"},
},
}
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)
}
})
}
}