~marcopolo/di

ref: baa3947d3bc1bddb0a034f5054121078962a226a di/di_test.go -rw-r--r-- 6.5 KiB
baa3947d — Marco Munizaga Initial Version 9 months ago
                                                                                
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
package di

import (
	"errors"
	"strings"
	"testing"
)

func TestBuildSuccess(t *testing.T) {
	type A struct {
		val string
	}
	type C struct {
		val int
	}
	type B struct {
		a  *A
		cs []C
	}

	type Config struct {
		MakeA  Provide[*A]
		MakeB  Provide[*B]
		MakeCs []Provide[C]
	}

	cfg := Config{
		MakeA: MustProvide[*A](func() (*A, error) {
			return &A{val: "hello"}, nil
		}),
		MakeB: MustProvide[*B](func(a *A, cs []C) *B {
			return &B{a: a, cs: cs}
		}),
		MakeCs: []Provide[C]{
			MustProvide[C](C{val: 1}),
			MustProvide[C](func() (C, error) {
				return C{val: 2}, nil
			})},
	}

	type Result struct {
		A *A
		B *B
	}
	var res Result
	err := Build(cfg, &res)
	if err != nil {
		t.Fatalf("Build failed: %v", err)
	}
	if res.A == nil {
		t.Fatalf("expected res.A to be populated")
	}
	if res.B == nil {
		t.Fatalf("expected res.B to be populated")
	}
	if res.B.a != res.A {
		t.Fatalf("expected B.a to reference A instance")
	}
	if len(res.B.cs) != 2 {
		t.Fatalf("wrong count. Saw %d", len(res.B.cs))
	}
	if res.B.cs[0].val != 1 {
		t.Fatalf("wrong value")
	}
	if res.B.cs[1].val != 2 {
		t.Fatalf("wrong value")
	}
	if res.A.val != "hello" {
		t.Fatalf("unexpected A value: %s", res.A.val)
	}
}

func TestBuildSuccess2(t *testing.T) {
	type A struct {
		val string
	}
	type B struct {
		a *A
	}

	type Config struct {
		MakeA func() (*A, error)
		MakeB func(*A) (*B, error)
	}

	type Result struct {
		A *A
	}

	cfg := Config{
		MakeA: func() (*A, error) {
			return &A{val: "hello"}, nil
		},
		MakeB: func(a *A) (*B, error) {
			panic("Unexpected call to MakeB")
			// (removed unreachable code after panic)
		},
	}

	var res Result
	err := Build(cfg, &res)
	if err != nil {
		t.Fatalf("Build failed: %v", err)
	}
	if res.A == nil {
		t.Fatalf("expected res.A to be populated")
	}
	if res.A.val != "hello" {
		t.Fatalf("unexpected A value: %s", res.A.val)
	}
}

// Test that constructor error is propagated.
func TestBuildConstructorError(t *testing.T) {
	type A struct{}
	sentinel := errors.New("boom")

	type Config struct {
		MakeA func() (*A, error)
	}
	type Result struct {
		A *A
	}

	cfg := Config{
		MakeA: func() (*A, error) {
			return nil, sentinel
		},
	}
	var res Result
	err := Build(cfg, &res)
	if err == nil {
		t.Fatalf("expected error")
	}
	if !strings.Contains(err.Error(), "MakeA") {
		t.Fatalf("expected error to mention constructor name, got: %v", err)
	}
	if !strings.Contains(err.Error(), "boom") {
		t.Fatalf("expected original error message, got: %v", err)
	}
	if res.A != nil {
		t.Fatalf("result A should not be populated on constructor failure")
	}
}

// Test missing dependency (constructor requires *A but *A not provided).
func TestBuildMissingDependency(t *testing.T) {
	type A struct{}
	type B struct {
		a *A
	}

	type Config struct {
		MakeB func(*A) (*B, error)
	}
	type Result struct {
		B *B
	}

	cfg := Config{
		MakeB: func(a *A) (*B, error) {
			return &B{a: a}, nil
		},
	}
	var res Result
	err := Build(cfg, &res)
	if err == nil {
		t.Fatalf("expected missing dependency error")
	}
	// Parameter type string should appear (may be *di.A).
	if !strings.Contains(err.Error(), "*di.A") && !strings.Contains(err.Error(), "di.A") {
		t.Fatalf("expected error to mention missing type *di.A, got: %v", err)
	}
	if res.B != nil {
		t.Fatalf("result B should not be populated")
	}
}

