-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdi_test.go
122 lines (90 loc) · 1.97 KB
/
di_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
package di
import (
"encoding/json"
"testing"
)
func TestStructBindName(t *testing.T) {
type BarService interface{}
type JojoService interface{}
type FooBase struct {
JojoService JojoService `di:"JojoService"`
}
type Foo struct {
FooBase `di:"embed"`
BarService BarService `di:"BarService"`
}
target := &Foo{}
target.FooBase.JojoService = 1
r := NewRegistry()
r.Register("BarService", struct{}{})
r.Register("JojoService", struct{}{})
b := NewBinder(target, r)
if err := b.Bind(); err != nil {
t.Fatal(err)
}
t.Log(target)
}
func TestStructBindType(t *testing.T) {
type BarService interface{}
type JojoService interface{}
type FooBase struct {
JojoService int `di:"*"`
}
type Foo struct {
FooBase `di:"embed"`
BarService string `di:"*"`
}
target := &Foo{}
target.FooBase.JojoService = 1
r := NewRegistry()
r.Register("whateverInt", 1)
r.Register("whateverString", "test")
b := NewBinder(target, r)
if err := b.Bind(); err != nil {
t.Fatal(err)
}
t.Log(target)
}
func TestStructBindFunc(t *testing.T) {
type BarService interface{}
type JojoService interface{}
type FooBase struct {
JojoService int `di:"*"`
}
type Foo struct {
FooBase `di:"embed"`
BarService string `di:"*"`
}
target := &Foo{}
target.FooBase.JojoService = 1
r := NewRegistry()
r.Register("whateverInt", func(string) int { return 1 })
r.Register("whateverString", func() string { return "test" })
b := NewBinder(target, r)
if err := b.Bind(); err != nil {
t.Fatal(err)
}
t.Log(target)
}
func TestBind(t *testing.T) {
instance := newInstance()
gInstance = instance
type FooBase struct {
JojoService int `di:"*"`
}
type Foo struct {
FooBase `di:"embed"`
BarService string `di:"*"`
}
Register("string", "string")
Register("int", 1)
Register("foo", &Foo{})
var jbm = &struct {
Test string `di:"*"`
TestInt int `di:"*"`
Foo *Foo `di:"*"`
}{}
MustBind(&jbm)
data, _ := json.Marshal(jbm)
t.Log(string(data))
}