-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdicgo_test.go
148 lines (122 loc) · 2.63 KB
/
dicgo_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
136
137
138
139
140
141
142
143
144
145
146
147
148
package dicgo
import "testing"
func TestValue(t *testing.T) {
c := NewContainer()
c.SetValue("val", "test")
injectedValue, ok := c.Get("val")
if !ok || injectedValue != "test" {
t.Errorf("Expected \"test\", got %+v", injectedValue)
}
}
func TestSingleton(t *testing.T) {
c := NewContainer()
c.SetSingleton("service", func(cont Container) interface{} {
return "test"
})
injectedValue, ok := c.Get("service")
if !ok || injectedValue != "test" {
t.Errorf("Expected \"test\", got %+v", injectedValue)
}
}
type service1 struct {
id string
num int
}
type service2 struct {
s1 service1
name string
}
func TestSingletonDep(t *testing.T) {
c := NewContainer()
c.SetSingleton("s1", func(cont Container) interface{} {
return "hello"
})
c.SetSingleton("s2", func(cont Container) interface{} {
s1, ok := cont.Get("s1")
if !ok {
return ""
}
return s1.(string) + " world"
})
injectedValue, ok := c.Get("s2")
if !ok || injectedValue != "hello world" {
t.Errorf("Expected \"hello world\", got %+v", injectedValue)
}
}
func TestSingletonPointer(t *testing.T) {
c := NewContainer()
c.SetSingleton("s1", func(cont Container) interface{} {
return &service1{
id: "s1",
num: 1,
}
})
injectedValue, ok := c.Get("s1")
s1 := injectedValue.(*service1)
if !ok || s1.id != "s1" {
t.Errorf("Expected \"s1\", got %+v", s1)
}
s1.num = 2
injectedValue, ok = c.Get("s1")
s := injectedValue.(*service1)
if s != s1 {
t.Errorf("Not the same pointer: %v, %v", s1, 2)
}
if !ok || s.num != 2 {
t.Errorf("Expected num \"2\", got %d", s.num)
}
}
func TestFactgory(t *testing.T) {
c := NewContainer()
c.SetFactory("s1", func(cont Container) interface{} {
return &service1{
id: "s1",
num: 1,
}
})
injectedValue, ok := c.Get("s1")
s1 := injectedValue.(*service1)
if !ok || s1.id != "s1" {
t.Errorf("Expected \"s1\", got %+v", s1)
}
s1.num = 2
injectedValue, ok = c.Get("s1")
s := injectedValue.(*service1)
if s == s1 {
t.Errorf("The same pointer: %v, %v", s1, 2)
}
if !ok || s.num == 2 {
t.Errorf("Expected num \"1\", got %d", s.num)
}
}
func TestHasDel(t *testing.T) {
c := NewContainer()
c.SetSingleton("s1", func(cont Container) interface{} {
return &service1{
id: "s1",
num: 1,
}
})
ok := c.Has("s1")
if !ok {
t.Error("Has no s1 service")
}
ok = c.Has("s2")
if ok {
t.Error("Has non existing s2 service")
}
s2, ok := c.Get("s2")
if ok {
t.Errorf("Got non existing s2 service, %+v", s2)
}
c.Del("s2")
c.Del("s1")
ok = c.Has("s1")
if ok {
t.Error("Still has deleted s1 service")
}
s1, ok := c.Get("s1")
if ok {
t.Errorf("Got deleted s1 service, %+v", s1)
}
}