-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_collection_test.go
95 lines (88 loc) · 1.7 KB
/
example_collection_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
package nject_test
import (
"fmt"
"github.com/muir/nject"
)
func ExampleSequence() {
seq := nject.Sequence("example",
func() string {
return "foo"
},
func(s string) {
fmt.Println(s)
},
)
nject.Run("run", seq)
// Output: foo
}
func ExampleCollection_Append() {
one := nject.Sequence("first sequence",
func() string {
return "foo"
},
func(s string) error {
fmt.Println("from one,", s)
// the return value means this provider isn't
// automatically desired
return nil
},
)
two := one.Append("second sequence",
nject.Sequence("third sequence",
func() int {
return 3
},
),
func(s string, i int) {
fmt.Println("from two,", s, i)
},
)
fmt.Println(nject.Run("one", one))
fmt.Println(nject.Run("two", two))
// Output: from one, foo
// <nil>
// from two, foo 3
// <nil>
}
func ExampleCollection_String() {
one := nject.Sequence("sequence",
func() string {
return "foo"
},
func(s string) error {
fmt.Println("from one,", s)
// the return value means this provider isn't
// automatically desired
return nil
},
)
fmt.Println(one)
// Output: sequence:
// func() string
// func(string) error
}
func ExampleCollection_Condense() {
var counter int
one := nject.Required(nject.Sequence("one",
func() int { counter++; return counter },
func(b bool) string {
return map[bool]string{
true: "t",
false: "f",
}[b]
},
func(s string, i int) string {
return fmt.Sprintf("%s-%d", s, i)
}).MustCondense(false))
fmt.Println(nject.Run("t",
func() bool { return true },
one,
func(s string) { fmt.Println(s) },
func() bool { return false },
one,
func(s string) { fmt.Println(s) },
))
// Output: t-1
// f-2
// <nil>
}