-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.go
98 lines (78 loc) · 1.71 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
package shaft_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/aegistudio/shaft"
)
type I interface {
invoke(*[]string)
}
type A struct {
}
func (A) invoke(events *[]string) {
*events = append(*events, "invoke a")
}
func provideObjectA(events *[]string, _ *D) []I {
*events = append(*events, "provide a")
return []I{&A{}}
}
type B struct {
counter int
}
func (b B) invoke(events *[]string) {
*events = append(*events,
fmt.Sprintf("invoke b %d", b.counter))
}
func stackObjectB(f func(*B, []I) error, events *[]string) error {
b := &B{}
*events = append(*events, "stack b")
defer func() { *events = append(*events, "defer b") }()
return f(b, []I{b})
}
type C struct {
}
func redundantObjectC(events *[]string) (*C, error) {
*events = append(*events, "provide c")
return &C{}, nil
}
type D struct {
}
func (D) invoke(events *[]string) {
*events = append(*events, "invoke d")
}
func decorateObjectD(events *[]string, b *B, val int) (*B, *D, []I) {
*events = append(*events, "provide d")
b.counter = val
d := &D{}
return b, d, []I{d}
}
func TestStandard(t *testing.T) {
assert := assert.New(t)
var events []string
assert.NoError(shaft.Run(
shaft.Provide(provideObjectA),
shaft.Stack(stackObjectB),
shaft.Provide(redundantObjectC),
shaft.Provide(decorateObjectD),
shaft.Module(
shaft.Supply(&events),
shaft.Supply(int(123456)),
shaft.Invoke(func(inputs []I, events *[]string) {
for _, input := range inputs {
input.invoke(events)
}
}),
),
))
assert.Equal(events, []string{
"stack b",
"provide d",
"provide a",
// XXX: group are collected in the same order of provision.
"invoke a",
"invoke b 123456",
"invoke d",
"defer b",
})
}