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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
package testing
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"math"
"reflect"
"github.com/aws/smithy-go/document"
"github.com/aws/smithy-go/middleware"
)
// CompareValues compares two values to determine if they are equal,
// specialized for comparison of SDK operation output types.
//
// CompareValues expects the two values to be of the same underlying type.
// Doing otherwise will result in undefined behavior.
//
// The third variadic argument is vestigial from a previous implementation that
// depended on go-cmp. Values passed therein have no effect.
func CompareValues(expect, actual interface{}, _ ...interface{}) error {
return deepEqual(reflect.ValueOf(expect), reflect.ValueOf(actual), "<root>")
}
func deepEqual(expect, actual reflect.Value, path string) error {
if et, at := expect.Kind(), actual.Kind(); et != at {
return fmt.Errorf("%s: kind %s != %s", path, et, at)
}
// there are a handful of short-circuit cases here within the context of
// operation responses:
// - ResultMetadata (we don't care)
// - document.Interface (check for marshaled []byte equality)
// - io.Reader (check for Read() []byte equality)
ei, ai := expect.Interface(), actual.Interface()
if _, _, ok := asMetadatas(ei, ai); ok {
return nil
}
if e, a, ok := asDocuments(ei, ai); ok {
if !compareDocumentTypes(e, a) {
return fmt.Errorf("%s: document values unequal", path)
}
return nil
}
if e, a, ok := asReaders(ei, ai); ok {
if err := CompareReaders(e, a); err != nil {
return fmt.Errorf("%s: %w", path, err)
}
return nil
}
switch expect.Kind() {
case reflect.Pointer:
if expect.Type() != actual.Type() {
return fmt.Errorf("%s: type mismatch", path)
}
expect = deref(expect)
actual = deref(actual)
ek, ak := expect.Kind(), actual.Kind()
if ek == reflect.Invalid || ak == reflect.Invalid {
// one was a nil pointer, so they both must be nil
if ek == ak {
return nil
}
return fmt.Errorf("%s: %s != %s", path, fmtNil(ek), fmtNil(ak))
}
if err := deepEqual(expect, actual, path); err != nil {
return err
}
return nil
case reflect.Slice:
if expect.Len() != actual.Len() {
return fmt.Errorf("%s: slice length unequal", path)
}
for i := 0; i < expect.Len(); i++ {
ipath := fmt.Sprintf("%s[%d]", path, i)
if err := deepEqual(expect.Index(i), actual.Index(i), ipath); err != nil {
return err
}
}
return nil
case reflect.Map:
if expect.Len() != actual.Len() {
return fmt.Errorf("%s: map length unequal", path)
}
for _, k := range expect.MapKeys() {
kpath := fmt.Sprintf("%s[%q]", path, k.String())
if err := deepEqual(expect.MapIndex(k), actual.MapIndex(k), kpath); err != nil {
return err
}
}
return nil
case reflect.Struct:
for i := 0; i < expect.NumField(); i++ {
if !expect.Field(i).CanInterface() {
continue // unexported
}
fpath := fmt.Sprintf("%s.%s", path, expect.Type().Field(i).Name)
if err := deepEqual(expect.Field(i), actual.Field(i), fpath); err != nil {
return err
}
}
return nil
case reflect.Float32, reflect.Float64:
ef, af := expect.Float(), actual.Float()
ebits, abits := math.Float64bits(ef), math.Float64bits(af)
if enan, anan := math.IsNaN(ef), math.IsNaN(af); enan || anan {
if enan != anan {
return fmt.Errorf("%s: NaN: float64(0x%x) != float64(0x%x)", path, ebits, abits)
}
return nil
}
if ebits != abits {
return fmt.Errorf("%s: float64(0x%x) != float64(0x%x)", path, ebits, abits)
}
return nil
default:
// everything else is just scalars and can be delegated
if !reflect.DeepEqual(ei, ai) {
return fmt.Errorf("%s: %v != %v", path, ei, ai)
}
return nil
}
}
func asMetadatas(i, j interface{}) (ii, jj middleware.Metadata, ok bool) {
ii, iok := i.(middleware.Metadata)
jj, jok := j.(middleware.Metadata)
return ii, jj, iok || jok
}
func asDocuments(i, j interface{}) (ii, jj documentInterface, ok bool) {
ii, iok := i.(documentInterface)
jj, jok := j.(documentInterface)
return ii, jj, iok || jok
}
func asReaders(i, j interface{}) (ii, jj io.Reader, ok bool) {
ii, iok := i.(io.Reader)
jj, jok := j.(io.Reader)
return ii, jj, iok || jok
}
func deref(v reflect.Value) reflect.Value {
switch v.Kind() {
case reflect.Interface, reflect.Ptr:
for v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr {
v = v.Elem()
}
}
return v
}
type documentInterface interface {
document.Marshaler
document.Unmarshaler
}
func compareDocumentTypes(x documentInterface, y documentInterface) bool {
if x == nil {
x = nopMarshaler{}
}
if y == nil {
y = nopMarshaler{}
}
xBytes, err := x.MarshalSmithyDocument()
if err != nil {
panic(fmt.Sprintf("MarshalSmithyDocument error: %v", err))
}
yBytes, err := y.MarshalSmithyDocument()
if err != nil {
panic(fmt.Sprintf("MarshalSmithyDocument error: %v", err))
}
return JSONEqual(xBytes, yBytes) == nil
}
// CompareReaders two io.Reader values together to determine if they are equal.
// Will read the contents of the readers until they are empty.
func CompareReaders(expect, actual io.Reader) error {
if expect == nil {
expect = nopReader{}
}
if actual == nil {
actual = nopReader{}
}
e, err := io.ReadAll(expect)
if err != nil {
return fmt.Errorf("failed to read expect body, %w", err)
}
a, err := io.ReadAll(actual)
if err != nil {
return fmt.Errorf("failed to read actual body, %w", err)
}
if !bytes.Equal(e, a) {
return fmt.Errorf("bytes do not match\nexpect:\n%s\nactual:\n%s",
hex.Dump(e), hex.Dump(a))
}
return nil
}
func fmtNil(k reflect.Kind) string {
if k == reflect.Invalid {
return "nil"
}
return "non-nil"
}
type nopReader struct{}
func (nopReader) Read(p []byte) (int, error) { return 0, io.EOF }
type nopMarshaler struct{}
func (nopMarshaler) MarshalSmithyDocument() ([]byte, error) { return nil, nil }
func (nopMarshaler) UnmarshalSmithyDocument(v interface{}) error { return nil }
|