-
Notifications
You must be signed in to change notification settings - Fork 8
/
type_configurator_test.go
100 lines (79 loc) · 3.33 KB
/
type_configurator_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
package goldi_test
import (
"fmt"
"github.com/fgrosse/goldi"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("TypeConfigurator", func() {
var container *goldi.Container
BeforeEach(func() {
container = goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
})
Describe("Configure", func() {
Context("when the type configurator has not been defined correctly", func() {
It("should return an error", func() {
someType := new(Foo)
configurator := goldi.NewTypeConfigurator("configurator", "Configure")
Expect(configurator.Configure(someType, container)).To(MatchError(`the configurator type "@configurator" has not been defined`))
})
})
Context("when the type configurator is no struct or pointer to struct", func() {
It("should return an error", func() {
container.InjectInstance("configurator", 42)
someType := new(Foo)
configurator := goldi.NewTypeConfigurator("configurator", "Configure")
Expect(configurator.Configure(someType, container)).To(MatchError("the configurator instance is no struct or pointer to struct but a int"))
})
})
Context("when the type configurator method does not exist", func() {
It("should return an error", func() {
someType := new(Foo)
configurator := goldi.NewTypeConfigurator("configurator", "Fooobar")
container.Register("configurator", goldi.NewInstanceType(&MyConfigurator{}))
Expect(configurator.Configure(someType, container)).To(MatchError(`the configurator does not have a method "Fooobar"`))
})
})
Context("when the type configurator has been defined properly", func() {
var configurator *MyConfigurator
BeforeEach(func() {
configurator = &MyConfigurator{ConfiguredValue: "success!"}
container.Register("configurator", goldi.NewInstanceType(configurator))
})
It("should return an error if the first argument is nil", func() {
configuratorType := goldi.NewTypeConfigurator("configurator", "Configure")
Expect(configuratorType.Configure(nil, container)).To(MatchError("can not configure nil"))
})
It("should call the requested function on the configurator", func() {
someType := new(Foo)
configuratorType := goldi.NewTypeConfigurator("configurator", "Configure")
Expect(someType.Value).NotTo(Equal("success!"))
Expect(configuratorType.Configure(someType, container)).To(Succeed())
Expect(someType.Value).To(Equal("success!"))
})
It("should return an error if the configurator returned an error", func() {
configurator.ReturnError = true
someType := new(Foo)
configuratorType := goldi.NewTypeConfigurator("configurator", "Configure")
Expect(configuratorType.Configure(someType, container)).To(MatchError("this is the error message from the tests.MockTypeConfigurator"))
})
It("should return nil if the configurator returned nil", func() {
configurator.ReturnError = false
someType := new(Foo)
configuratorType := goldi.NewTypeConfigurator("configurator", "Configure")
Expect(configuratorType.Configure(someType, container)).To(BeNil())
})
})
})
})
type MyConfigurator struct {
ConfiguredValue string
ReturnError bool
}
func (c *MyConfigurator) Configure(f *Foo) error {
if c.ReturnError {
return fmt.Errorf("this is the error message from the tests.MockTypeConfigurator")
}
f.Value = c.ConfiguredValue
return nil
}