| 1 |
// Copyright (c) 2019, 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 |
|
| 12 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 13 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 14 |
"github.com/maxatome/go-testdeep/internal/util" |
| 15 |
) |
| 16 |
|
| 17 |
type tdKVBase struct { |
| 18 |
tdSmugglerBase |
| 19 |
} |
| 20 |
|
| 21 |
func (b *tdKVBase) initKVBase(val any) bool { |
| 22 |
b.tdSmugglerBase = newSmugglerBase(val, 1) |
| 23 |
|
| 24 |
if vval := reflect.ValueOf(val); vval.IsValid() { |
| 25 |
if b.isTestDeeper { |
| 26 |
return true |
| 27 |
} |
| 28 |
|
| 29 |
if vval.Kind() == reflect.Slice { |
| 30 |
b.expectedValue = vval |
| 31 |
return true |
| 32 |
} |
| 33 |
} |
| 34 |
return false |
| 35 |
} |
| 36 |
|
| 37 |
type tdKeys struct { |
| 38 |
tdKVBase |
| 39 |
} |
| 40 |
|
| 41 |
var _ TestDeep = &tdKeys{} |
| 42 |
|
| 43 |
// summary(Keys): checks keys of a map |
| 44 |
// input(Keys): map |
| 45 |
|
| 46 |
// Keys is a smuggler operator. It takes a map and compares its |
| 47 |
// ordered keys to val. |
| 48 |
// |
| 49 |
// val can be a slice of items of the same type as the map keys: |
| 50 |
// |
| 51 |
// got := map[string]bool{"c": true, "a": false, "b": true} |
| 52 |
// td.Cmp(t, got, td.Keys([]string{"a", "b", "c"})) // succeeds, keys sorted |
| 53 |
// td.Cmp(t, got, td.Keys([]string{"c", "a", "b"})) // fails as not sorted |
| 54 |
// |
| 55 |
// as well as an other operator as [Bag], for example, to test keys in |
| 56 |
// an unsorted manner: |
| 57 |
// |
| 58 |
// got := map[string]bool{"c": true, "a": false, "b": true} |
| 59 |
// td.Cmp(t, got, td.Keys(td.Bag("c", "a", "b"))) // succeeds |
| 60 |
// |
| 61 |
// See also [Values] and [ContainsKey]. |
| 62 |
func Keys(val any) TestDeep { |
| 63 |
k := tdKeys{} |
| 64 |
if !k.initKVBase(val) { |
| 65 |
k.err = ctxerr.OpBadUsage("Keys", "(TESTDEEP_OPERATOR|SLICE)", val, 1, true) |
| 66 |
} |
| 67 |
return &k |
| 68 |
} |
| 69 |
|
| 70 |
func (k *tdKeys) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 71 |
if k.err != nil { |
| 72 |
return ctx.CollectError(k.err) |
| 73 |
} |
| 74 |
|
| 75 |
if got.Kind() != reflect.Map { |
| 76 |
if ctx.BooleanError { |
| 77 |
return ctxerr.BooleanError |
| 78 |
} |
| 79 |
return ctx.CollectError(ctxerr.BadKind(got, reflect.Map.String())) |
| 80 |
} |
| 81 |
|
| 82 |
// Build a sorted slice of keys |
| 83 |
l := got.Len() |
| 84 |
keys := reflect.MakeSlice(reflect.SliceOf(got.Type().Key()), l, l) |
| 85 |
for i, k := range tdutil.MapSortedKeys(got) { |
| 86 |
keys.Index(i).Set(k) |
| 87 |
} |
| 88 |
return deepValueEqual(ctx.AddFunctionCall("keys"), keys, k.expectedValue) |
| 89 |
} |
| 90 |
|
| 91 |
func (k *tdKeys) String() string { |
| 92 |
if k.err != nil { |
| 93 |
return k.stringError() |
| 94 |
} |
| 95 |
if k.isTestDeeper { |
| 96 |
return "keys: " + k.expectedValue.Interface().(TestDeep).String() |
| 97 |
} |
| 98 |
return "keys=" + util.ToString(k.expectedValue.Interface()) |
| 99 |
} |
| 100 |
|
| 101 |
type tdValues struct { |
| 102 |
tdKVBase |
| 103 |
} |
| 104 |
|
| 105 |
var _ TestDeep = &tdValues{} |
| 106 |
|
| 107 |
// summary(Values): checks values of a map |
| 108 |
// input(Values): map |
| 109 |
|
| 110 |
// Values is a smuggler operator. It takes a map and compares its |
| 111 |
// ordered values to val. |
| 112 |
// |
| 113 |
// val can be a slice of items of the same type as the map values: |
| 114 |
// |
| 115 |
// got := map[int]string{3: "c", 1: "a", 2: "b"} |
| 116 |
// td.Cmp(t, got, td.Values([]string{"a", "b", "c"})) // succeeds, values sorted |
| 117 |
// td.Cmp(t, got, td.Values([]string{"c", "a", "b"})) // fails as not sorted |
| 118 |
// |
| 119 |
// as well as an other operator as [Bag], for example, to test values in |
| 120 |
// an unsorted manner: |
| 121 |
// |
| 122 |
// got := map[int]string{3: "c", 1: "a", 2: "b"} |
| 123 |
// td.Cmp(t, got, td.Values(td.Bag("c", "a", "b"))) // succeeds |
| 124 |
// |
| 125 |
// See also [Keys]. |
| 126 |
func Values(val any) TestDeep { |
| 127 |
v := tdValues{} |
| 128 |
if !v.initKVBase(val) { |
| 129 |
v.err = ctxerr.OpBadUsage("Values", "(TESTDEEP_OPERATOR|SLICE)", val, 1, true) |
| 130 |
} |
| 131 |
return &v |
| 132 |
} |
| 133 |
|
| 134 |
func (v *tdValues) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 135 |
if v.err != nil { |
| 136 |
return ctx.CollectError(v.err) |
| 137 |
} |
| 138 |
|
| 139 |
if got.Kind() != reflect.Map { |
| 140 |
if ctx.BooleanError { |
| 141 |
return ctxerr.BooleanError |
| 142 |
} |
| 143 |
return ctx.CollectError(ctxerr.BadKind(got, reflect.Map.String())) |
| 144 |
} |
| 145 |
|
| 146 |
// Build a sorted slice of values |
| 147 |
l := got.Len() |
| 148 |
values := reflect.MakeSlice(reflect.SliceOf(got.Type().Elem()), l, l) |
| 149 |
for i, v := range tdutil.MapSortedValues(got) { |
| 150 |
values.Index(i).Set(v) |
| 151 |
} |
| 152 |
return deepValueEqual(ctx.AddFunctionCall("values"), values, v.expectedValue) |
| 153 |
} |
| 154 |
|
| 155 |
func (v *tdValues) String() string { |
| 156 |
if v.err != nil { |
| 157 |
return v.stringError() |
| 158 |
} |
| 159 |
if v.isTestDeeper { |
| 160 |
return "values: " + v.expectedValue.Interface().(TestDeep).String() |
| 161 |
} |
| 162 |
return "values=" + util.ToString(v.expectedValue.Interface()) |
| 163 |
} |