This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregistry_test.go
215 lines (183 loc) · 3.82 KB
/
registry_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
211
212
213
214
215
// Copyright 2018 ekino. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package godim
import (
"fmt"
"reflect"
"testing"
)
type myStruct struct {
a string `config:"truc"`
}
func (m *myStruct) Key() string {
return "testkey"
}
func TestDefault(t *testing.T) {
r := newRegistry()
r.appProfile.lock()
m := myStruct{
a: "a",
}
err := r.declare("default", &m)
if err != nil {
t.Fatalf("nothing added")
}
if !r.appProfile.isLocked() {
t.Fatalf("profiles not locked")
}
typ := reflect.TypeOf(m)
n := r.getElement("default", "testkey")
if n == nil {
t.Fatalf("Can't find declared value")
}
if n != &m {
t.Fatalf("not the same")
}
tc := r.tags[typ]
if tc == nil {
t.Fatalf("tag not declared")
}
if tc.configs["a"] != "truc" {
t.Fatalf("wrong tag reading")
}
}
type MyService struct {
OtherService *MyService `inject:"service:godim.MyService"`
}
type OneHandler struct {
Another *MyService `inject:"service:godim.MyService"`
}
func TestMultiProfile(t *testing.T) {
r := newRegistry()
r.appProfile = StrictHTTPAppProfile()
b := &MyService{}
m := MyService{
OtherService: b,
}
err := r.declare("service", m)
if err != nil {
c := OneHandler{}
err = r.declare("handler", c)
if err != nil {
t.Fatalf("Injection possible")
}
} else {
t.Fatalf("There must be an error")
}
}
func TestPtr(t *testing.T) {
r := newRegistry()
r.appProfile = StrictHTTPAppProfile()
a := &OneHandler{}
err := r.declare("thing", a)
if err == nil {
t.Fatal("thing doesn't exists")
}
err = r.declare("handler", a)
if err != nil {
t.Fatal("err ", err)
} else {
b := OneHandler{}
err = r.declare("handler", b)
if err == nil {
t.Fatalf("There must be an already declared error")
}
}
}
type TestConfig struct {
a string `config:"cle"`
}
func TestTags(t *testing.T) {
r := newRegistry()
r.appProfile = StrictHTTPAppProfile()
typ := reflect.TypeOf(TestConfig{})
err := r.declareTags(typ, "service")
if err != nil {
t.Fatalf("error while declaring tags")
}
tc := r.tags[typ]
if tc == nil {
t.Fatalf("no tagconfig")
}
if tc.configs["a"] != "cle" {
fmt.Printf("%+v\n", tc.configs)
t.Fatalf("wrong tag reading")
}
}
type NotIniter struct {
}
type Initer struct {
Initializer
}
func (i *Initer) OnInit() error {
return nil
}
func TestInterface(t *testing.T) {
r := newRegistry()
r.appProfile = StrictHTTPAppProfile()
ni := NotIniter{}
not := reflect.TypeOf(ni)
err := r.declareInterfaces(&ni, not)
if err != nil {
t.Fatalf("laze")
}
i := Initer{}
ini := reflect.TypeOf(i)
err = r.declareInterfaces(&i, ini)
if err != nil {
t.Fatalf("laze2 %s", err)
}
}
func TestGetKey(t *testing.T) {
tc := &TestConfig{}
typ := reflect.TypeOf(tc).Elem()
key := getKey(typ, tc)
if key != "TestConfig" {
t.Fatalf("Wrong key retrieval %s", key)
}
ms := &myStruct{}
typ = reflect.TypeOf(ms).Elem()
key = getKey(typ, ms)
if key != "testkey" {
t.Fatalf("Wrong key retrieval %s from method", key)
}
}
type Prioritized struct {
Initiated bool
}
func (p *Prioritized) OnInit() error {
p.Initiated = true
return nil
}
type NotPrioritized struct {
Prio *Prioritized
InitCounter int
}
func (np *NotPrioritized) Priority() int {
return 10
}
func (np *NotPrioritized) OnInit() error {
np.InitCounter++
if np.Prio.Initiated {
np.InitCounter++
}
return nil
}
func TestPriorization(t *testing.T) {
r := newRegistry()
r.appProfile = StrictHTTPAppProfile()
p := Prioritized{Initiated: false}
pt := reflect.TypeOf(p)
_ = r.declareInterfaces(&p, pt)
np := NotPrioritized{Prio: &p, InitCounter: 0}
npt := reflect.TypeOf(np)
_ = r.declareInterfaces(&np, npt)
_ = r.initializeAll()
if !p.Initiated {
t.Fatalf("OnInit has not been called")
}
if np.InitCounter != 2 {
t.Fatalf("Priorization didn't work as expected")
}
}