go-by-test/walk/walk_test.go

80 lines
1.3 KiB
Go
Raw Normal View History

2024-09-18 16:15:01 +00:00
package walk
import (
2024-09-20 17:56:28 +00:00
"reflect"
2024-09-18 16:15:01 +00:00
"testing"
)
2024-09-20 18:27:53 +00:00
type Profile struct {
Age int
City string
}
2024-09-20 18:19:49 +00:00
type Person struct {
Name string
2024-09-20 18:27:53 +00:00
Profile Profile
2024-09-20 18:19:49 +00:00
}
2024-09-18 16:15:01 +00:00
func TestWalk(t *testing.T) {
2024-09-20 17:56:28 +00:00
cases := []struct {
Name string
Input interface{}
ExpectedCalls []string
}{
{
"struct with one string field",
struct {
Name string
}{"Chris"},
[]string{"Chris"},
},
2024-09-20 17:59:39 +00:00
{
"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"},
},
2024-09-20 18:15:59 +00:00
{
"nested fields",
2024-09-20 18:27:53 +00:00
Person{"Chris", Profile{29, "London"}},
2024-09-20 18:19:49 +00:00
[]string{"Chris", "London"},
},
{
"pointer to things",
2024-09-20 18:27:53 +00:00
&Person{"Chris", Profile{29, "London"}},
2024-09-20 18:15:59 +00:00
[]string{"Chris", "London"},
},
2024-09-20 18:27:53 +00:00
{
"slices",
[]Profile{
{29, "London"},
{33, "Paris"},
},
[]string{"London", "Paris"},
},
2024-09-20 17:56:28 +00:00
}
2024-09-18 16:15:01 +00:00
2024-09-20 17:56:28 +00:00
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)
})
2024-09-18 16:15:01 +00:00
2024-09-20 17:56:28 +00:00
if !reflect.DeepEqual(got, test.ExpectedCalls) {
t.Errorf("got %v want %v", got, test.ExpectedCalls)
}
2024-09-18 16:15:01 +00:00
})
2024-09-20 17:56:28 +00:00
}
2024-09-18 16:15:01 +00:00
}