| 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 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 14 |
"github.com/maxatome/go-testdeep/internal/types" |
| 15 |
"github.com/maxatome/go-testdeep/internal/util" |
| 16 |
) |
| 17 |
|
| 18 |
type tdArrayEach struct { |
| 19 |
baseOKNil |
| 20 |
expected reflect.Value |
| 21 |
} |
| 22 |
|
| 23 |
var _ TestDeep = &tdArrayEach{} |
| 24 |
|
| 25 |
// summary(ArrayEach): compares each array or slice item |
| 26 |
// input(ArrayEach): array,slice,ptr(ptr on array/slice) |
| 27 |
|
| 28 |
// ArrayEach operator has to be applied on arrays or slices or on |
| 29 |
// pointers on array/slice. It compares each item of data array/slice |
| 30 |
// against expectedValue. During a match, all items have to match to |
| 31 |
// succeed. |
| 32 |
// |
| 33 |
// got := [3]string{"foo", "bar", "biz"} |
| 34 |
// td.Cmp(t, got, td.ArrayEach(td.Len(3))) // succeeds |
| 35 |
// td.Cmp(t, got, td.ArrayEach(td.HasPrefix("b"))) // fails coz "foo" |
| 36 |
// |
| 37 |
// Works on slices as well: |
| 38 |
// |
| 39 |
// got := []Person{ |
| 40 |
// {Name: "Bob", Age: 42}, |
| 41 |
// {Name: "Alice", Age: 24}, |
| 42 |
// } |
| 43 |
// td.Cmp(t, got, td.ArrayEach( |
| 44 |
// td.Struct(Person{}, td.StructFields{ |
| 45 |
// Age: td.Between(20, 45), |
| 46 |
// })), |
| 47 |
// ) // succeeds, each Person has Age field between 20 and 45 |
| 48 |
func ArrayEach(expectedValue any) TestDeep { |
| 49 |
return &tdArrayEach{ |
| 50 |
baseOKNil: newBaseOKNil(3), |
| 51 |
expected: reflect.ValueOf(expectedValue), |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func (a *tdArrayEach) Match(ctx ctxerr.Context, got reflect.Value) (err *ctxerr.Error) { |
| 56 |
if !got.IsValid() { |
| 57 |
if ctx.BooleanError { |
| 58 |
return ctxerr.BooleanError |
| 59 |
} |
| 60 |
return ctx.CollectError(&ctxerr.Error{ |
| 61 |
Message: "nil value", |
| 62 |
Got: types.RawString("nil"), |
| 63 |
Expected: types.RawString("slice OR array OR *slice OR *array"), |
| 64 |
}) |
| 65 |
} |
| 66 |
|
| 67 |
switch got.Kind() { |
| 68 |
case reflect.Ptr: |
| 69 |
gotElem := got.Elem() |
| 70 |
if !gotElem.IsValid() { |
| 71 |
if ctx.BooleanError { |
| 72 |
return ctxerr.BooleanError |
| 73 |
} |
| 74 |
return ctx.CollectError(ctxerr.NilPointer(got, "non-nil *slice OR *array")) |
| 75 |
} |
| 76 |
|
| 77 |
if gotElem.Kind() != reflect.Array && gotElem.Kind() != reflect.Slice { |
| 78 |
break |
| 79 |
} |
| 80 |
got = gotElem |
| 81 |
fallthrough |
| 82 |
|
| 83 |
case reflect.Array, reflect.Slice: |
| 84 |
gotLen := got.Len() |
| 85 |
|
| 86 |
var err *ctxerr.Error |
| 87 |
for idx := 0; idx < gotLen; idx++ { |
| 88 |
err = deepValueEqual(ctx.AddArrayIndex(idx), got.Index(idx), a.expected) |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
} |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
if ctx.BooleanError { |
| 97 |
return ctxerr.BooleanError |
| 98 |
} |
| 99 |
return ctx.CollectError(ctxerr.BadKind(got, "slice OR array OR *slice OR *array")) |
| 100 |
} |
| 101 |
|
| 102 |
func (a *tdArrayEach) String() string { |
| 103 |
const prefix = "ArrayEach(" |
| 104 |
|
| 105 |
content := util.ToString(a.expected) |
| 106 |
if strings.Contains(content, "\n") { |
| 107 |
return prefix + util.IndentString(content, " ") + ")" |
| 108 |
} |
| 109 |
return prefix + content + ")" |
| 110 |
} |