-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdep_test.go
613 lines (564 loc) · 13.6 KB
/
dep_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package gone
import (
"reflect"
"strings"
"testing"
)
// createTestCoffin is a helper function to create test coffins
func createTestCoffin(name string) *coffin {
return &coffin{
name: name,
goner: struct{}{},
}
}
func TestCheckCircularDepsAndGetBestInitOrder(t *testing.T) {
tests := []struct {
name string
initiatorDepsMap map[*coffin][]*coffin
wantCircular bool
wantOrderLen int
}{
{
name: "Linear dependency chain",
initiatorDepsMap: func() map[*coffin][]*coffin {
a := createTestCoffin("A")
b := createTestCoffin("B")
c := createTestCoffin("C")
return map[*coffin][]*coffin{
a: {b},
b: {c},
c: {},
}
}(),
wantCircular: false,
wantOrderLen: 3,
},
{
name: "Circular dependency",
initiatorDepsMap: func() map[*coffin][]*coffin {
a := createTestCoffin("A")
b := createTestCoffin("B")
c := createTestCoffin("C")
return map[*coffin][]*coffin{
a: {b},
b: {c},
c: {a}, // Creates a cycle
}
}(),
wantCircular: true,
wantOrderLen: 0,
},
{
name: "Empty dependency map",
initiatorDepsMap: map[*coffin][]*coffin{},
wantCircular: false,
wantOrderLen: 0,
},
{
name: "Diamond dependency",
initiatorDepsMap: func() map[*coffin][]*coffin {
a := createTestCoffin("A")
b := createTestCoffin("B")
c := createTestCoffin("C")
d := createTestCoffin("D")
return map[*coffin][]*coffin{
a: {b, c},
b: {d},
c: {d},
d: {},
}
}(),
wantCircular: false,
wantOrderLen: 4,
},
{
name: "Self dependency",
initiatorDepsMap: func() map[*coffin][]*coffin {
a := createTestCoffin("A")
return map[*coffin][]*coffin{
a: {a}, // Self dependency
}
}(),
wantCircular: true,
wantOrderLen: 0,
},
{
name: "Multiple circular dependencies",
initiatorDepsMap: func() map[*coffin][]*coffin {
a := createTestCoffin("A")
b := createTestCoffin("B")
c := createTestCoffin("C")
d := createTestCoffin("D")
return map[*coffin][]*coffin{
a: {b},
b: {c},
c: {a}, // First cycle
d: {d}, // Second cycle (self dependency)
}
}(),
wantCircular: true,
wantOrderLen: 0,
},
{
name: "Single node with no dependencies",
initiatorDepsMap: func() map[*coffin][]*coffin {
a := createTestCoffin("A")
return map[*coffin][]*coffin{
a: {},
}
}(),
wantCircular: false,
wantOrderLen: 1,
},
{
name: "Node with empty dependency slice",
initiatorDepsMap: func() map[*coffin][]*coffin {
a := createTestCoffin("A")
b := createTestCoffin("B")
return map[*coffin][]*coffin{
a: {b},
b: make([]*coffin, 0), // Explicitly empty slice
}
}(),
wantCircular: false,
wantOrderLen: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
circularDeps, initOrder := checkCircularDepsAndGetBestInitOrder(tt.initiatorDepsMap)
// Check circular dependency detection
if (len(circularDeps) > 0) != tt.wantCircular {
t.Errorf("checkCircularDepsAndGetBestInitOrder() circular = %v, want %v",
len(circularDeps) > 0, tt.wantCircular)
}
// Check initialization order length
if len(initOrder) != tt.wantOrderLen {
t.Errorf("checkCircularDepsAndGetBestInitOrder() order length = %v, want %v",
len(initOrder), tt.wantOrderLen)
}
if len(initOrder) > 0 {
// Verify the initialization order is valid
seen := make(map[*coffin]bool)
for _, co := range initOrder {
// Check that all dependencies of current coffin have been initialized
for _, dep := range tt.initiatorDepsMap[co] {
if !seen[dep] {
t.Errorf("Invalid initialization order: %v depends on %v but it's not initialized yet",
co.name, dep.name)
}
}
seen[co] = true
}
}
})
}
}
type testFunc func(*string, *int)
func TestGetGonerFillDeps(t *testing.T) {
type testStruct struct {
Flag // embed gone.Flag
Dep1 *string `gone:"dep1"`
Dep2 *int `gone:"dep2"`
}
tests := []struct {
name string
goner interface{}
setupCore func(*Core)
wantDepsCount int
wantErr bool
}{
{
name: "Struct with dependencies",
goner: &testStruct{},
setupCore: func(c *Core) {
dep1 := &coffin{name: "dep1", needInitBeforeUse: true}
dep2 := &coffin{name: "dep2", needInitBeforeUse: true}
c.nameMap = map[string]*coffin{
"dep1": dep1,
"dep2": dep2,
}
},
wantDepsCount: 2,
wantErr: false,
},
{
name: "Struct with missing dependency",
goner: &testStruct{},
setupCore: func(c *Core) {
dep1 := &coffin{name: "dep1", needInitBeforeUse: true}
c.nameMap = map[string]*coffin{
"dep1": dep1,
// dep2 is missing
}
},
wantDepsCount: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := &Core{}
tt.setupCore(core)
co := &coffin{
goner: tt.goner,
}
deps, err := core.getGonerFillDeps(co)
if (err != nil) != tt.wantErr {
t.Errorf("getGonerFillDeps() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && len(deps) != tt.wantDepsCount {
t.Errorf("getGonerFillDeps() got %d dependencies, want %d", len(deps), tt.wantDepsCount)
}
})
}
}
func TestGetGonerDeps(t *testing.T) {
type testStruct struct {
Flag // embed gone.Flag
Dep1 *string `gone:"dep1"`
Dep2 *int `gone:"dep2"`
}
tests := []struct {
name string
coffin *coffin
setupCore func(*Core)
wantFillDepsCount int
wantInitDepsCount int
wantErr bool
}{
{
name: "Normal coffin with dependencies",
coffin: &coffin{
goner: &testStruct{},
lazyFill: false,
},
setupCore: func(c *Core) {
dep1 := &coffin{name: "dep1", needInitBeforeUse: true}
dep2 := &coffin{name: "dep2", needInitBeforeUse: true}
c.nameMap = map[string]*coffin{
"dep1": dep1,
"dep2": dep2,
}
},
wantFillDepsCount: 2,
wantInitDepsCount: 1, // The coffin itself needs initialization
wantErr: false,
},
{
name: "Lazy fill coffin",
coffin: &coffin{
goner: &testStruct{},
lazyFill: true,
},
setupCore: func(c *Core) {
dep1 := &coffin{name: "dep1", needInitBeforeUse: true}
dep2 := &coffin{name: "dep2", needInitBeforeUse: true}
c.nameMap = map[string]*coffin{
"dep1": dep1,
"dep2": dep2,
}
},
wantFillDepsCount: 2,
wantInitDepsCount: 0, // No init dependencies due to lazy fill
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := &Core{}
tt.setupCore(core)
fillDeps, initDeps, err := core.getGonerDeps(tt.coffin)
if (err != nil) != tt.wantErr {
t.Errorf("getGonerDeps() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if len(fillDeps) != tt.wantFillDepsCount {
t.Errorf("getGonerDeps() got %d fill dependencies, want %d", len(fillDeps), tt.wantFillDepsCount)
}
if len(initDeps) != tt.wantInitDepsCount {
t.Errorf("getGonerDeps() got %d init dependencies, want %d", len(initDeps), tt.wantInitDepsCount)
}
}
})
}
}
func TestGetDepByName(t *testing.T) {
tests := []struct {
name string
setupCore func(*Core)
depName string
wantErr bool
}{
{
name: "Existing dependency",
setupCore: func(c *Core) {
c.nameMap = map[string]*coffin{
"test": {name: "test"},
}
},
depName: "test",
wantErr: false,
},
{
name: "Non-existing dependency",
setupCore: func(c *Core) {
c.nameMap = map[string]*coffin{}
},
depName: "missing",
wantErr: true,
},
{
name: "Empty name",
setupCore: func(c *Core) {
c.nameMap = map[string]*coffin{}
},
depName: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := &Core{}
tt.setupCore(core)
got, err := core.getDepByName(tt.depName)
if (err != nil) != tt.wantErr {
t.Errorf("getDepByName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got == nil {
t.Error("getDepByName() returned nil but expected a coffin")
}
})
}
}
func TestGetDepByType(t *testing.T) {
type testStruct struct{}
tests := []struct {
name string
setupCore func(*Core)
typeToGet reflect.Type
wantErr bool
errMsg string
}{
{
name: "Existing type in default coffins",
setupCore: func(c *Core) {
c.coffins = []*coffin{{
goner: &testStruct{},
}}
},
typeToGet: reflect.TypeOf(&testStruct{}),
wantErr: false,
},
{
name: "Existing type in provider map",
setupCore: func(c *Core) {
c.typeProviderDepMap = map[reflect.Type]*coffin{
reflect.TypeOf(&testStruct{}): {name: "provider"},
}
},
typeToGet: reflect.TypeOf(&testStruct{}),
wantErr: false,
},
{
name: "Non-pointer struct type",
setupCore: func(c *Core) {
c.coffins = []*coffin{}
},
typeToGet: reflect.TypeOf(testStruct{}),
wantErr: true,
errMsg: "Maybe, you should use A Pointer to this type?",
},
{
name: "Non-existing type",
setupCore: func(c *Core) {
c.coffins = []*coffin{}
},
typeToGet: reflect.TypeOf(&struct{}{}),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := &Core{
log: GetDefaultLogger(),
}
tt.setupCore(core)
got, err := core.getDepByType(tt.typeToGet)
if (err != nil) != tt.wantErr {
t.Errorf("getDepByType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && tt.errMsg != "" && err != nil {
if !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("getDepByType() error message = %v, want to contain %v", err, tt.errMsg)
}
}
if !tt.wantErr && got == nil {
t.Error("getDepByType() returned nil but expected a coffin")
}
})
}
}
func TestGetSliceDepsByType(t *testing.T) {
type testStruct struct {
Flag // embed gone.Flag
}
type testPtrSlice []*testStruct
tests := []struct {
name string
setupCore func(*Core)
typeToGet reflect.Type
wantDepsCount int
wantNilResult bool
description string
}{
{
name: "Non-slice type",
setupCore: func(c *Core) {},
typeToGet: reflect.TypeOf(testStruct{}),
wantNilResult: true,
description: "Should return nil for non-slice types",
},
{
name: "Pointer slice type",
setupCore: func(c *Core) {
elemCoffin := &coffin{
name: "elemCoffin",
goner: &testStruct{},
}
c.coffins = []*coffin{elemCoffin}
},
typeToGet: reflect.TypeOf(testPtrSlice{}),
wantDepsCount: 1,
description: "Should handle slice of pointers correctly",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := NewCore()
tt.setupCore(core)
got := core.getSliceDepsByType(tt.typeToGet)
if tt.wantNilResult {
if got != nil {
t.Errorf("getSliceDepsByType() = %v, want nil", got)
}
return
}
if len(got) != tt.wantDepsCount {
t.Errorf("getSliceDepsByType() returned %d deps, want %d deps - %s",
len(got), tt.wantDepsCount, tt.description)
}
// Check for duplicates
seen := make(map[*coffin]bool)
for _, dep := range got {
if seen[dep] {
t.Errorf("getSliceDepsByType() returned duplicate dependency: %v", dep.name)
}
seen[dep] = true
}
})
}
}
func TestCollectDeps(t *testing.T) {
type testStruct struct {
Flag // embed gone.Flag
Dep1 *string `gone:"dep1"`
Dep2 *int `gone:"dep2"`
}
tests := []struct {
name string
setupCore func(*Core)
wantDeps int
wantErr bool
}{
{
name: "Non-pointer goner",
setupCore: func(c *Core) {
c.coffins = []*coffin{
{
name: "non-pointer",
goner: testStruct{}, // Not a pointer
},
}
},
wantDeps: 0,
wantErr: true,
},
{
name: "Invalid dependency",
setupCore: func(c *Core) {
c.coffins = []*coffin{
{
name: "invalid",
goner: "not a valid goner",
},
}
},
wantDeps: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a new core with debug logging
core := NewCore()
core.log = GetDefaultLogger()
core.log.SetLevel(DebugLevel)
// Setup the test case
tt.setupCore(core)
// Call collectDeps
depsMap, err := core.collectDeps()
// Check error condition
if (err != nil) != tt.wantErr {
t.Errorf("collectDeps() error = %v, wantErr %v", err, tt.wantErr)
return
}
// If we expect success, verify the dependency count
if !tt.wantErr {
totalDeps := 0
for _, deps := range depsMap {
totalDeps += len(deps)
}
if totalDeps != tt.wantDeps {
t.Errorf("collectDeps() got %d total dependencies, want %d", totalDeps, tt.wantDeps)
}
// Verify that each dependency is valid
for d, deps := range depsMap {
if d.coffin == nil {
t.Error("collectDeps() returned a dependency with nil coffin")
}
for _, dep := range deps {
if dep.coffin == nil {
t.Error("collectDeps() returned a dependency with nil dependent coffin")
}
}
}
}
})
}
}
func TestNeedInitBeforeUse(t *testing.T) {
type Test struct {
Flag
Dep1 *testNamedProvider `gone:"dep1"`
Dep2 []*testNamedProvider `gone:"*"`
}
core := NewCore()
err := core.Load(&testNamedProvider{}, Name("dep1"))
if err != nil {
t.Fatalf("Failed to load testNamedProvider: %v", err)
}
err = core.Load(&Test{})
co := newCoffin(&Test{})
dependencies, err := core.getGonerFillDeps(co)
if err != nil {
t.Fatalf("Failed to get dependencies: %v", err)
}
if len(dependencies) == 0 {
t.Fatalf("Failed to get dependencies: %v", err)
}
}