-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_test.go
391 lines (367 loc) · 9.66 KB
/
internal_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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package endpoint
import (
"fmt"
"net/http"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const lastT = true
const lastF = false
const staticT = true
const staticF = false
const panicT = true
const panicF = false
const nowPastT = true
const nowPastF = false
type intType1 int
type intType2 int
type intType3 int
type intType4 int
type intType5 int
type intType6 int
type intType7 int
type stringA string
type stringB string
type stringC string
type stringD string
type stringE string
type stringF string
type stringG string
type stringH string
type stringI string
type stringJ string
var tsiwfsFunc1a = func() intType3 { return 9 }
var tsiwfsFunc2a = func(w http.ResponseWriter, r *http.Request) {}
type interfaceI interface {
I() int
}
type interfaceJ interface {
I() int
}
type interfaceK interface {
I() int
}
type doesI struct {
i int
}
func (di *doesI) I() int { return di.i * 2 }
type doesJ struct {
j int
}
func (dj *doesJ) I() int { return dj.j * 3 }
var characterizeTests = []struct {
name string
fn interface{}
expectedTypeCode typeCode
last bool
pastStatic bool
expectedToPanic bool
expectedNowPastStatic bool
expectedInputParams []typeCode // recevied from above
expectedOutputParams []typeCode // going down
expectedReturnParams []typeCode // going up
expectedReturnedParams []typeCode // received from below
}{
{
"tsiwfsFunc1",
tsiwfsFunc1a,
staticInjectorFunc,
lastF, staticF, panicF, nowPastF,
nil, []typeCode{intType3TC},
nil, nil,
},
{
"tsiwfsFunc2",
tsiwfsFunc2a,
endpointFunc,
lastT, staticF, panicF, nowPastT,
[]typeCode{responseWriterTC, requestTC}, nil,
nil, nil,
},
{
"static injector",
func(int) string { return "" },
staticInjectorFunc,
lastF, staticF, panicF, nowPastF,
[]typeCode{intTC}, []typeCode{stringTC},
nil, nil,
},
{
"minimal injector",
func() {},
injectorFunc,
lastF, staticT, panicF, nowPastT,
[]typeCode{}, []typeCode{},
nil, nil,
},
{
"minimal injector annotated as not static",
AnnotateNotStatic(func() {}),
injectorFunc,
lastF, staticF, panicF, nowPastT,
[]typeCode{}, []typeCode{},
nil, nil,
},
{
"static injector",
func(int) string { return "" },
injectorFunc,
lastF, staticT, panicF, nowPastT,
[]typeCode{intTC}, []typeCode{stringTC},
nil, nil,
},
{
"minimal fallible injector",
func() TerminalError { return nil },
fallibleInjectorFunc,
lastF, staticF, panicF, nowPastT,
[]typeCode{}, nil,
[]typeCode{errorTC}, nil,
},
{
"fallible injector",
func(int, string) (TerminalError, string) { return nil, "" },
fallibleInjectorFunc,
lastF, staticF, panicF, nowPastT,
[]typeCode{intTC, stringTC}, []typeCode{stringTC},
[]typeCode{errorTC}, nil,
},
{
"endpoint",
func(int, string) string { return "" },
endpointFunc,
lastT, staticT, panicF, nowPastT,
[]typeCode{intTC, stringTC}, nil,
[]typeCode{stringTC}, nil,
},
{
"consumer of value",
func(int, string) {},
injectorFunc,
lastF, staticF, panicF, nowPastT,
[]typeCode{intTC, stringTC}, nil,
nil, nil,
},
{
"injector switch over from static",
func(*http.Request) {},
injectorFunc,
lastF, staticF, panicF, nowPastT,
[]typeCode{requestTC}, nil,
nil, nil,
},
{
"invalid: anonymous func that isn't a wrap",
func(int) func() { return func() {} },
endpointFunc,
lastT, staticT, panicT, nowPastT,
nil, nil,
nil, nil,
},
{
"invalid: anonymous func that isn't a wrap #2",
func(func(), int) {},
endpointFunc,
lastT, staticT, panicT, nowPastT,
nil, nil,
nil, nil,
},
{
"middleware func",
func(func(http.ResponseWriter) error, string, bool) (int, error) { return 7, nil },
middlewareFunc,
lastF, staticF, panicF, nowPastT,
[]typeCode{noTypeCode, stringTC, boolTC}, []typeCode{responseWriterTC},
[]typeCode{intTC, errorTC}, []typeCode{errorTC},
},
{
"middleware func -- all middleware is past static",
func(func(intType3) (error, intType3), string, bool) (int, error) { return 7, nil },
middlewareFunc,
lastF, staticF, panicF, nowPastT,
[]typeCode{noTypeCode, stringTC, boolTC}, []typeCode{intType3TC},
[]typeCode{intTC, errorTC}, []typeCode{errorTC, intType3TC},
},
{
"simple middleware regression",
func(i func() error, w http.ResponseWriter) {},
middlewareFunc,
lastF, staticT, panicF, nowPastT,
[]typeCode{noTypeCode, responseWriterTC}, []typeCode{},
[]typeCode{}, []typeCode{errorTC},
},
{
"simple endpoint regression",
func(i func() error, w http.ResponseWriter) {},
middlewareFunc,
lastT, staticF, panicT, nowPastT,
[]typeCode{noTypeCode, responseWriterTC}, []typeCode{},
[]typeCode{}, []typeCode{errorTC},
},
}
var boolTC = GetTypeCode(reflect.TypeOf((*bool)(nil)).Elem())
var intTC = GetTypeCode(reflect.TypeOf((*int)(nil)).Elem())
var intType3TC = GetTypeCode(reflect.TypeOf((*intType3)(nil)).Elem())
var stringTC = GetTypeCode(reflect.TypeOf((*string)(nil)).Elem())
var terminalErrorTC = GetTypeCode(reflect.TypeOf((*TerminalError)(nil)).Elem())
var requestTC = GetTypeCode(requestType)
var responseWriterTC = GetTypeCode(responseWriterType)
var errorTC = GetTypeCode(errorType)
func recastToTypeCode(i []typeCode) []typeCode {
// if i == nil { return []typeCode{GetTypeCode(i)} }
if i == nil {
return ([]typeCode)(nil)
}
return i
}
func een(i []typeCode) string {
var s []string
for _, c := range i {
s = append(s, c.Type().String())
}
return "[" + strings.Join(s, "; ") + "]"
// if len(i) == 0 { return nil }
// return i
}
// This tests the basic functionality of characterizeFunc()
func TestCharacterize(t *testing.T) {
t.Parallel()
for i, test := range characterizeTests {
f1 := func() {
t.Logf("trying to characterize... %s", test.name)
fm, isFuncO := test.fn.(*funcOrigin)
if isFuncO {
fm.origin = test.name
fm.index = i
} else {
fm = &funcOrigin{
origin: test.name,
index: i,
fn: test.fn,
}
}
tr, _, flows, nowPastStatic := characterizeFuncDetails(fm, true, test.last, test.pastStatic)
assert.NotNil(t, tr)
t.Logf("%s type codes: %s vs %s", test.name, test.expectedTypeCode.Type().String(), tr.FuncType.Type().String())
assert.Equal(t, test.expectedTypeCode.Type().String(), tr.FuncType.Type().String(), "type: "+test.name)
assert.Equal(t, test.expectedNowPastStatic, nowPastStatic, "past static: "+test.name)
assert.EqualValues(t, een(test.expectedInputParams), een(flows[inputParams]), "input flow: "+test.name)
assert.EqualValues(t, een(test.expectedOutputParams), een(flows[outputParams]), "output flow: "+test.name)
assert.EqualValues(t, een(test.expectedReturnParams), een(flows[returnParams]), "return flow: "+test.name)
assert.EqualValues(t, een(test.expectedReturnedParams), een(flows[returnedParams]), "returned flow: "+test.name)
}
if test.expectedToPanic {
assert.Panics(t, f1, test.name)
} else {
f1()
}
}
}
var checkMatchTests = []struct {
a interface{}
b interface{}
match bool
expectedInputParams []typeCode
expectedOutputParams []typeCode
expectedReturnParams []typeCode
expectedReturnedParams []typeCode
}{
{
func() {},
reflect.TypeOf((*typeMoreInputs)(nil)).Elem(),
false,
nil, nil,
nil, nil,
},
}
func toType(i interface{}) reflect.Type {
if t, already := i.(reflect.Type); already {
return t
}
if c, already := i.(typeCode); already {
return c.Type()
}
return reflect.TypeOf(i)
}
func TestCheckMatch(t *testing.T) {
t.Parallel()
for _, test := range checkMatchTests {
a := toType(test.a)
b := toType(test.b)
name := fmt.Sprintf("%s vs %s", a, b)
t.Logf("trying to test match... %s vs %s", a, b)
mismatch, flows := checkMatch(a, b)
if test.match {
assert.Equal(t, "", mismatch, name)
assert.EqualValues(t, een(test.expectedInputParams), een(flows[inputParams]), "input flow: "+name)
assert.EqualValues(t, een(test.expectedOutputParams), een(flows[outputParams]), "output flow: "+name)
assert.EqualValues(t, een(test.expectedReturnParams), een(flows[returnParams]), "return flow: "+name)
assert.EqualValues(t, een(test.expectedReturnedParams), een(flows[returnedParams]), "returned flow: "+name)
} else {
assert.NotEqual(t, "", mismatch, name)
}
}
}
var testI int
var i3 intType3
var di = &doesI{}
var dj = &doesJ{}
var interfaceIType = reflect.TypeOf((*interfaceI)(nil)).Elem()
var interfaceJType = reflect.TypeOf((*interfaceJ)(nil)).Elem()
var interfaceKType = reflect.TypeOf((*interfaceK)(nil)).Elem()
var provideSet1 = map[reflect.Type]int{
requestType: 1,
responseWriterType: 1,
reflect.TypeOf(testI): 3,
reflect.TypeOf(testI): 4,
}
var provideSet2 = map[reflect.Type]int{
interfaceIType: 1,
interfaceJType: 2,
reflect.TypeOf(di): 3,
reflect.TypeOf(dj): 4,
reflect.TypeOf(testI): 5,
}
var bestMatchTests = []struct {
Name string
MapData map[reflect.Type]int
Find reflect.Type
Want reflect.Type
}{
{
"responseWriter",
provideSet1,
responseWriterType,
responseWriterType,
},
{
"interface K",
provideSet2,
interfaceKType,
reflect.TypeOf(dj),
},
}
func TestBestMatch(t *testing.T) {
t.Parallel()
for _, test := range bestMatchTests {
m := make(interfaceMap)
for typ, layer := range test.MapData {
t.Logf("%s: #%d get type code for %v", test.Name, layer, typ)
m.Add(GetTypeCode(typ), layer)
}
f := func() {
for tc, d := range m {
t.Logf("\tm[%s] = %s (%s) %d", tc.Type(), d.Name, d.TypeCode.Type(), d.Layer)
}
got := m.bestMatch(GetTypeCode(test.Find), "searching for "+test.Name)
assert.Equal(t, test.Want.String(), got.Type().String(), test.Name)
}
if test.Want == nil {
assert.Panics(t, f, test.Name)
} else {
f()
}
}
}