-
Notifications
You must be signed in to change notification settings - Fork 0
/
walker_test.go
186 lines (176 loc) · 3.8 KB
/
walker_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package gowalker
import (
"reflect"
"sync"
"testing"
)
func TestWalk(t *testing.T) {
type testStruct struct {
Field string `tag:"field"`
}
tests := []struct {
name string
obj interface{}
wantErr bool
options []Option
fn UserFunc
}{
{
name: "valid struct",
obj: &testStruct{Field: "test"},
wantErr: false,
fn: func(v reflect.Value, meta ObjMeta) error {
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Walk(tt.obj, tt.fn, tt.options...)
if (err != nil) != tt.wantErr {
t.Errorf("Walk() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
type testStruct struct {
Field1 int
Field2 string
SubStruct struct {
SubField1 float32
SubField2 bool
subfield3 bool
}
}
type TestCase struct {
Name string
Obj interface{}
Options []Option
ExpectedPaths map[string]bool
}
func TestVisitedFieldsWithDifferentOptions(t *testing.T) {
testCases := []TestCase{
{
Name: "TestMaxDepth",
Obj: &struct {
Field1 struct {
SubField1 struct {
SubSubField1 int
}
}
}{},
Options: []Option{MaxDepth(1)},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestPrivateFields",
Obj: &struct {
field1 int // Unexported field.
Field2 string
}{},
Options: []Option{PrivateFields()},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestOnlySettable",
Obj: &struct {
ExportedField int
unexportedField int
ExportedButUnsettable int `gowalker:"-"`
}{
ExportedButUnsettable: 5,
},
Options: []Option{OnlySettable()},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestTagFilter",
Obj: &struct {
Field1 int `filter:"true"`
Field2 string
}{},
Options: []Option{WithTagFilter(TagExists("filter", "true"))},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestTypeFilter",
Obj: &struct {
Field1 int
Field2 string
Field3 float64
}{},
Options: []Option{
WithTypeFilter(
TypeIsOneOf(reflect.TypeOf(0), reflect.TypeOf("")),
),
},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestIgnoreType",
Obj: &struct {
Field1 int
Field2 string
Field3 float64
}{},
Options: []Option{WithTypeFilter(IgnoreType(reflect.TypeOf(0.0)))},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestIgnoreTag",
Obj: &struct {
Field1 int `filter:"true"`
Field2 string
}{},
Options: []Option{WithTagFilter(IgnoreTag("filter", "true"))},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestMetaFilter",
Obj: &struct {
Field1 int
Field2 string
}{},
Options: []Option{WithMetaFilter(func(meta ObjMeta) bool {
return meta.Name == "Field1"
})},
ExpectedPaths: map[string]bool{},
},
{
Name: "TestMultipleFilters",
Obj: &struct {
Field1 int `filter:"true"`
Field2 string
Field3 float64
}{},
Options: []Option{
WithTypeFilter(TypeIsOneOf(reflect.TypeOf(""))),
WithTagFilter(TagExists("filter", "true")),
},
ExpectedPaths: map[string]bool{},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
var visitedFields sync.Map
fn := func(v reflect.Value, meta ObjMeta) error {
visitedFields.Store(meta.Path, struct{}{})
return nil
}
if _, err := Walk(tc.Obj, fn, tc.Options...); err != nil {
t.Fatalf("Error during crawling: %v", err)
}
visitedFields.Range(func(key, value interface{}) bool {
path := key.(string)
if _, ok := tc.ExpectedPaths[path]; !ok {
t.Errorf("Unexpected field visited: %s", path)
}
delete(tc.ExpectedPaths, path)
return true
})
for missingField := range tc.ExpectedPaths {
t.Errorf("Field was not visited: %s", missingField)
}
})
}
}