forked from fgrosse/goldi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_type_test.go
69 lines (55 loc) · 2.01 KB
/
instance_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
package goldi_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"fmt"
"github.com/fgrosse/goldi"
)
func ExampleNewInstanceType() {
container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
myInstance := new(SimpleLogger)
myInstance.Name = "Foobar" // you can configure the instance in your code
// now register this instance as a type
container.Register("logger", goldi.NewInstanceType(myInstance))
// each reference to the "logger" type will now be resolved to that instance
fmt.Println(container.MustGet("logger").(*SimpleLogger).Name)
// Output:
// Foobar
}
// ExampleNewInstanceType_ prevents godoc from printing the whole content of this file as example
func ExampleNewInstanceType_() {}
var _ = Describe("instanceType", func() {
var resolver *goldi.ParameterResolver
BeforeEach(func() {
container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
resolver = goldi.NewParameterResolver(container)
})
It("should return an invalid type if NewInstanceType is called with nil", func() {
Expect(goldi.IsValid(goldi.NewInstanceType(nil))).To(BeFalse())
})
Describe("Arguments()", func() {
It("should return an empty list", func() {
typeDef := goldi.NewInstanceType(NewFoo())
Expect(typeDef.Arguments()).To(BeEmpty())
})
})
Describe("Generate", func() {
It("should always return the given instance", func() {
instance := NewFoo()
factory := goldi.NewInstanceType(instance)
for i := 0; i < 3; i++ {
generateResult, err := factory.Generate(resolver)
Expect(err).NotTo(HaveOccurred())
Expect(generateResult == instance).To(BeTrue(),
fmt.Sprintf("generateResult (%p) should point to the same instance as instance (%p)", generateResult, instance),
)
}
})
})
It("should implement the TypeFactory interface", func() {
var factory goldi.TypeFactory
factory = goldi.NewInstanceType("foo")
// if this compiles the test passes (next expectation only to make compiler happy)
Expect(factory).NotTo(BeNil())
})
})