| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/netip" |
| 6 |
"reflect" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
"unsafe" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
) |
| 13 |
|
| 14 |
// AssertDeepCopyEqual checks to see if two variables have the same values but DO NOT share any memory |
| 15 |
// There is currently a special case for `time.loc` (as this code traverses into unexported fields) |
| 16 |
func AssertDeepCopyEqual(t *testing.T, a any, b any) { |
| 17 |
v1 := reflect.ValueOf(a) |
| 18 |
v2 := reflect.ValueOf(b) |
| 19 |
|
| 20 |
if !assert.Equal(t, v1.Type(), v2.Type()) { |
| 21 |
return |
| 22 |
} |
| 23 |
|
| 24 |
traverseDeepCopy(t, v1, v2, v1.Type().String()) |
| 25 |
} |
| 26 |
|
| 27 |
func traverseDeepCopy(t *testing.T, v1 reflect.Value, v2 reflect.Value, name string) bool { |
| 28 |
if v1.Type() == v2.Type() && v1.Type() == reflect.TypeOf(netip.Addr{}) { |
| 29 |
// Ignore netip.Addr types since they reuse an interned global value |
| 30 |
return false |
| 31 |
} |
| 32 |
|
| 33 |
switch v1.Kind() { |
| 34 |
case reflect.Array: |
| 35 |
for i := 0; i < v1.Len(); i++ { |
| 36 |
if !traverseDeepCopy(t, v1.Index(i), v2.Index(i), fmt.Sprintf("%s[%v]", name, i)) { |
| 37 |
return false |
| 38 |
} |
| 39 |
} |
| 40 |
return true |
| 41 |
|
| 42 |
case reflect.Slice: |
| 43 |
if v1.IsNil() || v2.IsNil() { |
| 44 |
return assert.Equal(t, v1.IsNil(), v2.IsNil(), "%s are not both nil %+v, %+v", name, v1, v2) |
| 45 |
} |
| 46 |
|
| 47 |
if !assert.Equal(t, v1.Len(), v2.Len(), "%s did not have the same length", name) { |
| 48 |
return false |
| 49 |
} |
| 50 |
|
| 51 |
// A slice with cap 0 |
| 52 |
if v1.Cap() != 0 && !assert.NotEqual(t, v1.Pointer(), v2.Pointer(), "%s point to the same slice %v == %v", name, v1.Pointer(), v2.Pointer()) { |
| 53 |
return false |
| 54 |
} |
| 55 |
|
| 56 |
v1c := v1.Cap() |
| 57 |
v2c := v2.Cap() |
| 58 |
if v1c > 0 && v2c > 0 && v1.Slice(0, v1c).Slice(v1c-1, v1c-1).Pointer() == v2.Slice(0, v2c).Slice(v2c-1, v2c-1).Pointer() { |
| 59 |
return assert.Fail(t, "", "%s share some underlying memory", name) |
| 60 |
} |
| 61 |
|
| 62 |
for i := 0; i < v1.Len(); i++ { |
| 63 |
if !traverseDeepCopy(t, v1.Index(i), v2.Index(i), fmt.Sprintf("%s[%v]", name, i)) { |
| 64 |
return false |
| 65 |
} |
| 66 |
} |
| 67 |
return true |
| 68 |
|
| 69 |
case reflect.Interface: |
| 70 |
if v1.IsNil() || v2.IsNil() { |
| 71 |
return assert.Equal(t, v1.IsNil(), v2.IsNil(), "%s are not both nil", name) |
| 72 |
} |
| 73 |
return traverseDeepCopy(t, v1.Elem(), v2.Elem(), name) |
| 74 |
|
| 75 |
case reflect.Ptr: |
| 76 |
local := reflect.ValueOf(time.Local).Pointer() |
| 77 |
if local == v1.Pointer() && local == v2.Pointer() { |
| 78 |
return true |
| 79 |
} |
| 80 |
|
| 81 |
if !assert.NotEqual(t, v1.Pointer(), v2.Pointer(), "%s points to the same memory", name) { |
| 82 |
return false |
| 83 |
} |
| 84 |
|
| 85 |
return traverseDeepCopy(t, v1.Elem(), v2.Elem(), name) |
| 86 |
|
| 87 |
case reflect.Struct: |
| 88 |
for i, n := 0, v1.NumField(); i < n; i++ { |
| 89 |
if !traverseDeepCopy(t, v1.Field(i), v2.Field(i), name+"."+v1.Type().Field(i).Name) { |
| 90 |
return false |
| 91 |
} |
| 92 |
} |
| 93 |
return true |
| 94 |
|
| 95 |
case reflect.Map: |
| 96 |
if v1.IsNil() || v2.IsNil() { |
| 97 |
return assert.Equal(t, v1.IsNil(), v2.IsNil(), "%s are not both nil", name) |
| 98 |
} |
| 99 |
|
| 100 |
if !assert.Equal(t, v1.Len(), v2.Len(), "%s are not the same length", name) { |
| 101 |
return false |
| 102 |
} |
| 103 |
|
| 104 |
if !assert.NotEqual(t, v1.Pointer(), v2.Pointer(), "%s point to the same memory", name) { |
| 105 |
return false |
| 106 |
} |
| 107 |
|
| 108 |
for _, k := range v1.MapKeys() { |
| 109 |
val1 := v1.MapIndex(k) |
| 110 |
val2 := v2.MapIndex(k) |
| 111 |
if !assert.True(t, val1.IsValid(), "%s is an invalid key in %s", k, name) { |
| 112 |
return false |
| 113 |
} |
| 114 |
|
| 115 |
if !assert.True(t, val2.IsValid(), "%s is an invalid key in %s", k, name) { |
| 116 |
return false |
| 117 |
} |
| 118 |
|
| 119 |
if !traverseDeepCopy(t, val1, val2, name+fmt.Sprintf("%s[%s]", name, k)) { |
| 120 |
return false |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return true |
| 125 |
|
| 126 |
default: |
| 127 |
if v1.CanInterface() && v2.CanInterface() { |
| 128 |
return assert.Equal(t, v1.Interface(), v2.Interface(), "%s was not equal", name) |
| 129 |
} |
| 130 |
|
| 131 |
e1 := reflect.NewAt(v1.Type(), unsafe.Pointer(v1.UnsafeAddr())).Elem().Interface() |
| 132 |
e2 := reflect.NewAt(v2.Type(), unsafe.Pointer(v2.UnsafeAddr())).Elem().Interface() |
| 133 |
|
| 134 |
return assert.Equal(t, e1, e2, "%s (unexported) was not equal", name) |
| 135 |
} |
| 136 |
} |