-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect_test.go
66 lines (56 loc) · 1.23 KB
/
reflect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package tmpl
import (
"strings"
"testing"
)
type testFieldTree struct {
}
func (*testFieldTree) Method1() error {
return nil
}
type testReturnType struct {
Field1 string
}
func (*testFieldTree) Method2() (*testReturnType, error) {
return &testReturnType{}, nil
}
func (t *testFieldTree) Method3() *testFieldTree {
return t
}
func (t *testFieldTree) Method4() testFieldTree {
return *t
}
func Test_createFieldTree(t *testing.T) {
testTable := []struct {
name string
structOrPtr interface{}
wantFields []string
wantErr bool
}{
{
name: "Detects methods attached via pointer receiver",
structOrPtr: &testFieldTree{},
wantFields: []string{
".Method1",
".Method2.Field1",
".Method3.Method1",
},
},
}
for _, tt := range testTable {
t.Run(tt.name, func(t *testing.T) {
fieldTree, err := createFieldTree(tt.structOrPtr)
if (err != nil) != tt.wantErr {
t.Errorf("createFieldTree() error = %v, wantErr %v", err, tt.wantErr)
return
}
for _, field := range tt.wantFields {
name := strings.TrimPrefix(field, ".")
node := fieldTree.FindPath(strings.Split(name, "."))
if node == nil {
t.Errorf("createFieldTree() field %q not found", field)
}
}
})
}
}