-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_test.go
289 lines (269 loc) · 8.66 KB
/
validate_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package van
import (
"context"
"reflect"
"testing"
)
func TestIsStructPtr(t *testing.T) {
if !isStructPtr(reflect.TypeOf(&struct{}{})) {
t.Errorf("expected true, got false")
}
if isStructPtr(reflect.TypeOf(struct{}{})) {
t.Errorf("expected false, got true")
}
if isStructPtr(reflect.TypeOf(1)) {
t.Errorf("expected false, got true")
}
}
func TestValidateProviderSignature(t *testing.T) {
tests := map[string]struct {
provider interface{}
wantErr string
wantOk bool
}{
"valid provider": {
provider: func(context.Context) (interface{}, error) { return nil, nil },
wantOk: true,
},
"not a function": {
provider: 0,
wantErr: "provider must be a function, got int",
},
"no return values": {
provider: func(context.Context) {},
wantErr: "provider must have two return values, got 0",
},
"too many return values": {
provider: func(context.Context) (interface{}, interface{}, error) { return nil, nil, nil },
wantErr: "provider must have two return values, got 3",
},
"first return value not interface": {
provider: func(context.Context) (int, error) { return 0, nil },
wantErr: "provider's first return value must be an interface, got int",
},
"second return value not error": {
provider: func(context.Context) (interface{}, int) { return nil, 0 },
wantErr: "provider's second return value must be an error, got int",
},
"argument not interface": {
provider: func(context.Context, int) (interface{}, error) { return nil, nil },
wantErr: "argument 1 must be an interface, struct or *van.Van, got int",
},
"dependency struct field is not exported": {
provider: func(context.Context, struct{ s interface{} }) (interface{}, error) { return nil, nil },
wantErr: "error in dependency struct argument 1: field s must be exported",
},
"dependency struct field is not an interface": {
provider: func(context.Context, struct{ S int }) (interface{}, error) { return nil, nil },
wantErr: "error in dependency struct argument 1: field S must be an interface, got int",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
providerType := reflect.TypeOf(tt.provider)
err := validateProviderSignature(providerType)
if tt.wantOk {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
} else {
if err == nil {
t.Errorf("expected error, got nil")
}
if err.Error() != tt.wantErr {
t.Errorf("expected error %q, got %q", tt.wantErr, err.Error())
}
}
})
}
}
func TestValidateHandlerSignature(t *testing.T) {
tests := map[string]struct {
handler interface{}
wantErr string
wantOk bool
}{
"valid handler": {
handler: func(context.Context, *struct{}, interface{}) error { return nil },
wantOk: true,
},
"not a function": {
handler: 0,
wantErr: "handler must be a function, got int",
},
"not enough arguments": {
handler: func(context.Context) error { return nil },
wantErr: "handler must have at least 2 arguments, got 1",
},
"first argument is not a not context": {
handler: func(int, *struct{}, interface{}) error { return nil },
wantErr: "handler's first argument must be context.Context, got int",
},
"second argument is not a pointer to struct": {
handler: func(context.Context, int, interface{}) error { return nil },
wantErr: "handler's second argument must be a struct pointer, got int",
},
"third argument is not an interface": {
handler: func(context.Context, *struct{}, int) error { return nil },
wantErr: "argument 2 must be an interface, struct or *van.Van, got int",
},
"dependency struct field is not exported": {
handler: func(context.Context, *struct{}, struct{ s interface{} }) error { return nil },
wantErr: "error in dependency struct argument 2: field s must be exported",
},
"dependency struct field is not an interface": {
handler: func(context.Context, *struct{}, struct{ S int }) error { return nil },
wantErr: "error in dependency struct argument 2: field S must be an interface, got int",
},
"no return values": {
handler: func(context.Context, *struct{}, interface{}) {},
wantErr: "handler must have one return value, got 0",
},
"too many return values": {
handler: func(context.Context, *struct{}, interface{}) (interface{}, error) { return nil, nil },
wantErr: "handler must have one return value, got 2",
},
"return value is not an error": {
handler: func(context.Context, *struct{}, interface{}) int { return 0 },
wantErr: "handler's return type must be error, got int",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
handlerType := reflect.TypeOf(tt.handler)
err := validateHandlerSignature(handlerType)
if tt.wantOk {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
} else {
if err == nil {
t.Errorf("expected error, got nil")
}
if err.Error() != tt.wantErr {
t.Errorf("expected error %q, got %q", tt.wantErr, err.Error())
}
}
})
}
}
func TestValidateListenerSignature(t *testing.T) {
tests := map[string]struct {
listener interface{}
wantErr string
wantOk bool
}{
"valid listener": {
listener: func(context.Context, struct{}, interface{}) {},
wantOk: true,
},
"not a function": {
listener: 0,
wantErr: "handler must be a function, got int",
},
"not enough arguments": {
listener: func(context.Context) {},
wantErr: "handler must have at least 2 arguments, got 1",
},
"first argument is not a not context": {
listener: func(int, struct{}, interface{}) {},
wantErr: "handler's first argument must be context.Context, got int",
},
"second argument is not a struct": {
listener: func(context.Context, int, interface{}) {},
wantErr: "handler's second argument must be a struct, got int",
},
"third argument is not an interface": {
listener: func(context.Context, struct{}, int) {},
wantErr: "argument 2 must be an interface, struct or *van.Van, got int",
},
"dependency struct field is not exported": {
listener: func(context.Context, struct{}, struct{ s interface{} }) {},
wantErr: "error in dependency struct argument 2: field s must be exported",
},
"dependency struct field is not an interface": {
listener: func(context.Context, struct{}, struct{ S int }) {},
wantErr: "error in dependency struct argument 2: field S must be an interface, got int",
},
"too many return values": {
listener: func(context.Context, struct{}, interface{}) int { return 0 },
wantErr: "event handler should not have any return values",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
listenerType := reflect.TypeOf(tt.listener)
err := validateListenerSignature(listenerType)
if tt.wantOk {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
} else {
if err == nil {
t.Errorf("expected error, got nil")
}
if err.Error() != tt.wantErr {
t.Errorf("expected error %q, got %q", tt.wantErr, err.Error())
}
}
})
}
}
func TestValidateExecLambdaSignature(t *testing.T) {
tests := map[string]struct {
fn interface{}
wantErr string
wantOk bool
}{
"valid": {
fn: func(dep1 interface{}, dep2 struct{ S interface{} }) error { return nil },
wantOk: true,
},
"not a function": {
fn: 1,
wantErr: "function must be a function, got int",
},
"no return values": {
fn: func() {},
wantErr: "function must have one return value, got 0",
},
"too many return values": {
fn: func() (int, error) { return 0, nil },
wantErr: "function must have one return value, got 2",
},
"return value is not an error": {
fn: func() int { return 0 },
wantErr: "return value must be an error, got int",
},
"dependency is not an interface": {
fn: func(int) error { return nil },
wantErr: "argument 0 must be an interface, struct or *van.Van, got int",
},
"dependency struct field is not exported": {
fn: func(struct{ s interface{} }) error { return nil },
wantErr: "error in dependency struct argument 0: field s must be exported",
},
"dependency struct field is not an interface": {
fn: func(struct{ S int }) error { return nil },
wantErr: "error in dependency struct argument 0: field S must be an interface, got int",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fnType := reflect.TypeOf(tt.fn)
err := validateExecLambdaSignature(fnType)
if tt.wantOk {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
} else {
if err == nil {
t.Errorf("expected error, got nil")
}
if err.Error() != tt.wantErr {
t.Errorf("expected error %q, got %q", tt.wantErr, err.Error())
}
}
})
}
}