forked from Azure/go-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
65 lines (58 loc) · 1.32 KB
/
helpers_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
package amqp
import (
"reflect"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func testEqual(x, y interface{}) bool {
return cmp.Equal(x, y, compareOpts(x, y)...)
}
func testDiff(x, y interface{}) string {
return cmp.Diff(x, y, compareOpts(x, y)...)
}
func compareOpts(x, y interface{}) []cmp.Option {
return cmp.Options{
deepAllowUnexported(x, y),
cmpopts.EquateNaNs(),
}
}
// from https://github.com/google/go-cmp/issues/40
func deepAllowUnexported(vs ...interface{}) cmp.Option {
m := make(map[reflect.Type]struct{})
for _, v := range vs {
structTypes(reflect.ValueOf(v), m)
}
var types []interface{}
for t := range m {
types = append(types, reflect.New(t).Elem().Interface())
}
return cmp.AllowUnexported(types...)
}
func structTypes(v reflect.Value, m map[reflect.Type]struct{}) {
if !v.IsValid() {
return
}
switch v.Kind() {
case reflect.Ptr:
if !v.IsNil() {
structTypes(v.Elem(), m)
}
case reflect.Interface:
if !v.IsNil() {
structTypes(v.Elem(), m)
}
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
structTypes(v.Index(i), m)
}
case reflect.Map:
for _, k := range v.MapKeys() {
structTypes(v.MapIndex(k), m)
}
case reflect.Struct:
m[v.Type()] = struct{}{}
for i := 0; i < v.NumField(); i++ {
structTypes(v.Field(i), m)
}
}
}