-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_test.go
96 lines (87 loc) · 1.77 KB
/
type_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
package staticplug
import (
"os"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
type fakeInterface interface {
MyFunc() string
}
func TestTypeOfInterface(t *testing.T) {
for _, tc := range []struct {
name string
value any
want reflect.Type
wantErr error
}{
{
name: "nil",
wantErr: os.ErrInvalid,
},
{
name: "string",
value: "value",
wantErr: os.ErrInvalid,
},
{
name: "error type",
value: (*error)(nil),
want: reflect.TypeOf((*error)(nil)).Elem(),
},
{
name: "custom",
value: (*fakeInterface)(nil),
want: reflect.TypeOf((*fakeInterface)(nil)).Elem(),
},
{
name: "custom non-ptr",
value: (fakeInterface)(nil),
wantErr: os.ErrInvalid,
},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := TypeOfInterface(tc.value)
if diff := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("Error diff (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tc.want, got, cmp.Comparer(func(a, b reflect.Type) bool {
return a == b
})); diff != "" {
t.Errorf("TypeOfInterface() diff (-want +got):\n%s", diff)
}
})
}
}
func TestMustTypeOfInterface(t *testing.T) {
for _, tc := range []struct {
name string
value any
wantErr error
}{
{
name: "nil",
wantErr: os.ErrInvalid,
},
{
name: "success",
value: (*fakeInterface)(nil),
},
{
name: "non-ptr",
value: (fakeInterface)(nil),
wantErr: os.ErrInvalid,
},
} {
t.Run(tc.name, func(t *testing.T) {
defer func() {
got := recover()
if diff := cmp.Diff(tc.wantErr, got, cmpopts.EquateErrors()); diff != "" {
t.Errorf("Panic diff (-want +got):\n%s", diff)
}
}()
MustTypeOfInterface(tc.value)
})
}
}