66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
package walk
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
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",
|
|
struct {
|
|
Name string
|
|
Profile struct {
|
|
Age int
|
|
City string
|
|
}
|
|
}{"Chris", struct {
|
|
Age int
|
|
City string
|
|
}{29, "London"}},
|
|
[]string{"Chris", "London"},
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|