-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject_test.go
135 lines (105 loc) · 3.12 KB
/
inject_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
package httputil
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type SpecialString interface {
}
type TestStruct struct {
Dep1 string `inject:"required" json:"dep1"`
Dep2 SpecialString `inject:"-"`
// 重复Dep1类型, 无法实现应该像Dep2那样自定义一个类型
Dep3 string
}
type Greeter struct {
Name string
}
func (g *Greeter) String() string {
return "Hello, My name is" + g.Name
}
func Test_InjectorInvoke(t *testing.T) {
inj := NewInjector()
assert.NotNil(t, inj)
// 映射字符串
dep1 := "some dependency"
inj.Map(dep1)
// 映射接口
dep2 := "another dep"
inj.MapTo(dep2, (*SpecialString)(nil))
// 映射有方向的通道
dep3 := make(chan *SpecialString)
dep4 := make(chan *SpecialString)
typRecv := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(dep3).Elem())
typSend := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(dep4).Elem())
inj.Set(typRecv, reflect.ValueOf(dep3))
inj.Set(typSend, reflect.ValueOf(dep4))
// 映射双向通道
dep5 := make(chan *SpecialString)
inj.Map(dep5)
rvs, err := inj.Invoke(
func(d1 string, d2 SpecialString, d3 <-chan *SpecialString, d4 chan<- *SpecialString, d5 chan *SpecialString) {
assert.Equal(t, d1, dep1)
assert.Equal(t, d2, dep2)
assert.Equal(t, reflect.TypeOf(d3).Elem(), reflect.TypeOf(dep3).Elem())
assert.Equal(t, reflect.TypeOf(d4).Elem(), reflect.TypeOf(dep4).Elem())
assert.Equal(t, reflect.TypeOf(d3).ChanDir(), reflect.RecvDir)
assert.Equal(t, reflect.TypeOf(d4).ChanDir(), reflect.SendDir)
})
assert.NoError(t, err)
assert.NoError(t, CheckError(rvs))
}
func Test_InjectorInvokeReturnValues(t *testing.T) {
inj := NewInjector()
assert.NotNil(t, inj)
rvs, err := inj.Invoke(func() (string, bool) {
return "test", false
})
assert.NoError(t, err)
assert.Len(t, rvs, 2)
assert.Equal(t, "test", rvs[0].String())
assert.Equal(t, false, rvs[1].Bool())
}
func Test_InjectorApply(t *testing.T) {
inj := NewInjector()
assert.NotNil(t, inj)
inj.Map("dep1").MapTo("dep2", (*SpecialString)(nil))
s1 := TestStruct{}
err := inj.Apply(&s1)
assert.NoError(t, err)
assert.Equal(t, s1.Dep1, "dep1")
assert.Equal(t, s1.Dep2, "dep2")
assert.Equal(t, s1.Dep3, "")
inj = NewInjector()
assert.NotNil(t, inj)
inj.MapTo("dep2", (*SpecialString)(nil))
s2 := TestStruct{}
err = inj.Apply(&s2)
assert.Error(t, err)
}
func Test_InterfaceOf(t *testing.T) {
iType := InterfaceOf((*SpecialString)(nil))
assert.Equal(t, iType.Kind(), reflect.Interface)
iType = InterfaceOf((**SpecialString)(nil))
assert.Equal(t, iType.Kind(), reflect.Interface)
// Expecting nil
defer func() {
rec := recover()
assert.NotNil(t, rec)
}()
iType = InterfaceOf((*testing.T)(nil))
}
func Test_InjectorSetParent(t *testing.T) {
injParent := NewInjector()
injParent.MapTo("another dep", (*SpecialString)(nil))
inj := NewInjector()
inj.SetParent(injParent)
assert.Equal(t, inj.Get(InterfaceOf((*SpecialString)(nil))).IsValid(), true)
}
func Test_InjectImplementors(t *testing.T) {
inj := NewInjector()
g := &Greeter{"Jeremy"}
inj.Map(g)
assert.Equal(t, inj.Get(InterfaceOf((*fmt.Stringer)(nil))).IsValid(), true)
}