-
Notifications
You must be signed in to change notification settings - Fork 8
/
configured_type_test.go
128 lines (105 loc) · 4.94 KB
/
configured_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
package goldi_test
import (
"fmt"
"github.com/fgrosse/goldi"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func ExampleNewConfiguredType() {
container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
// this example configurator accepts a Foo type and will set its Value field to the given value
configurator := &MyConfigurator{ConfiguredValue: "success!"}
// register the configurator under a type ID
container.Register("configurator_type", goldi.NewInstanceType(configurator))
// create the type that should be configured
embeddedType := goldi.NewStructType(Foo{})
container.Register("foo", goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure"))
fmt.Println(container.MustGet("foo").(*Foo).Value)
// Output:
// success!
}
// ExampleNewConfiguredType_ prevents godoc from printing the whole content of this file as example
func ExampleNewConfiguredType_() {}
var _ = Describe("configuredType", func() {
var embeddedType goldi.TypeFactory
BeforeEach(func() {
embeddedType = goldi.NewStructType(Foo{})
})
It("should implement the TypeFactory interface", func() {
var factory goldi.TypeFactory
factory = goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
// if this compiles the test passes (next expectation only to make compiler happy)
Expect(factory).NotTo(BeNil())
})
Describe("NewConfiguredType()", func() {
Context("with invalid argument", func() {
It("should return an invalid type if the embedded type is nil", func() {
typeDef := goldi.NewConfiguredType(nil, "configurator_type", "Configure")
Expect(goldi.IsValid(typeDef)).To(BeFalse())
})
It("should return an invalid type if either the configurator ID or method is empty", func() {
Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "", ""))).To(BeFalse())
Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "configurator_type", ""))).To(BeFalse())
Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "", "configure"))).To(BeFalse())
})
It("should return an invalid type if the configurator method is not exported", func() {
Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "configurator_type", "configure"))).To(BeFalse())
})
})
Context("with valid arguments", func() {
It("should create the type", func() {
typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
Expect(typeDef).NotTo(BeNil())
})
})
})
Describe("Arguments()", func() {
It("should return the arguments of the embedded type and the configurator as type ID", func() {
embeddedType = goldi.NewStructType(Foo{}, "%param_of_embedded%", "another param")
typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
Expect(typeDef.Arguments()).NotTo(BeNil())
Expect(typeDef.Arguments()).To(HaveLen(3))
Expect(typeDef.Arguments()).To(ContainElement("%param_of_embedded%"))
Expect(typeDef.Arguments()).To(ContainElement("another param"))
Expect(typeDef.Arguments()).To(ContainElement("@configurator_type"))
})
})
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)
})
It("should get the embedded type and configurator and configure it", func() {
typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
configurator := &MyConfigurator{ConfiguredValue: "success!"}
container.Register("configurator_type", goldi.NewInstanceType(configurator))
generatedType, err := typeDef.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
Expect(generatedType).NotTo(BeNil())
Expect(generatedType).To(BeAssignableToTypeOf(&Foo{}))
Expect(generatedType.(*Foo).Value).To(Equal("success!"))
})
It("should return an error if the embedded type can not be generated", func() {
invalidType := goldi.NewStructType(nil)
typeDef := goldi.NewConfiguredType(invalidType, "configurator_type", "Configure")
configurator := &MyConfigurator{ConfiguredValue: "should not happen"}
container.Register("configurator_type", goldi.NewInstanceType(configurator))
generatedType, err := typeDef.Generate(resolver)
Expect(err).To(MatchError("can not generate configured type: the given struct is nil"))
Expect(generatedType).To(BeNil())
})
It("should return an error if the configurator returns an error", func() {
typeDef := goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
configurator := &MyConfigurator{ReturnError: true}
container.Register("configurator_type", goldi.NewInstanceType(configurator))
generatedType, err := typeDef.Generate(resolver)
Expect(err).To(MatchError("can not configure type: this is the error message from the tests.MockTypeConfigurator"))
Expect(generatedType).To(BeNil())
})
})
})