-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_test.go
273 lines (267 loc) · 5.58 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package call
import (
"reflect"
"testing"
)
func TestReg_Arguments(t *testing.T) {
type addArgument struct {
name string
v any
}
type getArgument struct {
name string
Want any
WantOk bool
}
type deleteArgument struct {
name string
}
type getArgumentNames struct {
want []string
}
type argsTest struct {
name string
addArgument *addArgument
getArgument *getArgument
deleteArgument *deleteArgument
getArgumentNames *getArgumentNames
}
tests := []struct {
name string
tests []argsTest
}{
{
name: "test args",
tests: []argsTest{
{
name: "test add",
addArgument: &addArgument{
name: "test",
v: "test-1",
},
},
{
name: "test-2 add",
addArgument: &addArgument{
name: "test-2",
v: 22,
},
},
{
name: "test get",
getArgument: &getArgument{
name: "test",
Want: "test-1",
WantOk: true,
},
},
{
name: "test-2 get",
getArgument: &getArgument{
name: "test-2",
Want: 22,
WantOk: true,
},
},
{
name: "test delete",
deleteArgument: &deleteArgument{
name: "test",
},
},
{
name: "test get arguments",
getArgument: &getArgument{
name: "test",
Want: nil,
WantOk: false,
},
},
{
name: "test get arguments",
getArgumentNames: &getArgumentNames{
want: []string{"test-2"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := NewReg()
for _, test := range tt.tests {
if test.addArgument != nil {
r.AddArgument(test.addArgument.name, test.addArgument.v)
}
if test.getArgument != nil {
got, ok := r.GetArgument(test.getArgument.name)
if ok != test.getArgument.WantOk {
t.Errorf("GetArgument() ok = %v, want %v", ok, test.getArgument.WantOk)
}
if !reflect.DeepEqual(got, test.getArgument.Want) {
t.Errorf("GetArgument() got = %v, want %v", got, test.getArgument.Want)
}
}
if test.deleteArgument != nil {
r.DeleteArgument(test.deleteArgument.name)
}
if test.getArgumentNames != nil {
got := r.GetArgumentNames()
if !reflect.DeepEqual(got, test.getArgumentNames.want) {
t.Errorf("GetArgumentNames() got = %v, want %v", got, test.getArgumentNames.want)
}
}
}
})
}
}
func TestReg_Functions(t *testing.T) {
type addFunction struct {
name string
fn func([]any) any
args []string
}
type getFunction struct {
name string
Want func([]any) Func
WantOk bool
}
type deleteFunction struct {
name string
}
type getFunctionNames struct {
want []string
}
type functionTest struct {
name string
addFunction *addFunction
getFunction *getFunction
deleteFunction *deleteFunction
getFunctionNames *getFunctionNames
}
tests := []struct {
name string
tests []functionTest
functions []any
panic bool
}{
{
name: "test functions",
functions: []any{
func(v string) string {
return v
},
},
tests: []functionTest{
{
name: "test add",
addFunction: &addFunction{
name: "test",
fn: func(v []any) any {
return v[0]
},
args: []string{"value"},
},
},
{
name: "test add 2",
addFunction: &addFunction{
name: "test-2",
fn: func(v []any) any {
return v[0]
},
args: []string{"value-2"},
},
},
{
name: "get function",
getFunction: &getFunction{
name: "test",
Want: func(v []any) Func {
return Func{
Args: []string{"value"},
Fn: reflect.ValueOf(v[0]),
}
},
WantOk: true,
},
},
{
name: "test delete",
deleteFunction: &deleteFunction{
name: "test",
},
},
{
name: "get function",
getFunction: &getFunction{
name: "test",
Want: func([]any) Func {
return Func{}
},
WantOk: false,
},
},
{
name: "get function names",
getFunctionNames: &getFunctionNames{
want: []string{"test-2"},
},
},
},
},
{
name: "panic add functions",
functions: []any{
"test",
1234,
},
tests: []functionTest{
{
name: "wrong function type",
addFunction: &addFunction{
name: "test",
fn: func(v []any) any {
return v[1]
},
},
},
},
panic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if !tt.panic {
t.Errorf("Reg.Functions() panic = %v, want %v", r, tt.panic)
}
}
}()
r := NewReg()
for _, test := range tt.tests {
if test.addFunction != nil {
r.AddFunction(test.addFunction.name, test.addFunction.fn(tt.functions), test.addFunction.args...)
}
if test.getFunction != nil {
got, gotOk := r.GetFunction(test.getFunction.name)
if !reflect.DeepEqual(got, test.getFunction.Want(tt.functions)) {
t.Errorf("Reg.GetFunction() got = %v, want %v", got, test.getFunction.Want(tt.functions))
}
if gotOk != test.getFunction.WantOk {
t.Errorf("Reg.GetFunction() gotOk = %v, want %v", gotOk, test.getFunction.WantOk)
}
}
if test.deleteFunction != nil {
r.DeleteFunction(test.deleteFunction.name)
}
if test.getFunctionNames != nil {
if got := r.GetFunctionNames(); !reflect.DeepEqual(got, test.getFunctionNames.want) {
t.Errorf("Reg.GetFunctionNames() = %v, want %v", got, test.getFunctionNames.want)
}
}
}
})
}
}