-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject_struct_test.go
More file actions
486 lines (443 loc) · 10.3 KB
/
inject_struct_test.go
File metadata and controls
486 lines (443 loc) · 10.3 KB
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
package dscope
import (
"errors"
"fmt"
"io"
"strings"
"testing"
)
func TestInjectStruct(t *testing.T) {
// dependable
New(func(
inject InjectStruct,
) int {
return 42
})
scope := New(
func() int {
return 42
},
)
var s struct {
I int `dscope:"."`
}
injectStruct(scope, &s, 0)
if s.I != 42 {
t.Fatal()
}
scope.Call(func(
inject InjectStruct,
) {
var s struct {
I int `dscope:"."`
}
inject(&s)
if s.I != 42 {
t.Fatal()
}
})
}
func BenchmarkInjectStruct(b *testing.B) {
scope := New(
func() int {
return 42
},
)
var s struct {
I int `dscope:"."`
}
for b.Loop() {
injectStruct(scope, &s, 0)
}
}
func TestInjectStructBadType(t *testing.T) {
New().Call(func(
inject InjectStruct,
) {
func() {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
msg := fmt.Sprintf("%v", p)
if !strings.Contains(msg, "target type int is not a struct or pointer to struct") {
t.Fatalf("got %v", msg)
}
}()
inject(ptrTo(ptrTo(ptrTo(42))))
}()
})
}
func TestInjectStructNotFound(t *testing.T) {
t.Run("wrapped", func(t *testing.T) {
New().Call(func(
inject InjectStruct,
) {
var s struct {
I Inject[int]
}
inject(&s)
func() {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
msg := fmt.Sprintf("%v", p)
if !strings.Contains(msg, "no definition for int") {
t.Fatalf("got %v", msg)
}
}()
s.I()
}()
})
})
t.Run("tagged", func(t *testing.T) {
New().Call(func(
inject InjectStruct,
) {
var s struct {
I int `dscope:"."`
}
func() {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
msg := fmt.Sprintf("%v", p)
if !strings.Contains(msg, "no definition for int") {
t.Fatalf("got %v", msg)
}
}()
inject(&s)
}()
})
})
}
func TestInjectStructNested(t *testing.T) {
type Inner struct {
I int `dscope:"."`
}
type Outer struct {
S string `dscope:"."`
Inner // Nested struct
}
scope := New(
Provide(42), // Provides int
Provide("foo"), // Provides string
)
var outer Outer
injectStruct(scope, &outer, 0)
if outer.S != "foo" {
t.Fatalf("Outer string field not injected: got %q, want %q", outer.S, "foo")
}
if outer.Inner.I != 42 {
t.Fatalf("Nested struct int field not injected: got %d, want %d", outer.Inner.I, 42)
}
}
func TestInjectStructNilPointerTarget(t *testing.T) {
scope := New(Provide(42)) // Provide something to make injection possible
type MyStruct struct {
I int `dscope:"."`
}
t.Run("nil pointer to struct", func(t *testing.T) {
var ptr *MyStruct = nil // Nil pointer target
func() {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
msg := fmt.Sprintf("%v", p)
// Check for the expected panic message indicating a nil pointer target
if !strings.Contains(msg, "cannot inject into a nil pointer target of type *dscope.MyStruct") {
t.Fatalf("got %v", msg)
}
}()
injectStruct(scope, ptr, 0) // Call with nil pointer
}()
})
t.Run("non-nil pointer to struct", func(t *testing.T) {
ptr := &MyStruct{} // Non-nil pointer target
injectStruct(scope, ptr, 0)
if ptr.I != 42 {
t.Fatalf("injection failed for non-nil pointer: got %d, want %d", ptr.I, 42)
}
})
t.Run("nil interface holding nil pointer to struct", func(t *testing.T) {
var target any = (*MyStruct)(nil) // nil interface holding nil pointer
func() {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
msg := fmt.Sprintf("%v", p)
// Check for the expected panic message
if !strings.Contains(msg, "cannot inject into a nil pointer target of type *dscope.MyStruct") {
t.Fatalf("got %v", msg)
}
}()
injectStruct(scope, target, 0)
}()
})
t.Run("non-nil interface holding non-nil pointer to struct", func(t *testing.T) {
var target any = &MyStruct{} // non-nil interface holding non-nil pointer
injectStruct(scope, target, 0)
ptr := target.(*MyStruct)
if ptr.I != 42 {
t.Fatalf("injection failed for non-nil interface holding pointer: got %d, want %d", ptr.I, 42)
}
})
t.Run("embedded interface", func(t *testing.T) {
type Foo struct {
io.Reader // ignore
}
var foo Foo
New().InjectStruct(&foo)
})
t.Run("embedded pointers", func(t *testing.T) {
type Foo struct {
*int
}
var foo Foo
New().InjectStruct(&foo)
})
t.Run("private field", func(t *testing.T) {
type Foo struct {
i int `dscope:"."`
}
var foo Foo
New(func() int {
return 42
}).InjectStruct(&foo)
})
}
func TestInjectStructEmbeddedNilPointer(t *testing.T) {
type Inner struct {
I int `dscope:"."`
}
type Outer struct {
*Inner // Embedded nil pointer to struct
}
scope := New(Provide(42))
var outer Outer
// outer.Inner is nil here
// This should not panic. It should allocate and inject into Inner.
scope.InjectStruct(&outer)
if outer.Inner == nil {
t.Fatal("Embedded pointer field was not initialized")
}
if outer.Inner.I != 42 {
t.Fatalf("Nested struct int field not injected: got %d, want %d", outer.Inner.I, 42)
}
}
func TestInjectStructWithInjectTag(t *testing.T) {
scope := New(
Provide(int(42)),
)
var s struct {
I int `dscope:"inject"`
}
scope.InjectStruct(&s)
if s.I != 42 {
t.Fatal()
}
}
func TestInjectStructWithTaggedInjectField(t *testing.T) {
scope := New(
Provide(int(42)),
)
var s struct {
I Inject[int] `dscope:"."`
}
scope.InjectStruct(&s)
if s.I() != 42 {
t.Fatal()
}
}
func TestInjectStructTaggedEmbedded(t *testing.T) {
type Inner struct {
I int `dscope:"."`
}
type Outer struct {
Inner // Embedded, no tag -> should recurse
}
scope := New(
Provide(42), // Provides int, but not Inner
)
var outer Outer
// Before the fix, this panics because it tries to find a provider for `Inner`.
// After the fix, this should recursively inject into the embedded struct.
scope.InjectStruct(&outer)
if outer.I != 42 {
t.Fatalf("Embedded tagged struct field not injected: got %d, want %d", outer.I, 42)
}
}
func TestInjectStructNonPointerTarget(t *testing.T) {
scope := New(Provide(42))
// Pass a non‑pointer struct value; should panic with ErrBadArgument.
var s struct {
I int `dspace:"."`
}
defer func() {
if p := recover(); p == nil {
t.Fatal("should panic")
} else {
err, ok := p.(error)
if !ok {
t.Fatalf("panic value not an error: %v", p)
}
if !errors.Is(err, ErrBadArgument) {
t.Fatalf("expected ErrBadArgument, got %T: %v", err, err)
}
if !strings.Contains(err.Error(), "target must be a pointer") {
t.Fatalf("unexpected error message: %s", err.Error())
}
}
}()
injectStruct(scope, s, 0) // non‑pointer target
}
func TestInjectStructEmbeddedValue(t *testing.T) {
type Inner struct {
I int `dscope:"."`
}
type Outer struct {
Inner `dscope:"."` // Tagged embedded field -> should inject value from scope
}
// Case 1: Inner is provided in scope. Should be injected.
scope := New(
Provide(42),
func() Inner {
return Inner{I: 99}
},
)
var outer Outer
scope.InjectStruct(&outer)
if outer.Inner.I != 99 {
t.Fatalf("Tagged embedded struct should receive value from scope, got %d want 99", outer.Inner.I)
}
// Case 2: Inner is NOT provided. Should panic (not recurse).
scope2 := New(Provide(42))
var outer2 Outer
func() {
defer func() {
if p := recover(); p == nil {
t.Fatal("should panic when tagged embedded struct is missing in scope")
}
}()
scope2.InjectStruct(&outer2)
}()
}
func TestInjectStructNilIntermediatePointer(t *testing.T) {
type S struct {
I int `dscope:"."`
}
scope := New(Provide(42))
t.Run("pointer to nil pointer", func(t *testing.T) {
var s *S // nil
// InjectStruct takes &s which is **S.
scope.InjectStruct(&s)
if s == nil {
t.Fatal("s is still nil")
}
if s.I != 42 {
t.Fatalf("s.I = %d, want 42", s.I)
}
})
t.Run("pointer to pointer to nil pointer", func(t *testing.T) {
var s **S // nil
scope.InjectStruct(&s)
if s == nil {
t.Fatal("s is nil")
}
if *s == nil {
t.Fatal("*s is nil")
}
if (*s).I != 42 {
t.Fatal("not injected")
}
})
}
func TestInjectStructRecursivePointer(t *testing.T) {
type T *T
var ptr T
func() {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
err, ok := p.(error)
if !ok {
t.Fatalf("panic value not an error: %v", p)
}
if !errors.Is(err, ErrBadArgument) {
t.Fatalf("expected ErrBadArgument, got %T: %v", err, err)
}
if !strings.Contains(err.Error(), "recursive pointer type") {
t.Fatalf("unexpected error message: %s", err.Error())
}
}()
injectStruct(New(), &ptr, 0)
}()
}
func TestInjectStructPointerToInject(t *testing.T) {
// This test reproduces a panic where InjectStruct tries to process *Inject[T]
// because it implements the isInject interface, but it's not a function.
type S struct {
I *Inject[int]
}
scope := New(Provide(42))
var s S
// Should not panic. It should simply ignore the field or handle it gracefully.
scope.InjectStruct(&s)
}
func TestInjectStructNil(t *testing.T) {
scope := New()
defer func() {
p := recover()
if p == nil {
t.Fatal("InjectStruct(nil) should panic")
}
err, ok := p.(error)
if !ok {
t.Fatalf("panic value not an error: %v", p)
}
// We expect a structured ErrBadArgument, not a raw reflect panic.
if !errors.Is(err, ErrBadArgument) {
t.Errorf("expected ErrBadArgument, got %T: %v", err, err)
}
if !strings.Contains(err.Error(), "target must be a pointer") {
t.Errorf("unexpected error message: %s", err.Error())
}
}()
// This currently causes a reflect panic because Type() is called on invalid value
scope.InjectStruct(nil)
}
func TestInjectStructCircularEmbedding(t *testing.T) {
// This test verifies that InjectStruct detects circular embedding and panics
// instead of causing a stack overflow.
type Rec struct {
*Rec
}
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
err, ok := p.(error)
if !ok {
t.Fatalf("panic value not an error: %v", p)
}
if !errors.Is(err, ErrBadArgument) {
t.Fatalf("expected ErrBadArgument, got %T: %v", err, err)
}
if !strings.Contains(err.Error(), "recursive struct injection depth limit exceeded") {
t.Fatalf("unexpected error message: %s", err.Error())
}
}()
New().InjectStruct(&Rec{})
}