-
Notifications
You must be signed in to change notification settings - Fork 24
/
reflect_test.go
130 lines (111 loc) · 2.82 KB
/
reflect_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
package reflect_test
import (
"fmt"
corereflect "reflect"
"testing"
"unsafe"
"github.com/goccy/go-reflect"
)
func TestTypeID(t *testing.T) {
intID := reflect.TypeID(int(0))
if intID != reflect.TypeID(int(0)) {
t.Fatal("failed to get typeid")
}
}
func TestTypeAndPtrOf(t *testing.T) {
typ, ptr := reflect.TypeAndPtrOf(int(10))
if typ.Kind() != reflect.Int {
t.Fatal("failed to get type")
}
if *(*int)(ptr) != 10 {
t.Fatal("failed to get ptr")
}
}
func TestValueNoEscapeOf(t *testing.T) {
v := reflect.ValueNoEscapeOf(&struct{ I int }{I: 10})
if v.Elem().Field(0).Int() != 10 {
t.Fatal("failed to create reflect.Value from ValueNoEscapeOf")
}
}
func TestToReflectType(t *testing.T) {
typ := reflect.ToReflectType(reflect.TypeOf(1))
if fmt.Sprintf("%T", typ) != "*reflect.rtype" {
t.Fatalf("failed to convert reflect.Type")
}
}
func TestToReflectValue(t *testing.T) {
v := reflect.ToReflectValue(reflect.ValueOf(1))
if fmt.Sprintf("%T", v) != "reflect.Value" {
t.Fatalf("failed to convert reflect.Value")
}
}
func TestToType(t *testing.T) {
typ := reflect.ToType(corereflect.TypeOf(1))
if fmt.Sprintf("%T", typ) != "*reflect.rtype" {
t.Fatalf("failed to convert reflect.Type")
}
}
func TestToValue(t *testing.T) {
v := reflect.ToValue(corereflect.ValueOf(1))
if fmt.Sprintf("%T", v) != "reflect.Value" {
t.Fatalf("failed to convert reflect.Value")
}
}
func TestNewAt(t *testing.T) {
var i int
ivalFromField := reflect.ValueOf(&struct{ foo int }{100}).Elem().Field(0)
ival := reflect.ValueOf(&i).Elem()
v := reflect.NewAt(ivalFromField.Type(), unsafe.Pointer(ivalFromField.UnsafeAddr())).Elem()
ival.Set(v)
v.Set(ival)
if v.Int() != 100 {
t.Fatal("failed to NewAt")
}
}
func TestTypeBits(t *testing.T) {
bits := reflect.TypeOf(1).Bits()
if bits != 1<<6 {
t.Fatal("failed to get bits from type")
}
}
func TestTypeIsVariadic(t *testing.T) {
if !reflect.TypeOf(func(...int) {}).IsVariadic() {
t.Fatal("doesn't work IsVariadic")
}
}
func TestTypeFieldByNameFunc(t *testing.T) {
_, ok := reflect.TypeOf(struct {
Foo int
}{}).FieldByNameFunc(func(name string) bool {
return name == "Foo"
})
if !ok {
t.Fatal("failed to FieldByNameFunc")
}
}
func TestTypeLen(t *testing.T) {
if reflect.TypeOf([3]int{}).Len() != 3 {
t.Fatal("failed to Type.Len")
}
}
func TestTypeOut(t *testing.T) {
if reflect.TypeOf(func() int { return 0 }).Out(0).Kind() != reflect.Int {
t.Fatal("failed to get output parameter")
}
}
func TestTypeCanInterface(t *testing.T) {
v := "hello"
if !reflect.ValueOf(v).CanInterface() {
t.Fatal("failed to Type.CanInterface")
}
}
func TestValueFieldByNameFunc(t *testing.T) {
field := reflect.ValueOf(struct {
Foo int
}{}).FieldByNameFunc(func(name string) bool {
return name == "Foo"
})
if field.Type().Kind() != reflect.Int {
t.Fatal("failed to FieldByNameFunc")
}
}