-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_bind_test.go
132 lines (121 loc) · 2.6 KB
/
example_bind_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
package nject_test
import (
"fmt"
"github.com/muir/nject"
)
// Bind does as much work before invoke as possible.
func ExampleCollection_Bind() {
providerChain := nject.Sequence("example sequence",
func(s string) int {
return len(s)
},
func(i int, s string) {
fmt.Println(s, i)
})
var aInit func(string)
var aInvoke func()
providerChain.Bind(&aInvoke, &aInit)
aInit("string comes from init")
aInit("ignored since invoke is done")
aInvoke()
aInvoke()
var bInvoke func(string)
providerChain.Bind(&bInvoke, nil)
bInvoke("string comes from invoke")
bInvoke("not a constant")
// Output: string comes from init 22
// string comes from init 22
// string comes from invoke 24
// not a constant 14
}
// Parameters can be passed to both the init and then
// invoke functions when using Bind.
func ExampleCollection_Bind_passing_in_parameters() {
chain := nject.Sequence("example",
nject.Provide("static-injector",
// This will be a static injector because its input
// will come from the bind init function
func(s string) int {
return len(s)
}),
nject.Provide("regular-injector",
// This will be a regular injector because its input
// will come from the bind invoke function
func(i int32) int64 {
return int64(i)
}),
nject.Provide("final-injector",
// This will be the last injector in the chain and thus
// is the final injector and it must be included
func(i int64, j int) int32 {
fmt.Println(i, j)
return int32(i) + int32(j)
}),
)
var initFunc func(string)
var invokeFunc func(int32) int32
fmt.Println(chain.Bind(&invokeFunc, &initFunc))
initFunc("example thirty-seven character string")
fmt.Println(invokeFunc(10))
// Output: <nil>
// 10 37
// 47
}
func ExampleMustBindSimple() {
f := nject.MustBindSimple(
nject.Sequence("example",
func() int {
return 7
},
func(i int) {
fmt.Println(i)
},
), "bind-name")
f()
f()
// Output: 7
// 7
}
func ExampleCollection_MustBindSimple() {
f := nject.Sequence("example",
func() int {
return 7
},
func(i int) {
fmt.Println(i)
},
).MustBindSimple()
f()
f()
// Output: 7
// 7
}
func ExampleMustBindSimpleError() {
f := nject.MustBindSimpleError(
nject.Sequence("example",
func() int {
return 7
},
func(i int) error {
fmt.Println(i)
return nil
},
), "bind-name")
fmt.Println(f())
// Output: 7
// <nil>
}
func ExampleCollection_MustBindSimpleError() {
f := nject.Sequence("example",
func() int {
return 7
},
func(i int) error {
fmt.Println(i)
return nil
},
).MustBindSimpleError()
fmt.Println(f())
// Output: 7
// <nil>
}