| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
// |
| 7 |
// deepValueEqual function is heavily based on reflect.deepValueEqual function |
| 8 |
// licensed under the BSD-style license found in the LICENSE file in the |
| 9 |
// golang repository: https://github.com/golang/go/blob/master/LICENSE |
| 10 |
|
| 11 |
package td |
| 12 |
|
| 13 |
import ( |
| 14 |
"fmt" |
| 15 |
"reflect" |
| 16 |
|
| 17 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 18 |
"github.com/maxatome/go-testdeep/internal/color" |
| 19 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 20 |
"github.com/maxatome/go-testdeep/internal/dark" |
| 21 |
"github.com/maxatome/go-testdeep/internal/types" |
| 22 |
) |
| 23 |
|
| 24 |
func isNilStr(isNil bool) types.RawString { |
| 25 |
if isNil { |
| 26 |
return "nil" |
| 27 |
} |
| 28 |
return "not nil" |
| 29 |
} |
| 30 |
|
| 31 |
func deepValueEqualFinal(ctx ctxerr.Context, got, expected reflect.Value) (err *ctxerr.Error) { |
| 32 |
err = deepValueEqual(ctx, got, expected) |
| 33 |
if err == nil { |
| 34 |
// Try to merge pending errors |
| 35 |
errMerge := ctx.MergeErrors() |
| 36 |
if errMerge != nil { |
| 37 |
return errMerge |
| 38 |
} |
| 39 |
} |
| 40 |
return |
| 41 |
} |
| 42 |
|
| 43 |
func deepValueEqualFinalOK(ctx ctxerr.Context, got, expected reflect.Value) (bool, *ctxerr.Error) { |
| 44 |
ctx = ctx.ResetErrors() |
| 45 |
ctx.BooleanError = true |
| 46 |
|
| 47 |
if err := deepValueEqualFinal(ctx, got, expected); err != nil { |
| 48 |
if err == ctxerr.BooleanError { |
| 49 |
return false, nil |
| 50 |
} |
| 51 |
// The error is a user error |
| 52 |
return false, err |
| 53 |
} |
| 54 |
return true, nil |
| 55 |
} |
| 56 |
|
| 57 |
// nilHandler is called when one of got or expected is nil (but never |
| 58 |
// both, it is caller responsibility). |
| 59 |
func nilHandler(ctx ctxerr.Context, got, expected reflect.Value) *ctxerr.Error { |
| 60 |
err := ctxerr.Error{} |
| 61 |
|
| 62 |
if expected.IsValid() { // here: !got.IsValid() |
| 63 |
if expected.Type().Implements(testDeeper) { |
| 64 |
curOperator := dark.MustGetInterface(expected).(TestDeep) |
| 65 |
if curOperator.GetLocation().IsInitialized() { |
| 66 |
ctx.CurOperator = curOperator |
| 67 |
} |
| 68 |
if curOperator.HandleInvalid() { |
| 69 |
return curOperator.Match(ctx, got) |
| 70 |
} |
| 71 |
if ctx.BooleanError { |
| 72 |
return ctxerr.BooleanError |
| 73 |
} |
| 74 |
|
| 75 |
// Special case if expected is a TestDeep operator which does |
| 76 |
// not handle invalid values: the operator is not called, but |
| 77 |
// for the user the error comes from it |
| 78 |
} else if ctx.BooleanError { |
| 79 |
return ctxerr.BooleanError |
| 80 |
} |
| 81 |
|
| 82 |
err.Expected = expected |
| 83 |
} else { // here: !expected.IsValid() && got.IsValid() |
| 84 |
switch got.Kind() { |
| 85 |
// Special case: got is a nil interface, so consider as equal |
| 86 |
// to expected nil. |
| 87 |
case reflect.Interface: |
| 88 |
if got.IsNil() { |
| 89 |
return nil |
| 90 |
} |
| 91 |
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice: |
| 92 |
// If BeLax, it is OK: we consider typed nil is equal to (untyped) nil |
| 93 |
if ctx.BeLax && got.IsNil() { |
| 94 |
return nil |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ctx.BooleanError { |
| 99 |
return ctxerr.BooleanError |
| 100 |
} |
| 101 |
|
| 102 |
err.Got = got |
| 103 |
} |
| 104 |
|
| 105 |
err.Message = "values differ" |
| 106 |
return ctx.CollectError(&err) |
| 107 |
} |
| 108 |
|
| 109 |
func isCustomEqual(a, b reflect.Value) (bool, bool) { |
| 110 |
aType, bType := a.Type(), b.Type() |
| 111 |
|
| 112 |
equal, ok := aType.MethodByName("Equal") |
| 113 |
if ok { |
| 114 |
ft := equal.Type |
| 115 |
if !ft.IsVariadic() && |
| 116 |
ft.NumIn() == 2 && |
| 117 |
ft.NumOut() == 1 && |
| 118 |
ft.In(0).AssignableTo(ft.In(1)) && |
| 119 |
ft.Out(0) == types.Bool && |
| 120 |
bType.AssignableTo(ft.In(1)) { |
| 121 |
return true, equal.Func.Call([]reflect.Value{a, b})[0].Bool() |
| 122 |
} |
| 123 |
} |
| 124 |
return false, false |
| 125 |
} |
| 126 |
|
| 127 |
// resolveAnchor does the same as ctx.Anchors.ResolveAnchor but checks |
| 128 |
// whether v is valid and not already a TestDeep operator first. |
| 129 |
func resolveAnchor(ctx ctxerr.Context, v reflect.Value) (reflect.Value, bool) { |
| 130 |
if !v.IsValid() || v.Type().Implements(testDeeper) { |
| 131 |
return v, false |
| 132 |
} |
| 133 |
return ctx.Anchors.ResolveAnchor(v) |
| 134 |
} |
| 135 |
|
| 136 |
func deepValueEqual(ctx ctxerr.Context, got, expected reflect.Value) (err *ctxerr.Error) { |
| 137 |
if !ctx.TestDeepInGotOK { |
| 138 |
// got must not implement testDeeper |
| 139 |
if got.IsValid() && got.Type().Implements(testDeeper) { |
| 140 |
panic(color.Bad("Found a TestDeep operator in got param, " + |
| 141 |
"can only use it in expected one!")) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Try to see if a TestDeep operator is anchored in expected |
| 146 |
if op, ok := resolveAnchor(ctx, expected); ok { |
| 147 |
expected = op |
| 148 |
} |
| 149 |
|
| 150 |
if got.IsValid() { |
| 151 |
// Check if a Smuggle hook matches got type |
| 152 |
if handled, e := ctx.Hooks.Smuggle(&got); handled && e != nil { |
| 153 |
// ctx.BooleanError is always false here as hooks cannot be set globally |
| 154 |
return ctx.CollectError(&ctxerr.Error{ |
| 155 |
Message: e.Error(), |
| 156 |
Got: got, |
| 157 |
Expected: expected, |
| 158 |
}) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if !got.IsValid() || !expected.IsValid() { |
| 163 |
if got.IsValid() == expected.IsValid() { |
| 164 |
return |
| 165 |
} |
| 166 |
return nilHandler(ctx, got, expected) |
| 167 |
} |
| 168 |
|
| 169 |
// Check if a Cmp hook matches got & expected types |
| 170 |
if handled, e := ctx.Hooks.Cmp(got, expected); handled { |
| 171 |
if e == nil { |
| 172 |
return |
| 173 |
} |
| 174 |
// ctx.BooleanError is always false here as hooks cannot be set globally |
| 175 |
return ctx.CollectError(&ctxerr.Error{ |
| 176 |
Message: e.Error(), |
| 177 |
Got: got, |
| 178 |
Expected: expected, |
| 179 |
}) |
| 180 |
} |
| 181 |
|
| 182 |
// Look for an Equal() method |
| 183 |
if ctx.UseEqual || ctx.Hooks.UseEqual(got.Type()) { |
| 184 |
hasEqual, isEqual := isCustomEqual(got, expected) |
| 185 |
if hasEqual { |
| 186 |
if isEqual { |
| 187 |
return |
| 188 |
} |
| 189 |
if ctx.BooleanError { |
| 190 |
return ctxerr.BooleanError |
| 191 |
} |
| 192 |
return ctx.CollectError(&ctxerr.Error{ |
| 193 |
Message: "got.Equal(expected) failed", |
| 194 |
Got: got, |
| 195 |
Expected: expected, |
| 196 |
}) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if got.Type() != expected.Type() { |
| 201 |
if expected.Type().Implements(testDeeper) { |
| 202 |
curOperator := dark.MustGetInterface(expected).(TestDeep) |
| 203 |
|
| 204 |
// Resolve interface |
| 205 |
if got.Kind() == reflect.Interface { |
| 206 |
got = got.Elem() |
| 207 |
|
| 208 |
if !got.IsValid() { |
| 209 |
return nilHandler(ctx, got, expected) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if curOperator.GetLocation().IsInitialized() { |
| 214 |
ctx.CurOperator = curOperator |
| 215 |
} |
| 216 |
return curOperator.Match(ctx, got) |
| 217 |
} |
| 218 |
|
| 219 |
// expected is not a TestDeep operator |
| 220 |
|
| 221 |
if got.Type() == recvKindType || expected.Type() == recvKindType { |
| 222 |
if ctx.BooleanError { |
| 223 |
return ctxerr.BooleanError |
| 224 |
} |
| 225 |
return ctx.CollectError(&ctxerr.Error{ |
| 226 |
Message: "values differ", |
| 227 |
Got: got, |
| 228 |
Expected: expected, |
| 229 |
}) |
| 230 |
} |
| 231 |
|
| 232 |
if ctx.BeLax && types.IsConvertible(expected, got.Type()) { |
| 233 |
return deepValueEqual(ctx, got, expected.Convert(got.Type())) |
| 234 |
} |
| 235 |
|
| 236 |
// If got is an interface, try to see what is behind before failing |
| 237 |
// Used by Set/Bag Match method in such cases: |
| 238 |
// []any{123, "foo"} → Bag("foo", 123) |
| 239 |
// Interface kind -^-----^ but String-^ and ^- Int kinds |
| 240 |
if got.Kind() == reflect.Interface { |
| 241 |
return deepValueEqual(ctx, got.Elem(), expected) |
| 242 |
} |
| 243 |
|
| 244 |
if ctx.BooleanError { |
| 245 |
return ctxerr.BooleanError |
| 246 |
} |
| 247 |
return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), expected.Type())) |
| 248 |
} |
| 249 |
|
| 250 |
// if ctx.Depth > 10 { panic("deepValueEqual") } // for debugging |
| 251 |
|
| 252 |
// Avoid looping forever on cyclic references |
| 253 |
if ctx.Visited.Record(got, expected) { |
| 254 |
return |
| 255 |
} |
| 256 |
|
| 257 |
switch got.Kind() { |
| 258 |
case reflect.Array: |
| 259 |
for i, l := 0, got.Len(); i < l; i++ { |
| 260 |
err = deepValueEqual(ctx.AddArrayIndex(i), |
| 261 |
got.Index(i), expected.Index(i)) |
| 262 |
if err != nil { |
| 263 |
return |
| 264 |
} |
| 265 |
} |
| 266 |
return |
| 267 |
|
| 268 |
case reflect.Slice: |
| 269 |
if got.IsNil() != expected.IsNil() { |
| 270 |
if ctx.BooleanError { |
| 271 |
return ctxerr.BooleanError |
| 272 |
} |
| 273 |
return ctx.CollectError(&ctxerr.Error{ |
| 274 |
Message: "nil slice", |
| 275 |
Got: isNilStr(got.IsNil()), |
| 276 |
Expected: isNilStr(expected.IsNil()), |
| 277 |
}) |
| 278 |
} |
| 279 |
|
| 280 |
var ( |
| 281 |
gotLen = got.Len() |
| 282 |
expectedLen = expected.Len() |
| 283 |
) |
| 284 |
|
| 285 |
if gotLen != expectedLen { |
| 286 |
// Shortcut in boolean context |
| 287 |
if ctx.BooleanError { |
| 288 |
return ctxerr.BooleanError |
| 289 |
} |
| 290 |
} else { |
| 291 |
if got.Pointer() == expected.Pointer() { |
| 292 |
return |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
var maxLen int |
| 297 |
if gotLen >= expectedLen { |
| 298 |
maxLen = expectedLen |
| 299 |
} else { |
| 300 |
maxLen = gotLen |
| 301 |
} |
| 302 |
|
| 303 |
// Special case for internal tuple type: it is clearer to read |
| 304 |
// TUPLE instead of DATA when an error occurs when using this type |
| 305 |
if got.Type() == tupleType && |
| 306 |
ctx.Path.Len() == 1 && ctx.Path.String() == contextDefaultRootName { |
| 307 |
ctx = ctx.ResetPath("TUPLE") |
| 308 |
} |
| 309 |
|
| 310 |
for i := 0; i < maxLen; i++ { |
| 311 |
err = deepValueEqual(ctx.AddArrayIndex(i), |
| 312 |
got.Index(i), expected.Index(i)) |
| 313 |
if err != nil { |
| 314 |
return |
| 315 |
} |
| 316 |
} |
| 317 |
if gotLen == expectedLen { |
| 318 |
return |
| 319 |
} |
| 320 |
|
| 321 |
res := tdSetResult{ |
| 322 |
Kind: itemsSetResult, |
| 323 |
// do not sort Extra/Mising here |
| 324 |
} |
| 325 |
|
| 326 |
if gotLen > expectedLen { |
| 327 |
res.Extra = make([]reflect.Value, gotLen-expectedLen) |
| 328 |
for i := expectedLen; i < gotLen; i++ { |
| 329 |
res.Extra[i-expectedLen] = got.Index(i) |
| 330 |
} |
| 331 |
} else { |
| 332 |
res.Missing = make([]reflect.Value, expectedLen-gotLen) |
| 333 |
for i := gotLen; i < expectedLen; i++ { |
| 334 |
res.Missing[i-gotLen] = expected.Index(i) |
| 335 |
} |
| 336 |
} |
| 337 |
return ctx.CollectError(&ctxerr.Error{ |
| 338 |
Message: fmt.Sprintf("comparing slices, from index #%d", maxLen), |
| 339 |
Summary: res.Summary(), |
| 340 |
}) |
| 341 |
|
| 342 |
case reflect.Interface: |
| 343 |
return deepValueEqual(ctx, got.Elem(), expected.Elem()) |
| 344 |
|
| 345 |
case reflect.Ptr: |
| 346 |
if got.Pointer() == expected.Pointer() { |
| 347 |
return |
| 348 |
} |
| 349 |
return deepValueEqual(ctx.AddPtr(1), got.Elem(), expected.Elem()) |
| 350 |
|
| 351 |
case reflect.Struct: |
| 352 |
sType := got.Type() |
| 353 |
ignoreUnexported := ctx.IgnoreUnexported || ctx.Hooks.IgnoreUnexported(sType) |
| 354 |
for i, n := 0, got.NumField(); i < n; i++ { |
| 355 |
field := sType.Field(i) |
| 356 |
if ignoreUnexported && field.PkgPath != "" { |
| 357 |
continue |
| 358 |
} |
| 359 |
err = deepValueEqual(ctx.AddField(field.Name), |
| 360 |
got.Field(i), expected.Field(i)) |
| 361 |
if err != nil { |
| 362 |
return |
| 363 |
} |
| 364 |
} |
| 365 |
return |
| 366 |
|
| 367 |
case reflect.Map: |
| 368 |
if got.IsNil() != expected.IsNil() { |
| 369 |
if ctx.BooleanError { |
| 370 |
return ctxerr.BooleanError |
| 371 |
} |
| 372 |
return ctx.CollectError(&ctxerr.Error{ |
| 373 |
Message: "nil map", |
| 374 |
Got: isNilStr(got.IsNil()), |
| 375 |
Expected: isNilStr(expected.IsNil()), |
| 376 |
}) |
| 377 |
} |
| 378 |
|
| 379 |
// Shortcut in boolean context |
| 380 |
if ctx.BooleanError && got.Len() != expected.Len() { |
| 381 |
return ctxerr.BooleanError |
| 382 |
} |
| 383 |
|
| 384 |
if got.Pointer() == expected.Pointer() { |
| 385 |
return |
| 386 |
} |
| 387 |
|
| 388 |
var notFoundKeys []reflect.Value |
| 389 |
foundKeys := map[any]bool{} |
| 390 |
|
| 391 |
for _, vkey := range tdutil.MapSortedKeys(expected) { |
| 392 |
gotValue := got.MapIndex(vkey) |
| 393 |
if !gotValue.IsValid() { |
| 394 |
notFoundKeys = append(notFoundKeys, vkey) |
| 395 |
continue |
| 396 |
} |
| 397 |
|
| 398 |
err = deepValueEqual(ctx.AddMapKey(vkey), |
| 399 |
gotValue, expected.MapIndex(vkey)) |
| 400 |
if err != nil { |
| 401 |
return |
| 402 |
} |
| 403 |
foundKeys[dark.MustGetInterface(vkey)] = true |
| 404 |
} |
| 405 |
|
| 406 |
if got.Len() == len(foundKeys) { |
| 407 |
if len(notFoundKeys) == 0 { |
| 408 |
return |
| 409 |
} |
| 410 |
return ctx.CollectError(&ctxerr.Error{ |
| 411 |
Message: "comparing map", |
| 412 |
Summary: (tdSetResult{ |
| 413 |
Kind: keysSetResult, |
| 414 |
Missing: notFoundKeys, |
| 415 |
Sort: true, |
| 416 |
}).Summary(), |
| 417 |
}) |
| 418 |
} |
| 419 |
|
| 420 |
if ctx.BooleanError { |
| 421 |
return ctxerr.BooleanError |
| 422 |
} |
| 423 |
|
| 424 |
// Retrieve extra keys |
| 425 |
res := tdSetResult{ |
| 426 |
Kind: keysSetResult, |
| 427 |
Missing: notFoundKeys, |
| 428 |
Extra: make([]reflect.Value, 0, got.Len()-len(foundKeys)), |
| 429 |
Sort: true, |
| 430 |
} |
| 431 |
|
| 432 |
for _, vkey := range tdutil.MapSortedKeys(got) { |
| 433 |
if !foundKeys[dark.MustGetInterface(vkey)] { |
| 434 |
res.Extra = append(res.Extra, vkey) |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
return ctx.CollectError(&ctxerr.Error{ |
| 439 |
Message: "comparing map", |
| 440 |
Summary: res.Summary(), |
| 441 |
}) |
| 442 |
|
| 443 |
case reflect.Func: |
| 444 |
if got.IsNil() && expected.IsNil() { |
| 445 |
return |
| 446 |
} |
| 447 |
if ctx.BooleanError { |
| 448 |
return ctxerr.BooleanError |
| 449 |
} |
| 450 |
// Can't do better than this: |
| 451 |
return ctx.CollectError(&ctxerr.Error{ |
| 452 |
Message: "functions mismatch", |
| 453 |
Summary: ctxerr.NewSummary("<can not be compared>"), |
| 454 |
}) |
| 455 |
|
| 456 |
default: |
| 457 |
// Normal equality suffices |
| 458 |
if dark.MustGetInterface(got) == dark.MustGetInterface(expected) { |
| 459 |
return |
| 460 |
} |
| 461 |
if ctx.BooleanError { |
| 462 |
return ctxerr.BooleanError |
| 463 |
} |
| 464 |
return ctx.CollectError(&ctxerr.Error{ |
| 465 |
Message: "values differ", |
| 466 |
Got: got, |
| 467 |
Expected: expected, |
| 468 |
}) |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
func deepValueEqualOK(got, expected reflect.Value) bool { |
| 473 |
return deepValueEqualFinal(newBooleanContext(), got, expected) == nil |
| 474 |
} |
| 475 |
|
| 476 |
// EqDeeply returns true if got matches expected. expected can |
| 477 |
// be the same type as got is, or contains some [TestDeep] operators. |
| 478 |
// |
| 479 |
// got := "foobar" |
| 480 |
// td.EqDeeply(got, "foobar") // returns true |
| 481 |
// td.EqDeeply(got, td.HasPrefix("foo")) // returns true |
| 482 |
func EqDeeply(got, expected any) bool { |
| 483 |
return deepValueEqualOK(reflect.ValueOf(got), reflect.ValueOf(expected)) |
| 484 |
} |
| 485 |
|
| 486 |
// EqDeeplyError returns nil if got matches expected. expected can be |
| 487 |
// the same type as got is, or contains some [TestDeep] operators. If |
| 488 |
// got does not match expected, the returned [*ctxerr.Error] contains |
| 489 |
// the reason of the first mismatch detected. |
| 490 |
// |
| 491 |
// got := "foobar" |
| 492 |
// if err := td.EqDeeplyError(got, "foobar"); err != nil { |
| 493 |
// // … |
| 494 |
// } |
| 495 |
// if err := td.EqDeeplyError(got, td.HasPrefix("foo")); err != nil { |
| 496 |
// // … |
| 497 |
// } |
| 498 |
func EqDeeplyError(got, expected any) error { |
| 499 |
err := deepValueEqualFinal(newContext(nil), |
| 500 |
reflect.ValueOf(got), reflect.ValueOf(expected)) |
| 501 |
if err == nil { |
| 502 |
return nil |
| 503 |
} |
| 504 |
return err |
| 505 |
} |