-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
214 lines (177 loc) · 6.84 KB
/
container_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Copyright 2023 Alexandre Mahdhaoui.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package di_test
import (
"github.com/alexandremahdhaoui/di" //nolint:depguard
. "github.com/onsi/ginkgo/v2" //nolint:depguard
. "github.com/onsi/gomega" //nolint:depguard
"strconv" //nolint:goimports,gofumpt,gci
)
var _ = Describe("Container", func() {
var userDefinedContainerName string
var assignedKey string
var assignedValue string
var assignedPointer *string
var notAssignedKey string
var userDefinedContainer di.Container
var defaultContainer di.Container
Describe("creating a new container", func() {
It("should create a new container", func() {
container := di.New("test-container")
Expect(container).ShouldNot(BeNil())
})
It("should panic", func() {
Expect(func() {
_ = di.New("")
}).To(Panic())
})
})
Describe("setting a value to a container", func() {
It("should successfully set a value", func() {
err := di.Set(di.New("test"), di.NewValue[int]("test", new(int)))
Expect(err).ShouldNot(HaveOccurred())
err = di.Set(di.DefaultContainer, di.NewValue[int]("test", new(int)))
Expect(err).ShouldNot(HaveOccurred())
})
It("should fail setting a value to a built container", func() {
c := di.New("test")
c.Build()
err := di.Set(c, di.NewValue[int]("test", new(int)))
Expect(err).Should(HaveOccurred())
})
})
BeforeEach(func() {
userDefinedContainerName = "user-defined-container"
assignedKey = "assigned-key"
assignedValue = "assigned-value"
assignedPointer = &assignedValue
notAssignedKey = "not-assigned-key"
userDefinedContainer = di.New(userDefinedContainerName)
defaultContainer = di.DefaultContainer // Warn: this container will not be reset throughout the code
err := di.Set(userDefinedContainer, di.NewValue(assignedKey, assignedPointer))
if err != nil {
panic(err)
}
})
Describe("initializing values in an immature container", func() {
It("should initialize values", func() {
for _, c := range []di.Container{userDefinedContainer} {
for i := 0; i < 10; i++ {
di.MustWithOptions[int](c, strconv.Itoa(i), di.InitializeOption).MustSet(i)
}
}
})
})
Describe("accessing an immature container", func() {
Context("with Get", func() {
It("should successfully return a value", func() {
v, err := di.Get[string](userDefinedContainer, assignedKey) //nolint:nolintlint,varnamelen
Expect(err).ShouldNot(HaveOccurred())
Expect(v.MustPtr()).To(BeIdenticalTo(assignedPointer))
Expect(v.MustValue()).To(BeIdenticalTo(assignedValue))
})
})
Context("with Must", func() {
It("should successfully return a value", func() {
Expect(func() { di.Must[string](userDefinedContainer, assignedKey) }).ShouldNot(Panic())
Expect(di.Must[string](userDefinedContainer, assignedKey).MustPtr()).To(BeIdenticalTo(assignedPointer))
Expect(di.Must[string](userDefinedContainer, assignedKey).MustValue()).To(BeIdenticalTo(assignedValue))
})
})
Context("with MustWithOptions", func() {
It("should successfully return a value", func() {
Expect(func() { di.MustWithOptions[string](userDefinedContainer, assignedKey) }).ShouldNot(Panic())
Expect(di.MustWithOptions[string](userDefinedContainer, assignedKey).MustPtr()).
To(BeIdenticalTo(assignedPointer))
Expect(di.MustWithOptions[string](userDefinedContainer, assignedKey).MustValue()).
To(BeIdenticalTo(assignedValue))
})
})
})
Describe("building a new container", func() {
BeforeEach(func() {
for _, c := range []di.Container{userDefinedContainer, defaultContainer} {
for i := 0; i < 10; i++ {
di.MustWithOptions[int](c, strconv.Itoa(i), di.InitializeOption).MustSet(i)
}
}
})
It("should build non empty container", func() {
Expect(func() { userDefinedContainer.Build() }).NotTo(Panic())
Expect(func() { defaultContainer.Build() }).NotTo(Panic())
})
})
Describe("initializing values in a built container", func() {
BeforeEach(func() {
userDefinedContainer.Build()
})
Context("with Set", func() {
It("should fail", func() {
err := di.Set(userDefinedContainer, di.NewValue(assignedKey, assignedPointer))
Expect(err).Should(HaveOccurred())
err = di.Set(defaultContainer, di.NewValue(assignedKey, assignedPointer))
Expect(err).Should(HaveOccurred())
})
})
Context("with MustWithOptions and InitializeOption", func() {
It("should panic", func() {
Expect(func() {
di.MustWithOptions[int](userDefinedContainer, "should-panic", di.InitializeOption).MustSet(0)
}).To(Panic())
})
})
})
Describe("accessing a built container's value", func() {
BeforeEach(func() {
userDefinedContainer.Build()
})
Context("with Get", func() {
It("should successfully return a value", func() {
v, err := di.Get[string](userDefinedContainer, assignedKey)
Expect(err).ShouldNot(HaveOccurred())
Expect(v.MustValue()).Should(BeIdenticalTo(assignedValue))
Expect(v.MustPtr()).Should(Equal(assignedPointer))
// Should not be an identical pointer
Expect(v.MustPtr()).ShouldNot(BeIdenticalTo(assignedPointer))
})
It("should fail to return a value", func() {
_, err := di.Get[int](defaultContainer, notAssignedKey)
Expect(err).Should(HaveOccurred())
_, err = di.Get[int](userDefinedContainer, notAssignedKey)
Expect(err).Should(HaveOccurred())
})
})
Context("with Must", func() {
It("should successfully return a value", func() {
v := di.Must[string](userDefinedContainer, assignedKey)
Expect(v.MustValue()).Should(BeIdenticalTo(assignedValue))
Expect(v.MustPtr()).Should(Equal(assignedPointer))
Expect(v.MustPtr()).ShouldNot(BeIdenticalTo(assignedPointer))
})
It("should panic retrieving an unassigned value", func() {
Expect(func() { di.Must[string](userDefinedContainer, notAssignedKey) }).To(Panic())
Expect(func() { di.Must[string](defaultContainer, notAssignedKey) }).To(Panic())
})
})
Context("with MustWithOptions", func() {
It("should successfully return a value", func() {
Expect(di.MustWithOptions[string](userDefinedContainer, assignedKey).MustValue()).
To(Equal(assignedValue))
})
It("should panic on not assigned values", func() {
Expect(func() { di.MustWithOptions[string](userDefinedContainer, notAssignedKey) }).To(Panic())
Expect(func() { di.MustWithOptions[string](defaultContainer, notAssignedKey) }).To(Panic())
})
})
})
})