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
|
package testutils
import (
"bytes"
"fmt"
"reflect"
"github.com/go-quicktest/qt"
)
// Contains checks if interface value I is of type T. Use with qt.Satisfies:
//
// qt.Assert(t, qt.Satisfies(p, testutils.Contains[*ebpf.Program]))
func Contains[T, I any](i I) bool {
_, ok := any(i).(T)
return ok
}
// IsDeepCopy checks that got is a deep copy of want.
//
// All primitive values must be equal, but pointers must be distinct.
// This is different from [reflect.DeepEqual] which will accept equal pointer values.
// That is, reflect.DeepEqual(a, a) is true, while IsDeepCopy(a, a) is false.
func IsDeepCopy[T any](got, want T) qt.Checker {
return &deepCopyChecker[T]{got, want, make(map[pair]struct{})}
}
type pair struct {
got, want reflect.Value
}
type deepCopyChecker[T any] struct {
got, want T
visited map[pair]struct{}
}
func (dcc *deepCopyChecker[T]) Check(_ func(key string, value any)) error {
return dcc.check(reflect.ValueOf(dcc.got), reflect.ValueOf(dcc.want))
}
func (dcc *deepCopyChecker[T]) check(got, want reflect.Value) error {
switch want.Kind() {
case reflect.Interface:
return dcc.check(got.Elem(), want.Elem())
case reflect.Pointer:
if got.IsNil() && want.IsNil() {
return nil
}
if got.IsNil() {
return fmt.Errorf("expected non-nil pointer")
}
if want.IsNil() {
return fmt.Errorf("expected nil pointer")
}
if got.UnsafePointer() == want.UnsafePointer() {
return fmt.Errorf("equal pointer values")
}
switch want.Type() {
case reflect.TypeOf((*bytes.Reader)(nil)):
// bytes.Reader doesn't allow modifying it's contents, so we
// allow a shallow copy.
return nil
}
if _, ok := dcc.visited[pair{got, want}]; ok {
// Deal with recursive types.
return nil
}
dcc.visited[pair{got, want}] = struct{}{}
return dcc.check(got.Elem(), want.Elem())
case reflect.Slice:
if got.IsNil() && want.IsNil() {
return nil
}
if got.IsNil() {
return fmt.Errorf("expected non-nil slice")
}
if want.IsNil() {
return fmt.Errorf("expected nil slice")
}
if got.Len() != want.Len() {
return fmt.Errorf("expected %d elements, got %d", want.Len(), got.Len())
}
if want.Len() == 0 {
return nil
}
if got.UnsafePointer() == want.UnsafePointer() {
return fmt.Errorf("equal backing memory")
}
fallthrough
case reflect.Array:
for i := 0; i < want.Len(); i++ {
if err := dcc.check(got.Index(i), want.Index(i)); err != nil {
return fmt.Errorf("index %d: %w", i, err)
}
}
return nil
case reflect.Struct:
for i := 0; i < want.NumField(); i++ {
if err := dcc.check(got.Field(i), want.Field(i)); err != nil {
return fmt.Errorf("%q: %w", want.Type().Field(i).Name, err)
}
}
return nil
case reflect.Map:
if got.Len() != want.Len() {
return fmt.Errorf("expected %d items, got %d", want.Len(), got.Len())
}
if got.UnsafePointer() == want.UnsafePointer() {
return fmt.Errorf("maps are equal")
}
iter := want.MapRange()
for iter.Next() {
key := iter.Key()
got := got.MapIndex(iter.Key())
if !got.IsValid() {
return fmt.Errorf("key %v is missing", key)
}
want := iter.Value()
if err := dcc.check(got, want); err != nil {
return fmt.Errorf("key %v: %w", key, err)
}
}
return nil
case reflect.Chan, reflect.UnsafePointer:
return fmt.Errorf("%s is not supported", want.Type())
default:
// Compare by value as usual.
if !got.Equal(want) {
return fmt.Errorf("%#v is not equal to %#v", got, want)
}
return nil
}
}
func (dcc *deepCopyChecker[T]) Args() []qt.Arg {
return []qt.Arg{
{Name: "got", Value: dcc.got},
{Name: "want", Value: dcc.want},
}
}
|