-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_test.go
99 lines (93 loc) · 2.23 KB
/
kind_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
package refutil
import (
"reflect"
"testing"
"github.com/huttarichard/refutil/test"
)
func TestKind(t *testing.T) {
var s string
val := reflect.ValueOf(s)
typ := reflect.TypeOf(s)
unexported := test.Sample(test.SampleInt)
val2 := reflect.ValueOf(unexported)
typ2 := reflect.TypeOf(unexported)
unexported2 := test.Sample(test.SampleStruct)
val3 := reflect.ValueOf(unexported2)
typ3 := reflect.TypeOf(unexported2)
tests := []struct {
expected Kinder
equals reflect.Kind
want bool
}{
{typ, reflect.String, true},
{val, reflect.String, true},
{typ, reflect.Map, false},
{typ, reflect.Chan, false},
{typ, reflect.Bool, false},
{val, reflect.Map, false},
{val, reflect.Chan, false},
{val, reflect.Bool, false},
{val2, reflect.Bool, false},
{typ2, reflect.Bool, false},
{val3, reflect.Ptr, true},
{typ3, reflect.Ptr, true},
{val3, reflect.Bool, false},
{typ3, reflect.Bool, false},
}
for _, tt := range tests {
t.Run("test len", func(t *testing.T) {
l := IsKind(tt.expected, tt.equals)
if l != tt.want {
t.Errorf("IsKind() = %v, want %v", l, tt.want)
}
})
}
}
func TestKindOf(t *testing.T) {
var s string
val := reflect.ValueOf(s)
typ := reflect.TypeOf(s)
tests := []struct {
expected Kinder
equals []reflect.Kind
want bool
}{
{typ, []reflect.Kind{reflect.String, reflect.Map}, true},
{val, []reflect.Kind{reflect.String, reflect.Map}, true},
{typ, []reflect.Kind{reflect.String}, true},
{val, []reflect.Kind{reflect.String}, true},
{typ, []reflect.Kind{reflect.Map}, false},
{val, []reflect.Kind{reflect.Map}, false},
}
for _, tt := range tests {
t.Run("test len", func(t *testing.T) {
l := KindOneOf(tt.expected, tt.equals...)
if l != tt.want {
t.Errorf("KindOneOf() = %v, want %v", l, tt.want)
}
})
}
}
func TestKindIs(t *testing.T) {
t1 := NewType(struct{}{})
if !t1.IsKind(reflect.Struct) {
t.Fatal()
}
if !t1.KindOneOf(reflect.Struct, reflect.Ptr) {
t.Fatal()
}
v1 := NewValue(struct{}{})
if !v1.IsKind(reflect.Struct) {
t.Fatal()
}
if !v1.KindOneOf(reflect.Struct, reflect.Ptr) {
t.Fatal()
}
d1 := NewData(struct{}{})
if !d1.IsKind(reflect.Struct) {
t.Fatal()
}
if !d1.KindOneOf(reflect.Struct, reflect.Ptr) {
t.Fatal()
}
}