-
Notifications
You must be signed in to change notification settings - Fork 1
/
pointers_test.go
121 lines (113 loc) · 2.61 KB
/
pointers_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
package refutil
import (
"reflect"
"testing"
)
func TestCanIndirectType(t *testing.T) {
tests := []struct {
x reflect.Type
want bool
}{
{reflect.TypeOf([]uint{}), true},
{reflect.TypeOf(false), false},
}
for _, tt := range tests {
t.Run("test can indirect type", func(t *testing.T) {
if got := CanIndirectType(tt.x); got != tt.want {
t.Errorf("CanIndirectType() = %v, want %v", got, tt.want)
}
})
}
}
func TestIndirectType(t *testing.T) {
tests := []struct {
x reflect.Type
expected string
}{
{reflect.TypeOf([]uint{}), "uint"},
{reflect.TypeOf([]*uint{}), "*uint"},
{IndirectType(reflect.TypeOf([]*uint{})), "uint"},
{reflect.TypeOf(false), "bool"},
}
for _, tt := range tests {
t.Run("test can indirect type", func(t *testing.T) {
if got := IndirectType(tt.x); got.String() != tt.expected {
t.Errorf("IndirectType() = %v, want %v", got.String(), tt.expected)
}
})
}
}
func TestIndirectTypeOf(t *testing.T) {
tests := []struct {
x interface{}
expected string
}{
{[]uint{}, "uint"},
{[]*uint{}, "*uint"},
{false, "bool"},
}
for _, tt := range tests {
t.Run("test can indirect type of", func(t *testing.T) {
if got := IndirectTypeOf(tt.x); got.String() != tt.expected {
t.Errorf("IndirectTypeOf() = %v, want %v", got.String(), tt.expected)
}
})
}
}
func TestCanIndirectValue(t *testing.T) {
if CanIndirectValue(reflect.ValueOf([]uint{})) {
t.Fail()
}
if !CanIndirectValue(reflect.ValueOf(&struct{}{})) {
t.Fail()
}
}
func TestIndirectValue(t *testing.T) {
a := 1
if IndirectValue(reflect.ValueOf(&a)).Type().String() != "int" {
t.Fatalf("invalid indirect value of a")
}
var x *string
if !NewValue(x).IsValid() {
t.Fatalf("invalid indirect value of x pointer")
}
if IndirectValueOf(x).IsValid() {
t.Fatalf("indirect value of x should not be valid")
}
}
func TestIndirect(t *testing.T) {
a := 1
b := struct{}{}
c := &b
if Indirect(&a) != 1 {
t.Fatalf("indirect of 1 is not 1")
}
if Indirect(&b) != b {
t.Fatalf("indirect of &b is not b")
}
if Indirect(Indirect(&c)) != b {
t.Fatalf("double indirect of &c is not b")
}
}
func TestCanIndirect(t *testing.T) {
t1 := NewData(nil)
if t1.Type().CanIndirect() {
t.Fatalf("nil is not indirectable")
}
if t1.CanIndirect() {
t.Fatalf("nil is not indirectable")
}
if CanIndirect(nil) {
t.Fatalf("nil is not indirectable")
}
t2 := NewData(&struct{}{})
if !t2.Type().CanIndirect() {
t.Fatalf("&struct{}{} is indirectable")
}
if !t2.CanIndirect() {
t.Fatalf("&struct{}{} is indirectable")
}
if !CanIndirect(&struct{}{}) {
t.Fatalf("&struct{}{} is indirectable")
}
}