-
Notifications
You must be signed in to change notification settings - Fork 8
/
parameter_resolver_test.go
163 lines (134 loc) · 5.44 KB
/
parameter_resolver_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
package goldi_test
import (
"reflect"
"github.com/fgrosse/goldi"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("ParameterResolver", func() {
var (
config map[string]interface{}
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
config = map[string]interface{}{}
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
It("should return static parameters", func() {
parameter := reflect.ValueOf(42)
expectedType := parameter.Type()
Expect(resolver.Resolve(parameter, expectedType)).To(Equal(parameter))
})
Context("with dynamic parameters", func() {
Context("with invalid parameter name", func() {
It("should not try to resolve `%`", func() {
parameter := reflect.ValueOf("%")
result, err := resolver.Resolve(parameter, parameter.Type())
Expect(err).NotTo(HaveOccurred())
Expect(result.Interface()).To(Equal("%"))
})
It("should not try to resolve `%%`", func() {
parameter := reflect.ValueOf("%%")
result, err := resolver.Resolve(parameter, parameter.Type())
Expect(err).NotTo(HaveOccurred())
Expect(result.Interface()).To(Equal("%%"))
})
})
Context("when parameter has not been defined", func() {
It("should return the parameter as is", func() {
parameter := reflect.ValueOf("%foo%")
expectedType := parameter.Type()
result, err := resolver.Resolve(parameter, expectedType)
Expect(err).NotTo(HaveOccurred())
Expect(result.Interface()).To(Equal("%foo%"))
})
})
Context("when the parameter has been defined", func() {
It("should resolve string parameters using the configuration", func() {
config["foo"] = "success"
parameter := reflect.ValueOf("%foo%")
expectedType := reflect.TypeOf(config["foo"])
result, err := resolver.Resolve(parameter, expectedType)
Expect(err).NotTo(HaveOccurred())
Expect(result.Interface()).To(Equal(config["foo"]))
})
It("should resolve float parameters using the configuration", func() {
config["bar"] = 3.1415
parameter := reflect.ValueOf("%bar%")
expectedType := reflect.TypeOf(config["bar"])
result, err := resolver.Resolve(parameter, expectedType)
Expect(err).NotTo(HaveOccurred())
Expect(result.Interface()).To(Equal(config["bar"]))
})
})
})
Context("with type references", func() {
Context("when the type has been registered", func() {
BeforeEach(func() {
container.RegisterType("foo", NewFoo)
})
Context("when the type is assignable to the expected type", func() {
It("should generate the type and return it", func() {
parameter := reflect.ValueOf("@foo")
expectedType := reflect.TypeOf(NewFoo())
result, err := resolver.Resolve(parameter, expectedType)
Expect(err).NotTo(HaveOccurred())
Expect(result.Interface()).To(BeAssignableToTypeOf(NewFoo()))
})
})
Context("when the type is not assignable to the expected type", func() {
It("should return an error", func() {
parameter := reflect.ValueOf("@foo")
expectedType := reflect.TypeOf(NewBar())
result, err := resolver.Resolve(parameter, expectedType)
Expect(result.IsValid()).To(BeFalse())
Expect(err).To(MatchError(`the referenced type "@foo" (type *goldi_test.Foo) is not assignable to the expected type *goldi_test.Bar`))
Expect(err.(goldi.TypeReferenceError).TypeID).To(Equal("foo"))
})
})
Context("when a func reference type is requested", func() {
It("should generate the type and return the function", func() {
foo := &Foo{Value: "Success!"}
container.InjectInstance("foo", foo)
parameter := reflect.ValueOf("@foo::ReturnString")
expectedType := reflect.TypeOf(func(string) string { return "" })
result, err := resolver.Resolve(parameter, expectedType)
Expect(err).NotTo(HaveOccurred())
Expect(result.Interface()).To(BeAssignableToTypeOf(foo.ReturnString))
Expect(result.Interface().(func(string) string)("YEAH")).To(Equal("Success! YEAH"))
})
It("should return an error if the method does not exist", func() {
container.InjectInstance("foo", new(Foo))
parameter := reflect.ValueOf("@foo::ThisMethodDoesNotExist")
expectedType := reflect.TypeOf(func() {})
result, err := resolver.Resolve(parameter, expectedType)
Expect(result.IsValid()).To(BeFalse())
Expect(err).To(MatchError(`the referenced method "@foo::ThisMethodDoesNotExist" does not exist or is not exported`))
})
})
})
Context("when the type has not been registered", func() {
It("should return an error", func() {
parameter := reflect.ValueOf("@foo")
expectedType := reflect.TypeOf(&Foo{})
result, err := resolver.Resolve(parameter, expectedType)
Expect(err).To(HaveOccurred())
Expect(result.IsValid()).To(BeFalse())
Expect(err).To(MatchError(`the referenced type "@foo" has not been defined`))
Expect(err.(goldi.UnknownTypeReferenceError).TypeID).To(Equal("foo"))
})
})
Context("when the type has is invalid", func() {
It("should return an error", func() {
parameter := reflect.ValueOf("@foo")
expectedType := reflect.TypeOf(&Foo{})
container.Register("foo", goldi.NewType(nil)) // foo will be invalid
result, err := resolver.Resolve(parameter, expectedType)
Expect(result).To(Equal(reflect.Zero(expectedType)))
Expect(err).To(MatchError(`goldi: error while generating type "foo": the given factoryFunction is nil`))
})
})
})
})