-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdescriptor_test.go
173 lines (147 loc) · 3.94 KB
/
descriptor_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package memdump
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func assertCompareDescriptors(t *testing.T, a interface{}, b interface{}, expected bool) {
da := describe(reflect.TypeOf(a))
db := describe(reflect.TypeOf(b))
assert.Equal(t, expected, descriptorsEqual(da, db))
}
func TestDescribeScalar(t *testing.T) {
assertCompareDescriptors(t, "abc", "d", true)
assertCompareDescriptors(t, 123, 456, true)
assertCompareDescriptors(t, 1.2, 3.4, true)
assertCompareDescriptors(t, uint64(12), uint64(34), true)
assertCompareDescriptors(t, int16(12), int64(34), false)
assertCompareDescriptors(t, uint64(12), int64(34), false)
assertCompareDescriptors(t, uint64(12), "34", false)
assertCompareDescriptors(t, uint64(12), 4.5, false)
}
func TestDescribePointer(t *testing.T) {
var x int
var y uint64
var z string
assertCompareDescriptors(t, &x, &x, true)
assertCompareDescriptors(t, &y, &y, true)
assertCompareDescriptors(t, &z, &z, true)
assertCompareDescriptors(t, &x, &y, false)
assertCompareDescriptors(t, &x, &z, false)
assertCompareDescriptors(t, &x, x, false)
xptr := &x
xptrptr := &xptr
assertCompareDescriptors(t, xptrptr, xptrptr, true)
assertCompareDescriptors(t, xptr, xptrptr, false)
assertCompareDescriptors(t, x, xptrptr, false)
}
func TestDescribeSlice(t *testing.T) {
var x []int
var y []uint64
var z [][]int16
assertCompareDescriptors(t, x, x, true)
assertCompareDescriptors(t, y, y, true)
assertCompareDescriptors(t, z, z, true)
assertCompareDescriptors(t, &x, &x, true)
assertCompareDescriptors(t, &y, &y, true)
assertCompareDescriptors(t, &z, &z, true)
assertCompareDescriptors(t, x, y, false)
assertCompareDescriptors(t, &x, &y, false)
assertCompareDescriptors(t, x, z, false)
assertCompareDescriptors(t, &x, x, false)
}
func TestDescribeArray(t *testing.T) {
var x [4]int
var y [10]int
var z [2][3]int16
assertCompareDescriptors(t, x, x, true)
assertCompareDescriptors(t, y, y, true)
assertCompareDescriptors(t, z, z, true)
assertCompareDescriptors(t, &x, &x, true)
assertCompareDescriptors(t, &y, &y, true)
assertCompareDescriptors(t, &z, &z, true)
assertCompareDescriptors(t, x, y, false)
assertCompareDescriptors(t, &x, &y, false)
assertCompareDescriptors(t, x, z, false)
assertCompareDescriptors(t, &x, x, false)
}
func TestDescribeStruct(t *testing.T) {
type u struct {
A string
B int
}
type v struct {
A string
B int32
}
type w struct {
u
A string
B int
c v
}
assertCompareDescriptors(t, u{}, u{}, true)
assertCompareDescriptors(t, v{}, v{}, true)
assertCompareDescriptors(t, w{}, w{}, true)
assertCompareDescriptors(t, &u{}, &u{}, true)
assertCompareDescriptors(t, &v{}, &v{}, true)
assertCompareDescriptors(t, &w{}, &w{}, true)
assertCompareDescriptors(t, u{}, v{}, false)
assertCompareDescriptors(t, u{}, w{}, false)
assertCompareDescriptors(t, v{}, w{}, false)
assertCompareDescriptors(t, u{}, &u{}, false)
}
func TestDescribeStructWithTags(t *testing.T) {
type u struct {
A string
B int
}
type v struct {
A string
xyz int
}
type w struct {
A string
B int `memdump:"xyz"`
}
assertCompareDescriptors(t, u{}, v{}, false)
assertCompareDescriptors(t, u{}, w{}, false)
assertCompareDescriptors(t, v{}, w{}, true)
}
func TestDescriptorsEqual_DifferentElems(t *testing.T) {
type u struct {
A []string
}
type v struct {
A []int
}
type w struct {
A []int
}
assertCompareDescriptors(t, u{}, v{}, false)
assertCompareDescriptors(t, u{}, w{}, false)
assertCompareDescriptors(t, v{}, w{}, true)
}
func TestDescriptorsEqual_DifferentNumFields(t *testing.T) {
type u struct {
A string
B string
}
type v struct {
A string
}
type w struct {
A string
}
assertCompareDescriptors(t, u{}, v{}, false)
assertCompareDescriptors(t, u{}, w{}, false)
assertCompareDescriptors(t, v{}, w{}, true)
}
func TestDescribe_PanicsOnMap(t *testing.T) {
type T struct {
A map[string]int
}
assert.Panics(t, func() {
describe(reflect.TypeOf(T{}))
})
}