-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_connect_test.go
286 lines (222 loc) · 5.6 KB
/
graph_connect_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package inj
import (
"fmt"
"reflect"
"testing"
)
//////////////////////////////////////////////
// Types
//////////////////////////////////////////////
type validConnectTester struct {
Child1 *connectTesterChild1 `inj:""`
Child2 *connectTesterChild2 `inj:""`
}
type invalidConnectTester struct {
Child1 *connectTesterChild1 `inj:""`
child2 *connectTesterChild2 `inj:""`
}
type connectTesterChild1 struct {
Value string
}
type connectTesterChild2 struct {
Value string
}
func newChildren() (*connectTesterChild1, *connectTesterChild2) {
return &connectTesterChild1{"1"}, &connectTesterChild2{"2"}
}
//////////////////////////////////////////////
// Unit tests
//////////////////////////////////////////////
// Connected objects should be in the graph
func Test_ConnectHappyPath(t *testing.T) {
g, p := newGraph(), validConnectTester{}
c1, c2 := newChildren()
g.Provide(&p, c1, c2)
// Provide calls connect, but call it again
// explicitly
g.connect()
// Basic tests against injection failure
if p.Child1 == nil {
t.Errorf("Child1 is nil")
}
if p.Child2 == nil {
t.Errorf("Child2 is nil")
}
// The main test is of the graph itself
c1_found, c2_found := false, false
for _, n := range g.nodes {
if n.Object == c1 {
c1_found = true
}
if n.Object == c2 {
c2_found = true
}
}
if !c1_found {
t.Errorf("Didn't find c1")
}
if !c2_found {
t.Errorf("Didn't find c2")
}
}
// Unmet dependencies should be counted, and their
// errors stored
func Test_ConnectDepCount(t *testing.T) {
g, p := newGraph(), validConnectTester{}
g.Provide(&p)
g.connect()
if g, e := g.unmetDependency, 2; g != e {
t.Errorf("Got %d unmet deps, expected %d", g, e)
}
if g, e := len(g.errors), 2; g != e {
t.Errorf("Got %d unmet dep errors, expected %d", g, e)
}
}
// Values should actually be assigned
func Test_ConnectAssignmentHappyPath(t *testing.T) {
g, p := newGraph(), &validConnectTester{}
c1, c2 := newChildren()
v := reflect.ValueOf(p)
gnds := []graphNodeDependency{
graphNodeDependency{
Path: structPath(".Child1"),
Type: reflect.TypeOf(c1),
},
graphNodeDependency{
Path: structPath(".Child2"),
Type: reflect.TypeOf(c2),
},
}
g.Provide(c1, c2)
for _, gnd := range gnds {
if err := g.assignValueToNode(v, gnd); err != nil {
t.Errorf("assignValueToNode: %s", err.Error())
}
}
// Basic tests against injection failure
if p.Child1 == nil {
t.Errorf("Child1 is nil")
}
if p.Child2 == nil {
t.Errorf("Child2 is nil")
}
}
// Nodes should update when the values do
func Test_ConnectAssignmentNeutralPath(t *testing.T) {
g, p := newGraph(), &validConnectTester{}
c1, c2 := newChildren()
c3, c4 := newChildren()
v := reflect.ValueOf(p)
// Manually assign the deps
p.Child1 = c1
p.Child2 = c2
gnds := []graphNodeDependency{
graphNodeDependency{
Path: structPath(".Child1"),
Type: reflect.TypeOf(c1),
},
graphNodeDependency{
Path: structPath(".Child2"),
Type: reflect.TypeOf(c2),
},
}
// Assign everything
g.Provide(c3, c4)
// Run through and re-assign (shouldn't error)
for _, gnd := range gnds {
if err := g.assignValueToNode(v, gnd); err != nil {
t.Errorf("assignValueToNode: %s", err.Error())
}
}
if p.Child1 == c1 {
t.Errorf("Child1 is the original c1")
}
if p.Child2 == c2 {
t.Errorf("Child1 is the original c1")
}
}
// Internal variabled should cause the assignment to fail
func Test_ConnectSadPath1(t *testing.T) {
g, p := newGraph(), &invalidConnectTester{}
c1, c2 := newChildren()
v := reflect.ValueOf(p)
gnds := []graphNodeDependency{
graphNodeDependency{
Path: structPath(".child2"),
Type: reflect.TypeOf(c2),
},
}
g.Provide(c1, c2)
// Run through and assign (should error)
for _, gnd := range gnds {
if err := g.assignValueToNode(v, gnd); err == nil {
t.Errorf("assignValueToNode: didn't error")
}
}
}
// Unmet dependencies should cause an error
func Test_ConnectSadPath2(t *testing.T) {
g, p := newGraph(), &validConnectTester{}
c1, c2 := newChildren()
v := reflect.ValueOf(p)
gnds := []graphNodeDependency{
graphNodeDependency{
Path: structPath(".Child1"),
Type: reflect.TypeOf(c1),
},
graphNodeDependency{
Path: structPath(".Child2"),
Type: reflect.TypeOf(c2),
},
}
// Run through and assign (should error)
for _, gnd := range gnds {
if err := g.assignValueToNode(v, gnd); err == nil {
t.Errorf("assignValueToNode: didn't error")
}
}
}
// Should find a reflect value for a path
func Test_ConnectFindFieldValue(t *testing.T) {
g, p := newGraph(), &validConnectTester{}
v := reflect.ValueOf(p)
var descs = []struct {
fieldName string
path structPath
}{
{
fieldName: "*inj.connectTesterChild1",
path: structPath(".Child1"),
},
{
fieldName: "*inj.connectTesterChild2",
path: structPath(".Child2"),
},
}
for _, d := range descs {
rv, e := g.findFieldValue(v, d.path, &[]reflect.Value{})
if e != nil {
t.Errorf("findFieldValue: %s", e.Error())
}
if g, e := rv.Type().String(), d.fieldName; g != e {
t.Errorf("fieldname: got %s, expected %s", g, e)
}
}
}
// Error should be returned if input reflect value isn't
// a struct
func Test_ConnectF(t *testing.T) {
g := newGraph()
_, e := g.findFieldValue(reflect.ValueOf("123"), ".Child1", &[]reflect.Value{})
if e == nil {
fmt.Errorf("Didn't error when type wasn't struct")
}
}
// Incorrect paths should cause an error to be returned
func Test_ConnectG(t *testing.T) {
g, p := newGraph(), &validConnectTester{}
_, e := g.findFieldValue(reflect.ValueOf(p), ".This.Doesnt.Exist", &[]reflect.Value{})
if e == nil {
fmt.Errorf("Didn't error when path was wrong")
}
}