| 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/helpers/tdutil" |
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/types" |
| 16 |
"github.com/maxatome/go-testdeep/internal/util" |
| 17 |
) |
| 18 |
|
| 19 |
type tdMapEach struct { |
| 20 |
baseOKNil |
| 21 |
expected reflect.Value |
| 22 |
} |
| 23 |
|
| 24 |
var _ TestDeep = &tdMapEach{} |
| 25 |
|
| 26 |
// summary(MapEach): compares each map entry |
| 27 |
// input(MapEach): map,ptr(ptr on map) |
| 28 |
|
| 29 |
// MapEach operator has to be applied on maps. It compares each value |
| 30 |
// of data map against expectedValue. During a match, all values have |
| 31 |
// to match to succeed. |
| 32 |
// |
| 33 |
// got := map[string]string{"test": "foo", "buzz": "bar"} |
| 34 |
// td.Cmp(t, got, td.MapEach("bar")) // fails, coz "foo" ≠ "bar" |
| 35 |
// td.Cmp(t, got, td.MapEach(td.Len(3))) // succeeds as values are 3 chars long |
| 36 |
func MapEach(expectedValue any) TestDeep { |
| 37 |
return &tdMapEach{ |
| 38 |
baseOKNil: newBaseOKNil(3), |
| 39 |
expected: reflect.ValueOf(expectedValue), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func (m *tdMapEach) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 44 |
if !got.IsValid() { |
| 45 |
if ctx.BooleanError { |
| 46 |
return ctxerr.BooleanError |
| 47 |
} |
| 48 |
return ctx.CollectError(&ctxerr.Error{ |
| 49 |
Message: "nil value", |
| 50 |
Got: types.RawString("nil"), |
| 51 |
Expected: types.RawString("map OR *map"), |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
switch got.Kind() { |
| 56 |
case reflect.Ptr: |
| 57 |
gotElem := got.Elem() |
| 58 |
if !gotElem.IsValid() { |
| 59 |
if ctx.BooleanError { |
| 60 |
return ctxerr.BooleanError |
| 61 |
} |
| 62 |
return ctx.CollectError(ctxerr.NilPointer(got, "non-nil *map")) |
| 63 |
} |
| 64 |
|
| 65 |
if gotElem.Kind() != reflect.Map { |
| 66 |
break |
| 67 |
} |
| 68 |
got = gotElem |
| 69 |
fallthrough |
| 70 |
|
| 71 |
case reflect.Map: |
| 72 |
var err *ctxerr.Error |
| 73 |
tdutil.MapEach(got, func(k, v reflect.Value) bool { |
| 74 |
err = deepValueEqual(ctx.AddMapKey(k), v, m.expected) |
| 75 |
return err == nil |
| 76 |
}) |
| 77 |
return err |
| 78 |
} |
| 79 |
|
| 80 |
if ctx.BooleanError { |
| 81 |
return ctxerr.BooleanError |
| 82 |
} |
| 83 |
return ctx.CollectError(ctxerr.BadKind(got, "map OR *map")) |
| 84 |
} |
| 85 |
|
| 86 |
func (m *tdMapEach) String() string { |
| 87 |
const prefix = "MapEach(" |
| 88 |
|
| 89 |
content := util.ToString(m.expected) |
| 90 |
if strings.Contains(content, "\n") { |
| 91 |
return prefix + util.IndentString(content, " ") + ")" |
| 92 |
} |
| 93 |
return prefix + content + ")" |
| 94 |
} |