-
Notifications
You must be signed in to change notification settings - Fork 8
/
struct_type_test.go
180 lines (150 loc) · 6.18 KB
/
struct_type_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package goldi_test
import (
"fmt"
"github.com/fgrosse/goldi"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func ExampleNewStructType() {
container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
// all of the following types are semantically identical
container.Register("foo_1", goldi.NewStructType(Foo{}))
container.Register("foo_2", goldi.NewStructType(&Foo{}))
container.Register("foo_3", goldi.NewStructType(new(Foo)))
// each reference to the "logger" type will now be resolved to that instance
fmt.Printf("foo_1: %T\n", container.MustGet("foo_1"))
fmt.Printf("foo_2: %T\n", container.MustGet("foo_2"))
fmt.Printf("foo_3: %T\n", container.MustGet("foo_3"))
// Output:
// foo_1: *goldi_test.Foo
// foo_2: *goldi_test.Foo
// foo_3: *goldi_test.Foo
}
// ExampleNewStructType_ prevents godoc from printing the whole content of this file as example
func ExampleNewStructType_() {}
var _ = Describe("structType", func() {
It("should implement the TypeFactory interface", func() {
var factory goldi.TypeFactory
factory = goldi.NewStructType(MockType{})
// if this compiles the test passes (next expectation only to make compiler happy)
Expect(factory).NotTo(BeNil())
})
Describe("goldi.NewStructType()", func() {
Context("with invalid arguments", func() {
It("should return an invalid type if the generator is no struct or pointer to a struct", func() {
Expect(goldi.IsValid(goldi.NewStructType(42))).To(BeFalse())
})
It("should return an invalid type if the generator is a pointer to something other than a struct", func() {
something := "Hello Pointer World!"
Expect(goldi.IsValid(goldi.NewStructType(&something))).To(BeFalse())
})
})
Context("with first argument beeing a struct", func() {
It("should create the type", func() {
typeDef := goldi.NewStructType(MockType{})
Expect(typeDef).NotTo(BeNil())
})
})
Context("with first argument beeing a pointer to struct", func() {
It("should create the type", func() {
typeDef := goldi.NewStructType(&MockType{})
Expect(typeDef).NotTo(BeNil())
})
})
It("should return an invalid type if more factory arguments were provided than the struct has fields", func() {
t := goldi.NewStructType(&MockType{}, "foo", true, "bar")
Expect(goldi.IsValid(t)).To(BeFalse())
Expect(t).To(MatchError("the struct MockType has only 2 fields but 3 arguments where provided"))
})
})
Describe("Arguments()", func() {
It("should return all factory arguments", func() {
args := []interface{}{"foo", true}
typeDef := goldi.NewStructType(MockType{}, args...)
Expect(typeDef.Arguments()).To(Equal(args))
})
})
Describe("Generate()", func() {
var (
config = map[string]interface{}{}
container *goldi.Container
resolver *goldi.ParameterResolver
)
BeforeEach(func() {
container = goldi.NewContainer(goldi.NewTypeRegistry(), config)
resolver = goldi.NewParameterResolver(container)
})
Context("without struct arguments", func() {
Context("when the factory is a struct (no pointer)", func() {
It("should generate the type", func() {
typeDef := goldi.NewStructType(MockType{})
Expect(typeDef.Generate(resolver)).To(BeAssignableToTypeOf(&MockType{}))
})
})
It("should generate the type", func() {
typeDef := goldi.NewStructType(&MockType{})
Expect(typeDef.Generate(resolver)).To(BeAssignableToTypeOf(&MockType{}))
})
It("should generate a new type each time", func() {
typeDef := goldi.NewStructType(&MockType{})
t1, err1 := typeDef.Generate(resolver)
t2, err2 := typeDef.Generate(resolver)
Expect(err1).NotTo(HaveOccurred())
Expect(err2).NotTo(HaveOccurred())
Expect(t1).NotTo(BeNil())
Expect(t2).NotTo(BeNil())
Expect(t1 == t2).To(BeFalse(), fmt.Sprintf("t1 (%p) should not point to the same instance as t2 (%p)", t1, t2))
// Just to make the whole issue more explicit:
t1Mock := t1.(*MockType)
t2Mock := t2.(*MockType)
t1Mock.StringParameter = "CHANGED"
Expect(t2Mock.StringParameter).NotTo(Equal(t1Mock.StringParameter),
"Changing two indipendently generated types should not affect both at the same time",
)
})
})
Context("with one or more arguments", func() {
It("should generate the type", func() {
typeDef := goldi.NewStructType(&MockType{}, "foo", true)
generatedType, err := typeDef.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
Expect(generatedType).To(BeAssignableToTypeOf(&MockType{}))
generatedMock := generatedType.(*MockType)
Expect(generatedMock.StringParameter).To(Equal("foo"))
Expect(generatedMock.BoolParameter).To(Equal(true))
})
It("should use the given parameters", func() {
typeDef := goldi.NewStructType(&MockType{}, "%param1%", "%param2%")
config["param1"] = "TEST"
config["param2"] = true
generatedType, err := typeDef.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
Expect(generatedType).To(BeAssignableToTypeOf(&MockType{}))
generatedMock := generatedType.(*MockType)
Expect(generatedMock.StringParameter).To(Equal("TEST"))
Expect(generatedMock.BoolParameter).To(Equal(true))
})
Context("when a type reference is given", func() {
Context("and its type matches the struct field type", func() {
It("should generate the type", func() {
container.RegisterType("foo", NewMockType)
typeDef := goldi.NewStructType(TypeForServiceInjection{}, "@foo")
generatedType, err := typeDef.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
Expect(generatedType).To(BeAssignableToTypeOf(&TypeForServiceInjection{}))
generatedMock := generatedType.(*TypeForServiceInjection)
Expect(generatedMock.InjectedType).To(BeAssignableToTypeOf(&MockType{}))
})
})
Context("and its type does not match the function signature", func() {
It("should return an error", func() {
container.RegisterType("foo", NewFoo)
typeDef := goldi.NewStructType(TypeForServiceInjection{}, "@foo")
_, err := typeDef.Generate(resolver)
Expect(err).To(MatchError(`the referenced type "@foo" (type *goldi_test.Foo) can not be used as field 1 for struct type goldi_test.TypeForServiceInjection`))
})
})
})
})
})
})