// Test cycle detection between X and Y.
func TestBuildCycleDetection(t *testing.T) {
	type X struct{}
	type Y struct{}

	type Config struct {
		MakeX func(*Y) *X
		MakeY func(*X) *Y
	}
	type Result struct {
		X *X
		Y *Y
	}

	cfg := Config{
		MakeX: func(y *Y) *X {
			return &X{}
		},
		MakeY: func(x *X) *Y {
			return &Y{}
		},
	}

	var res Result
	err := Build(cfg, &res)
	if err == nil {
		t.Fatalf("expected cycle detection error")
	}
	// Both constructors should still be listed as remaining.
	if !strings.Contains(err.Error(), "MakeX") || !strings.Contains(err.Error(), "MakeY") {
		t.Fatalf("expected error to list remaining constructors MakeX and MakeY, got: %v", err)
	}
	if res.X != nil || res.Y != nil {
		t.Fatalf("cycle should prevent any construction; got X=%v Y=%v", res.X, res.Y)
	}
}

// Ensure that providing pre-supplied value satisfies dependency without constructor for it.
func TestBuildWithPreSuppliedValue(t *testing.T) {
	type A struct {
		v int
	}
	type B struct {
		a *A
	}

	type Config struct {
		// Only constructor for B; A provided directly in config.
		A  *A
		MB func(*A) (*B, error)
	}
	type Result struct {
		A *A
		B *B
	}

	cfg := Config{
		A: &A{v: 42},
		MB: func(a *A) (*B, error) {
			return &B{a: a}, nil
		},
	}

	var res Result
	err := Build(cfg, &res)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if res.A == nil || res.A.v != 42 {
		t.Fatalf("expected pre-supplied A (42), got %+v", res.A)
	}
	if res.B == nil || res.B.a != res.A {
		t.Fatalf("expected B referencing A, got %+v", res.B)
	}
}

func TestTypeAlias(t *testing.T) {
	type ANum int
	type Config struct {
		A ANum
		B int
	}

	type Result struct {
		A ANum
	}
	var res Result
	err := Build(Config{3, 4}, &res)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if res.A != 3 {
		t.Fatalf("expected A=3, got %v", res.A)
	}
}

func TestReferenceConfig(t *testing.T) {
	type NestedConfig struct {
		OtherSetting   bool
		NestedDecision func(c NestedConfig) uint
	}

	type Config struct {
		NestedConfig
		SomeSetting bool
		Inner       func(c Config) int
	}

	type Result struct {
		A int
		B uint
	}
	var res Result
	err := Build(Config{
		SomeSetting: true,
		Inner: func(c Config) int {
			if c.SomeSetting {
				return 1
			}
			return 0
		},
		NestedConfig: NestedConfig{
			OtherSetting: true,
			NestedDecision: func(c NestedConfig) uint {
				if c.OtherSetting {
					return 1
				}
				return 0
			},
		},
	}, &res)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if res.A != 1 {
		t.Fatalf("expected A=1, got %v", res.A)
	}
	if res.B != 1 {
		t.Fatalf("expected B=1, got %v", res.A)
	}
}

func TestNew(t *testing.T) {
	type Config struct {
		A int
	}

	type Result struct {
		A int
	}

	cfg := Config{A: 42}
	res, err := New[Result](cfg)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if res.A != 42 {
		t.Fatalf("expected A=42, got %v", res.A)
	}
}

func TestSpecificTypes(t *testing.T) {
	type Config struct {
		A int
	}

	cfg := Config{A: 42}
	res, err := New[int](cfg)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if res != 42 {
		t.Fatalf("expected A=42, got %v", res)
	}

	var res2 int
	err = Build(Config{A: 42}, &res2)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if res2 != 42 {
		t.Fatalf("expected A=42, got %v", res2)
	}
}