-
Notifications
You must be signed in to change notification settings - Fork 9
/
container_struct_arg_test.go
132 lines (123 loc) · 3.01 KB
/
container_struct_arg_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 iocgo
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type FoobarInput struct {
foo Fooer
bar Barer
msg string
}
func NewFoobarWithInput(input *FoobarInput) Foobarer {
return &Foobar{
foo: input.foo,
bar: input.bar,
msg: input.msg,
}
}
func TestContainer_RegisterStructInitArg(t *testing.T) {
log = ""
defer Reset()
input := &FoobarInput{
msg: "studyzy",
}
Register(NewFoobarWithInput, Parameters(map[int]interface{}{0: input}))
Register(func() Fooer { return &Foo{} })
Register(func() Barer { return &Bar{} })
var fb Foobarer
err := Resolve(&fb)
assert.Nil(t, err)
fb.Say(123, "Hello World")
t.Log(log)
assert.True(t, strings.Contains(log, "foo:"))
assert.True(t, strings.Contains(log, "bar:"))
assert.True(t, strings.Contains(log, "studyzy"))
}
func TestContainer_Fill(t *testing.T) {
log = ""
defer Reset()
input := FoobarInput{
msg: "studyzy",
}
Register(func() Fooer { return &Foo{} })
Register(func() Barer { return &Bar{} })
err := Fill(&input)
assert.Nil(t, err)
t.Logf("%#v", input)
assert.NotNil(t, input.foo)
assert.NotNil(t, input.bar)
}
func TestContainer_ResolveStructInitArg(t *testing.T) {
log = ""
defer Reset()
Register(NewFoobarWithInput)
Register(func() Fooer { return &Foo{} })
Register(func() Barer { return &Bar{} })
var fb Foobarer
input := &FoobarInput{
msg: "studyzy",
}
err := Resolve(&fb, Arguments(map[int]interface{}{0: input}))
assert.Nil(t, err)
fb.Say(123, "Hello World")
t.Log(log)
assert.True(t, strings.Contains(log, "foo:"))
assert.True(t, strings.Contains(log, "bar:"))
assert.True(t, strings.Contains(log, "studyzy"))
}
type FoobarInputWithTag struct {
foo Fooer `optional:"true"`
bar Barer `name:"baz"`
msg string
}
func NewFoobarWithInputTag(input *FoobarInputWithTag) Foobarer {
return &Foobar{
foo: input.foo,
bar: input.bar,
msg: input.msg,
}
}
func TestContainer_ResolveStructInitArgOptional(t *testing.T) {
log = ""
defer Reset()
input := &FoobarInputWithTag{
msg: "studyzy",
}
Register(NewFoobarWithInputTag, Parameters(map[int]interface{}{0: input}))
Register(func() Barer { return &Bar{} }, Name("bar"))
Register(func() Barer { return &Baz{} }, Name("baz"))
var fb Foobarer
err := Resolve(&fb)
assert.Nil(t, err)
fb.Say(123, "Hello World")
t.Log(log)
assert.False(t, strings.Contains(log, "foo:"))
assert.True(t, strings.Contains(log, "baz:"))
assert.True(t, strings.Contains(log, "studyzy"))
}
type FoobarInputMultiBar struct {
foo Fooer
bar []Barer
msg string
}
func TestContainer_FillSlice(t *testing.T) {
log = ""
defer Reset()
input := FoobarInputMultiBar{
msg: "studyzy",
}
Register(func() Fooer { return &Foo{} })
Register(func() Barer { return &Bar{} }, Name("bar"))
Register(func() Barer { return &Baz{} }, Name("baz"))
err := Fill(&input)
assert.Nil(t, err)
t.Logf("%#v", input)
assert.NotNil(t, input.foo)
assert.NotNil(t, input.bar)
for _, bar := range input.bar {
bar.Bar("Hi")
}
assert.True(t, strings.Contains(log, "bar:"))
assert.True(t, strings.Contains(log, "baz:"))
}