-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_test.go
446 lines (407 loc) · 11.2 KB
/
run_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
package nject
// TODO: test MustConsume on terminal injector
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testSeq = Cacheable(Sequence("TBF",
func(s s0) s1 {
if s0("s0 value") != s {
panic("s1")
}
return "s1 value"
},
func(s s1) s2 {
if s1("s1 value") != s {
panic("s2")
}
return "s2 value"
},
func(s s3) s4 {
if s3("s3 value") != s {
panic("s4")
}
return "s4 value"
},
func(s s2) s5 {
if s2("s2 value") != s {
panic("s5")
}
return "s5 value"
}))
// TestRun verifies that run works.
func TestRunWorks(t *testing.T) {
wrapTest(t, func(t *testing.T) {
called := false
require.NoError(t, Run("run1",
s3("s3 value"),
s0("s0 value"),
testSeq, func(s s5) {
assert.Equal(t, s5("s5 value"), s)
called = true
}))
assert.True(t, called)
})
}
func TestRunMissingValue(t *testing.T) {
wrapTest(t, func(t *testing.T) {
called := false
require.Error(t, Run("run1",
s0("s0 value"),
testSeq, func(s s4) {
assert.Equal(t, s4("s4 value"), s)
called = true
}))
assert.False(t, called)
})
}
func TestRunReturnsErrorNoError(t *testing.T) {
testRunReturnsError(t, nil)
}
func TestRunReturnsErrorError(t *testing.T) {
testRunReturnsError(t, fmt.Errorf("an error"))
}
func testRunReturnsError(t *testing.T, e error) {
wrapTest(t, func(t *testing.T) {
called := false
err := Run("run returns error",
s3("s3 value"),
s0("s0 value"),
testSeq,
func(s s5) error {
assert.Equal(t, s5("s5 value"), s)
called = true
return e
})
if e == nil {
require.NoError(t, err)
} else {
require.Error(t, err)
assert.Equal(t, e.Error(), err.Error())
}
assert.True(t, called)
})
}
func TestMustRun(t *testing.T) {
wrapTest(t, func(t *testing.T) {
t.Run("no panic", func(t *testing.T) {
MustRun("run1",
s3("s3 value"),
s0("s0 value"),
testSeq,
func(s s4) {
assert.Equal(t, s4("s4 value"), s)
})
})
t.Run("panic", func(t *testing.T) {
assert.Panics(t, func() {
MustRun("run1",
s0("s0 value"),
testSeq,
func(s s4) {
assert.Equal(t, s4("s4 value"), s)
})
})
})
})
}
func TestNilLiterals(t *testing.T) {
wrapTest(t, func(t *testing.T) {
var intp *int
called := false
require.NoError(t, Run("test nil",
intp,
nil,
func(ip *int) error {
var ip2 *int
assert.Equal(t, ip2, ip)
called = true
return nil
}))
assert.True(t, called)
})
}
func TestUnusedLiteral(t *testing.T) {
wrapTest(t, func(t *testing.T) {
var intp *int
called := false
require.NoError(t, Run("test unused",
intp,
"seven",
func(s string) error {
assert.Equal(t, "seven", s)
called = true
return nil
}))
assert.True(t, called)
})
}
func testWrapperFuncs(t *testing.T) (*int, Provider, Provider, Provider) {
var callCount int
return &callCount,
Provide("S2SYNC", func(inner func() s2, s2p s2prime) {
assert.Equal(t, s2(s2p), inner())
}),
Required(Provide("S1S2", func(inner func(s0) s1, s s0) s2 {
return s2(inner(s+s0("bar")) + s1("foo"))
})),
Provide("S0S1", func(s s0) s1 {
assert.Equal(t, s0("bazbar"), s)
callCount++
return s1(s)
})
}
func TestWrappersRunError(t *testing.T) {
wrapTest(t, func(t *testing.T) {
_, _, s1s2, s0s1 := testWrapperFuncs(t)
require.Error(t, Run("tw2",
s2prime("bazbarfoo"),
s0("baz"),
// s2sync not included, so nobody consumes s2 returned by s1s2
s1s2,
s0s1))
})
}
func TestWrappersRunNoError(t *testing.T) {
wrapTest(t, func(t *testing.T) {
callCountP, s2sync, s1s2, s0s1 := testWrapperFuncs(t)
require.NoError(t, Run("tw1",
s2prime("bazbarfoo"),
s0("baz"),
s2sync,
s1s2,
s0s1))
require.Equal(t, 1, *callCountP)
})
}
func TestWrappersBindNoError(t *testing.T) {
wrapTest(t, func(t *testing.T) {
callCountP, _, s1s2, s0s1 := testWrapperFuncs(t)
var tw3Invoke func(s0) s2
require.NoError(t, Sequence("tw3", s1s2, s0s1).Bind(&tw3Invoke, nil))
require.Equal(t, s2("bazbarfoo"), tw3Invoke("baz"))
require.Equal(t, 1, *callCountP)
})
}
func TestWrappersBindError(t *testing.T) {
wrapTest(t, func(t *testing.T) {
_, _, _, s0s1 := testWrapperFuncs(t)
var tw4Invoke func(s0) s2
require.Error(t, Sequence("tw4", s0s1).Bind(&tw4Invoke, nil))
})
}
func TestEmpties(t *testing.T) {
wrapTest(t, func(t *testing.T) {
//nolint:testifylint
assert.Error(t, Run("no final func"))
//nolint:testifylint
assert.Error(t, Run("no final func", nil))
//nolint:testifylint
assert.NoError(t, Run("no final func", func() {}))
seq := Sequence("empty")
//nolint:testifylint
assert.Error(t, Run("no final func", seq))
//nolint:testifylint
assert.Error(t, Run("no final func", seq, nil))
//nolint:testifylint
assert.NoError(t, Run("no final func", seq, func() {}))
seq2 := seq.Append("nothing")
//nolint:testifylint
assert.Error(t, Run("no final func", seq2))
//nolint:testifylint
assert.Error(t, Run("no final func", seq2, nil))
//nolint:testifylint
assert.NoError(t, Run("no final func", seq2, func() {}))
seq3 := seq.Append("more nothing", Sequence("empty too"))
//nolint:testifylint
assert.Error(t, Run("no final func", seq3))
//nolint:testifylint
assert.Error(t, Run("no final func", seq3, nil))
//nolint:testifylint
assert.NoError(t, Run("no final func", seq3, func() {}))
})
}
func TestAppend(t *testing.T) {
wrapTest(t, func(t *testing.T) {
seq1 := Sequence("one",
Cacheable(func(s s0) s1 {
if s0("s0 value") != s {
panic("s1")
}
return "s1 value"
}),
Cacheable(func(s s1) s2 {
if s1("s1 value") != s {
panic("s2")
}
return "s2 value"
}))
seq2 := Cacheable(Sequence("two",
func(s s3) s4 {
if s3("s3 value") != s {
panic("s4")
}
return "s4 value"
},
func(s s2) s5 {
if s2("s2 value") != s {
panic("s5")
}
return "s5 value"
}))
assert.NoError(t, Run("x", s0("s0 value"),
s3("s3 value"),
s0("s0 value"),
seq1,
seq2,
func(s s5) {
assert.Equal(t, s5("s5 value"), s)
}))
assert.NoError(t, Run("x", s0("s0 value"),
s3("s3 value"),
s0("s0 value"),
seq1.Append("three", seq2),
func(s s5) {
assert.Equal(t, s5("s5 value"), s)
}))
assert.NoError(t, Run("x", s0("s0 value"),
s3("s3 value"),
s0("s0 value"),
Sequence("four", seq1, seq2),
func(s s5) {
assert.Equal(t, s5("s5 value"), s)
}))
})
}
func TestErrorStrings(t *testing.T) {
wrapTest(t, func(t *testing.T) {
invalid := func(int, func()) {}
//nolint:testifylint
assert.NoError(t, Run("one", func() {}))
err := Run("one", invalid, func() {})
//nolint:testifylint
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "one(0) ")
}
err = Run("two", Provide("i-name", invalid), func() {})
//nolint:testifylint
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "two(0)")
assert.Contains(t, err.Error(), "i-name ")
}
err = Run("three", nil, invalid, func() {})
//nolint:testifylint
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "three(1) ")
}
err = Run("four", nil, nil, Cacheable(invalid), func() {})
//nolint:testifylint
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "four(2) ")
}
})
}
func TestVariableImplementsInterfaceLoose(t *testing.T) {
wrapTest(t, func(t *testing.T) {
var i2v i2
var i2vimp i2imp
i2v = i2vimp
assert.NoError(t, Run("x",
Loose(i2v),
func(i2) {}))
})
}
func TestVariableImplementsInterfaceExact(t *testing.T) {
wrapTest(t, func(t *testing.T) {
var i2v i2
var i2vimp i2imp
i2v = i2vimp
assert.Error(t, Run("x",
i2v,
func(i2) {}))
})
}
func TestInjectorsIncluded(t *testing.T) {
wrapTest(t, func(t *testing.T) {
assert.NoError(t, Run("run1",
s3("s3 value"),
s0("s0 value"),
testSeq, func(s s5, d *Debugging) {
assert.Equal(t, []string{
"static static-injector: Debugging [func() *nject.Debugging]",
"literal literal-value: run1(1) [nject.s0]",
"static static-injector: TBF(0) [func(nject.s0) nject.s1]",
"static static-injector: TBF(1) [func(nject.s1) nject.s2]",
"static static-injector: TBF(3) [func(nject.s2) nject.s5]",
"invoke invoke-func: run1 invoke func [*func() error]",
"run fallible-injector: Run()error [func() nject.TerminalError]",
"final final-func: run1(3) [func(nject.s5, *nject.Debugging)]",
}, d.Included)
}))
})
}
func TestInjectorNamesIncluded(t *testing.T) {
wrapTest(t, func(t *testing.T) {
assert.NoError(t, Run("run1",
s3("s3 value"),
Provide("S0", s0("s0 value")),
testSeq, func(s s5, d *Debugging) {
assert.Equal(t, []string{
"Debugging",
"S0",
"TBF(0)",
"TBF(1)",
"TBF(3)",
"run1 invoke func",
"Run()error",
"run1(3)",
}, d.NamesIncluded)
}))
})
}
func TestInjectorsIncludeExclude(t *testing.T) {
wrapTest(t, func(t *testing.T) {
assert.NoError(t, Run("run1",
s3("s3 value"),
s0("s0 value"),
testSeq, func(s s5, d *Debugging) {
assert.Equal(t, []string{
"INCLUDED: static static-injector: Debugging [func() *nject.Debugging] BECAUSE used by final-func: run1(3) [func(nject.s5, *nject.Debugging)] (required)",
"EXCLUDED: literal literal-value: run1(0) [nject.s3] BECAUSE not used by any remaining providers",
"INCLUDED: literal literal-value: run1(1) [nject.s0] BECAUSE used by static-injector: TBF(0) [func(nject.s0) nject.s1] (used by static-injector: TBF(1) [func(nject.s1) nject.s2] (used by static-injector: TBF(3) [func(nject.s2) nject.s5] (used by final-func: run1(3) [func(nject.s5, *nject.Debugging)] (required))))",
"INCLUDED: static static-injector: TBF(0) [func(nject.s0) nject.s1] BECAUSE used by static-injector: TBF(1) [func(nject.s1) nject.s2] (used by static-injector: TBF(3) [func(nject.s2) nject.s5] (used by final-func: run1(3) [func(nject.s5, *nject.Debugging)] (required)))",
"INCLUDED: static static-injector: TBF(1) [func(nject.s1) nject.s2] BECAUSE used by static-injector: TBF(3) [func(nject.s2) nject.s5] (used by final-func: run1(3) [func(nject.s5, *nject.Debugging)] (required))",
"EXCLUDED: static static-injector: TBF(2) [func(nject.s3) nject.s4] BECAUSE not used by any remaining providers",
"INCLUDED: static static-injector: TBF(3) [func(nject.s2) nject.s5] BECAUSE used by final-func: run1(3) [func(nject.s5, *nject.Debugging)] (required)",
"INCLUDED: invoke invoke-func: run1 invoke func [*func() error] BECAUSE required",
"INCLUDED: run fallible-injector: Run()error [func() nject.TerminalError] BECAUSE auto-desired (injector with no outputs)",
"INCLUDED: final final-func: run1(3) [func(nject.s5, *nject.Debugging)] BECAUSE required",
}, d.IncludeExclude)
}))
})
}
func TestInjectorsDebugging(t *testing.T) {
wrapTest(t, func(t *testing.T) {
assert.NoError(t, Run("run1",
s3("s3 value"),
s0("s0 value"),
testSeq, func(s s5, d *Debugging) {
assert.Greater(t, len(d.Trace), 10000, d.Trace)
}))
})
}
func TestInjectorsReproduce(t *testing.T) {
wrapTest(t, func(t *testing.T) {
assert.NoError(t, Run("run1",
s3("s3 value"),
s0("s0 value"),
testSeq, func(s s5, d *Debugging) {
assert.Regexp(t, ` \*Debugging`, d.Reproduce)
assert.NotRegexp(t, `// \*nject\.Debugging`, d.Reproduce)
assert.NotRegexp(t, `\S\t`, d.Reproduce)
assert.NotRegexp(t, `\S `, d.Reproduce)
}))
})
}