-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuse_test.go
210 lines (185 loc) · 4.77 KB
/
fuse_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package fuse
import (
"fmt"
"os"
"reflect"
"testing"
)
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
fmt.Println("BEFORE---------------------")
i := m.Run()
fmt.Println("AFTER---------------------")
os.Exit(i)
}
func Test_Register(t *testing.T) {
fmt.Println("Testing Test_Register")
cs := make([]Entry, 0)
e1 := Entry{Name: "OrdCtrl", State: true, Instance: &OrderController{s: "first"}}
cs = append(cs, e1)
e2 := Entry{Name: "OrdSvc", State: true, Instance: &OrderService{T: "second"}}
cs = append(cs, e2)
fuse := New()
find := fuse.Find
errors := fuse.Register(cs)
if len(errors) > 0 {
t.Errorf("there should be no errorsin Register")
}
errors = fuse.Wire()
if len(errors) > 0 {
t.Errorf("there should be no errors in Wire")
}
comp := find("OrdCtrl")
s, ok := comp.(*OrderController)
if !ok {
t.Errorf("should have recorded OrdCtrl")
}
if s.OrdSvc == nil {
t.Errorf("should have wired in OrdSvc")
return
}
if s.OrdPtr == nil {
t.Errorf("should have wired in OrdPtr")
}
fmt.Println(s.OrdPtr.findOrder())
fmt.Println(s.OrdSvc.findOrder())
fmt.Println(s.OrdSvc2.findOrder())
}
func Test_no_comp_found(t *testing.T) {
fmt.Println("Testing Test_is_ok")
cs := make([]Entry, 0)
e1 := Entry{Name: "OrdCtrl", State: true, Instance: &OrderController1{s: "first"}}
cs = append(cs, e1)
e2 := Entry{Name: "OrdSvc1", State: true, Instance: &OrderService{T: "second"}}
cs = append(cs, e2)
fuse := New()
errors := fuse.Register(cs)
errors = fuse.Wire()
fmt.Println(len(errors))
if len(errors) != 3 {
t.Error("There should have been 2 errors for comp not found")
}
fmt.Println(errors)
}
func Test_no_addr_set(t *testing.T) {
fmt.Println("Testing Test_is_ok")
cs := make([]Entry, 0)
e1 := Entry{Name: "OrdCtrl", State: true, Instance: &OrderController1{s: "first"}}
cs = append(cs, e1)
e2 := Entry{Name: "OrdSvc", State: true, Instance: &OrderService{T: "second"}}
cs = append(cs, e2)
fuse := New()
errors := fuse.Register(cs)
errors = fuse.Wire()
if len(errors) != 1 {
t.Error("There should have been 2 errors for comp not found")
}
fmt.Println(errors)
}
func Test_no_annot_assign(t *testing.T) {
fmt.Println("Testing Test_is_ok")
cs := make([]Entry, 0)
e1 := Entry{Name: "OrdCtrl", State: true, Instance: &OrderController2{s: "first"}}
cs = append(cs, e1)
e2 := Entry{Name: "OrdSvc1", State: true, Instance: &OrderService{T: "second"}}
cs = append(cs, e2)
fuse := New()
errors := fuse.Register(cs)
errors = fuse.Wire()
if len(errors) != 2 {
t.Error("There should have been 2 errors for comp not assignable")
}
fmt.Println(errors)
}
type emp struct {
s string
dv dep
dv1 dep `_fuse:"OrdSvc"`
dv2 dep `_fuse:""`
dp *dep
dp2 *dep `_fuse:"OrdSvc"`
it itest
it2 itest `_fuse=" "`
it3 itest `_fuse:"name"`
}
func (e emp) m1() {
}
type itest interface {
m1()
}
type dep struct {
}
func Test_eligible(t *testing.T) {
ty := reflect.TypeOf(emp{})
sf, _ := ty.FieldByName("s")
if eligible(sf) {
t.Errorf("field s should not be eligible, neither pointer nor interface")
}
sf, _ = ty.FieldByName("dv")
if eligible(sf) {
t.Errorf("field s should not be eligible, neither pointer nor interface")
}
sf, _ = ty.FieldByName("dv1")
if eligible(sf) {
t.Errorf("field s should not be eligible, neither pointer nor interface")
}
sf, _ = ty.FieldByName("dv2")
if eligible(sf) {
t.Errorf("field s should not be eligible, neither pointer nor interface")
}
sf, _ = ty.FieldByName("dp")
if eligible(sf) {
t.Errorf("dp should not be eligible")
}
sf, _ = ty.FieldByName("dp2")
if !eligible(sf) {
t.Errorf("dp2 should not throw error as _fuse tag is set correctly")
}
sf, _ = ty.FieldByName("it")
if eligible(sf) {
t.Errorf("it should not be eligible")
}
sf, _ = ty.FieldByName("it2")
if eligible(sf) {
t.Errorf("it2 should not be eligible")
}
sf, _ = ty.FieldByName("it3")
if !eligible(sf) {
t.Errorf("it3 should not throw error as _fuse tag is set correctly")
}
}
func Test_create(t *testing.T) {
fmt.Println("Testing Test_create")
cs := make([]Entry, 0)
e1 := Entry{Name: "svc1", State: false, Instance: &Svc1{s: "first"}}
cs = append(cs, e1)
e2 := Entry{Name: "svc2", State: true, Instance: &Svc2{s: "second"}}
cs = append(cs, e2)
e3 := Entry{Name: "svc3", State: true, Instance: &Svc3{s: "third"}}
cs = append(cs, e3)
fuse := New()
errors := fuse.Register(cs)
fmt.Println(errors)
c := fuse.Find("svc1")
c1, ok := c.(*Svc1)
if !ok {
t.Errorf("should have recorded Svc1")
}
c1.M1()
c2, ok := c.(Isvc1)
if !ok {
t.Errorf("should have recorded Svc1")
}
c2.M1()
}
func Test_caller_panics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
} else {
t.Errorf("should have pacicked")
}
}()
fuse := New()
fuse.RegisterMock("test", nil)
}