forked from fgrosse/goldi
/
func_type_test.go
91 lines (75 loc) · 2.34 KB
/
func_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
package goldi_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/fgrosse/goldi"
"net/http"
)
func ExampleNewFuncType() {
container := goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
// define the type
container.Register("my_func", goldi.NewFuncType(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "test" {
w.WriteHeader(http.StatusAccepted)
}
}))
// generate it
result, err := container.Get("my_func")
if err != nil {
return
}
// call it
f := result.(func(name string, age int) (bool, error))
ok, err := f("foo", 42)
if ok != true || err != nil {
panic("!!!")
}
}
// ExampleNewFuncType_ prevents godoc from printing the whole content of this file as example
func ExampleNewFuncType_() {}
var _ = Describe("funcType", func() {
It("should implement the TypeFactory interface", func() {
var factory goldi.TypeFactory
factory = goldi.NewFuncType(SomeFunctionForFuncTypeTest)
// if this compiles the test passes (next expectation only to make compiler happy)
Expect(factory).NotTo(BeNil())
})
Describe("goldi.NewFuncType()", func() {
Context("with invalid argument", func() {
It("should return an invalid type if the argument is no function", func() {
Expect(goldi.IsValid(goldi.NewFuncType(42))).To(BeFalse())
})
})
Context("with argument beeing a function", func() {
It("should create the type", func() {
typeDef := goldi.NewFuncType(SomeFunctionForFuncTypeTest)
Expect(typeDef).NotTo(BeNil())
})
})
})
Describe("Arguments()", func() {
It("should return an empty list", func() {
typeDef := goldi.NewFuncType(SomeFunctionForFuncTypeTest)
Expect(typeDef.Arguments()).NotTo(BeNil())
Expect(typeDef.Arguments()).To(BeEmpty())
})
})
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 just return the function", func() {
typeDef := goldi.NewFuncType(SomeFunctionForFuncTypeTest)
Expect(typeDef.Generate(resolver)).To(BeAssignableToTypeOf(SomeFunctionForFuncTypeTest))
})
})
})
func SomeFunctionForFuncTypeTest(name string, age int) (bool, error) {
return true, nil
}