| 1 |
// Copyright (c) 2018-2025, 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 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 14 |
) |
| 15 |
|
| 16 |
type tdAll struct { |
| 17 |
tdListBase |
| 18 |
} |
| 19 |
|
| 20 |
var _ TestDeep = &tdAll{} |
| 21 |
|
| 22 |
// summary(All): all expected values have to match |
| 23 |
// input(All): all |
| 24 |
|
| 25 |
// All operator compares data against several expected values. During |
| 26 |
// a match, all of them have to match to succeed. Consider it |
| 27 |
// as a "AND" logical operator. |
| 28 |
// |
| 29 |
// td.Cmp(t, "foobar", td.All( |
| 30 |
// td.Len(6), |
| 31 |
// td.HasPrefix("fo"), |
| 32 |
// td.HasSuffix("ar"), |
| 33 |
// )) // succeeds |
| 34 |
// |
| 35 |
// Note [Flatten] function can be used to group or reuse some values or |
| 36 |
// operators and so avoid boring and inefficient copies: |
| 37 |
// |
| 38 |
// stringOps := td.Flatten([]td.TestDeep{td.HasPrefix("fo"), td.HasSuffix("ar")}) |
| 39 |
// td.Cmp(t, "foobar", td.All( |
| 40 |
// td.Len(6), |
| 41 |
// stringOps, |
| 42 |
// )) // succeeds |
| 43 |
// |
| 44 |
// One can do the same with All operator itself: |
| 45 |
// |
| 46 |
// stringOps := td.All(td.HasPrefix("fo"), td.HasSuffix("ar")) |
| 47 |
// td.Cmp(t, "foobar", td.All( |
| 48 |
// td.Len(6), |
| 49 |
// stringOps, |
| 50 |
// )) // succeeds |
| 51 |
// |
| 52 |
// but if an error occurs in the nested All, the report is a bit more |
| 53 |
// complex to read due to the nested level. [Flatten] does not create |
| 54 |
// a new level, its slice is just flattened in the All parameters. |
| 55 |
// |
| 56 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 57 |
// known non-interface types are equal, or if only interface types |
| 58 |
// are found (mostly issued from [Isa]) and they are equal. |
| 59 |
// |
| 60 |
// See also [Any] and [None]. |
| 61 |
func All(expectedValues ...any) TestDeep { |
| 62 |
return &tdAll{ |
| 63 |
tdListBase: newListBase(expectedValues...), |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func (a *tdAll) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 68 |
var origErr *ctxerr.Error |
| 69 |
for idx, item := range a.items { |
| 70 |
// Use deepValueEqualFinal here instead of deepValueEqual as we |
| 71 |
// want to know whether an error occurred or not, we do not want |
| 72 |
// to accumulate it silently |
| 73 |
origErr = deepValueEqualFinal( |
| 74 |
ctx.ResetErrors(). |
| 75 |
AddCustomLevel(fmt.Sprintf("<All#%d/%d>", idx+1, len(a.items))), |
| 76 |
got, item) |
| 77 |
if origErr != nil { |
| 78 |
if ctx.BooleanError { |
| 79 |
return ctxerr.BooleanError |
| 80 |
} |
| 81 |
err := &ctxerr.Error{ |
| 82 |
Message: fmt.Sprintf("compared (part %d of %d)", idx+1, len(a.items)), |
| 83 |
Got: got, |
| 84 |
Expected: item, |
| 85 |
} |
| 86 |
if item.IsValid() && item.Type().Implements(testDeeper) { |
| 87 |
err.Origin = origErr |
| 88 |
} |
| 89 |
return ctx.CollectError(err) |
| 90 |
} |
| 91 |
} |
| 92 |
return nil |
| 93 |
} |
| 94 |
|
| 95 |
func (a *tdAll) TypeBehind() reflect.Type { |
| 96 |
return uniqTypeBehindSlice(a.items) |
| 97 |
} |