-
Notifications
You must be signed in to change notification settings - Fork 2
/
match_test.go
85 lines (77 loc) · 1.87 KB
/
match_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
package nject
import (
"net/http"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testI int
di = &doesI{}
dj = &doesJ{}
interfaceIType = reflect.TypeOf((*interfaceI)(nil)).Elem()
interfaceJType = reflect.TypeOf((*interfaceJ)(nil)).Elem()
interfaceKType = reflect.TypeOf((*interfaceK)(nil)).Elem()
)
var (
requestType = reflect.TypeOf((**http.Request)(nil)).Elem()
responseWriterType = reflect.TypeOf((*http.ResponseWriter)(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) {
wrapTest(t, func(t *testing.T) {
for _, test := range bestMatchTests {
test := test
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, &provider{loose: true})
}
f := func() {
for tc, d := range m {
t.Logf("\tm[%s] = %s (%s) %d", tc.Type(), d.name, d.typeCode.Type(), d.layer)
}
got, _, err := m.bestMatch(getTypeCode(test.Find), "searching for "+test.Name)
require.NoError(t, err)
assert.Equal(t, test.Want.String(), got.Type().String(), test.Name)
}
if test.Want == nil {
require.Panics(t, f, test.Name)
} else {
f()
}
}
})
}