-
Notifications
You must be signed in to change notification settings - Fork 1
/
types_test.go
60 lines (50 loc) · 1.55 KB
/
types_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
package decouple
import (
"fmt"
"go/types"
"testing"
)
type typeConstraint interface {
types.Type
comparable
}
func TestGetType(t *testing.T) {
cases := []struct {
t types.Type
isChan, isSig, isIntf, isMap bool
}{{
t: types.NewStruct(nil, nil),
}, {
t: types.NewChan(types.SendRecv, types.NewStruct(nil, nil)),
isChan: true,
}, {
t: types.NewSignatureType(nil, nil, nil, nil, nil, false),
isSig: true,
}, {
t: types.NewInterfaceType(nil, nil),
isIntf: true,
}, {
t: types.NewMap(types.NewStruct(nil, nil), types.NewStruct(nil, nil)),
isMap: true,
}}
for i, tc := range cases {
t.Run(fmt.Sprintf("case_%02d", i+1), func(t *testing.T) {
checkType[*types.Chan](t, tc.t, tc.isChan)
checkType[*types.Chan](t, types.NewNamed(types.NewTypeName(0, nil, "foo", nil), tc.t, nil), tc.isChan)
checkType[*types.Signature](t, tc.t, tc.isSig)
checkType[*types.Signature](t, types.NewNamed(types.NewTypeName(0, nil, "foo", nil), tc.t, nil), tc.isSig)
checkType[*types.Interface](t, tc.t, tc.isIntf)
checkType[*types.Interface](t, types.NewNamed(types.NewTypeName(0, nil, "foo", nil), tc.t, nil), tc.isIntf)
checkType[*types.Map](t, tc.t, tc.isMap)
checkType[*types.Map](t, types.NewNamed(types.NewTypeName(0, nil, "foo", nil), tc.t, nil), tc.isMap)
})
}
}
func checkType[T typeConstraint](t *testing.T, inp types.Type, isType bool) {
t.Helper()
var zero T
got := getType[T](inp)
if (got != zero) != isType {
t.Errorf("is-type[%T] is %v, want %v", zero, got != zero, isType)
}
}