Debian Codegraph @

td/td_contains_key.go

raw
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
12 "github.com/maxatome/go-testdeep/helpers/tdutil"
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 tdContainsKey struct {
19 tdSmugglerBase
20 }
21
22 var _ TestDeep = &tdContainsKey{}
23
24 // summary(ContainsKey): checks that a map contains a key
25 // input(ContainsKey): map
26
27 // ContainsKey is a smuggler operator and works on maps only. It
28 // compares each key of map against expectedValue.
29 //
30 // hash := map[string]int{"foo": 12, "bar": 34, "zip": 28}
31 // td.Cmp(t, hash, td.ContainsKey("foo")) // succeeds
32 // td.Cmp(t, hash, td.ContainsKey(td.HasPrefix("z"))) // succeeds
33 // td.Cmp(t, hash, td.ContainsKey(td.HasPrefix("x"))) // fails
34 //
35 // hnum := map[int]string{1: "foo", 42: "bar"}
36 // td.Cmp(t, hash, td.ContainsKey(42)) // succeeds
37 // td.Cmp(t, hash, td.ContainsKey(td.Between(40, 45))) // succeeds
38 //
39 // When ContainsKey(nil) is used, nil is automatically converted to a
40 // typed nil on the fly to avoid confusion (if the map key type allows
41 // it of course.) So all following [Cmp] calls are equivalent
42 // (except the (*byte)(nil) one):
43 //
44 // num := 123
45 // hnum := map[*int]bool{&num: true, nil: true}
46 // td.Cmp(t, hnum, td.ContainsKey(nil)) // succeeds → (*int)(nil)
47 // td.Cmp(t, hnum, td.ContainsKey((*int)(nil))) // succeeds
48 // td.Cmp(t, hnum, td.ContainsKey(td.Nil())) // succeeds
49 // // But...
50 // td.Cmp(t, hnum, td.ContainsKey((*byte)(nil))) // fails: (*byte)(nil) ≠ (*int)(nil)
51 //
52 // See also [Contains].
53 func ContainsKey(expectedValue any) TestDeep {
54 c := tdContainsKey{
55 tdSmugglerBase: newSmugglerBase(expectedValue),
56 }
57
58 if !c.isTestDeeper {
59 c.expectedValue = reflect.ValueOf(expectedValue)
60 }
61 return &c
62 }
63
64 func (c *tdContainsKey) doesNotContainKey(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
65 if ctx.BooleanError {
66 return ctxerr.BooleanError
67 }
68 return ctx.CollectError(&ctxerr.Error{
69 Message: "does not contain key",
70 Summary: ctxerr.ErrorSummaryItems{
71 {
72 Label: "expected key",
73 Value: util.ToString(c.expectedValue),
74 },
75 {
76 Label: "not in keys",
77 Value: util.ToString(tdutil.MapSortedKeys(got)),
78 },
79 },
80 })
81 }
82
83 // getExpectedValue returns the expected value handling the
84 // Contains(nil) case: in this case it returns a typed nil (same type
85 // as the keys of got).
86 // got is a map (it's the caller responsibility to check).
87 func (c *tdContainsKey) getExpectedValue(got reflect.Value) reflect.Value {
88 // If the expectValue is non-typed nil
89 if !c.expectedValue.IsValid() {
90 // AND the kind of items in got is...
91 switch got.Type().Key().Kind() {
92 case reflect.Chan, reflect.Func, reflect.Interface,
93 reflect.Map, reflect.Ptr, reflect.Slice:
94 // returns a typed nil
95 return reflect.Zero(got.Type().Key())
96 }
97 }
98 return c.expectedValue
99 }
100
101 func (c *tdContainsKey) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
102 if got.Kind() == reflect.Map {
103 expectedValue := c.getExpectedValue(got)
104
105 // If expected value is a TestDeep operator OR BeLax, check each key
106 if c.isTestDeeper || ctx.BeLax {
107 for _, k := range got.MapKeys() {
108 ok, err := deepValueEqualFinalOK(ctx, k, expectedValue)
109 if err != nil || ok {
110 return err
111 }
112 }
113 } else if expectedValue.IsValid() &&
114 got.Type().Key() == expectedValue.Type() &&
115 got.MapIndex(expectedValue).IsValid() {
116 return nil
117 }
118 return c.doesNotContainKey(ctx, got)
119 }
120
121 if ctx.BooleanError {
122 return ctxerr.BooleanError
123 }
124 var expectedType any
125 if c.expectedValue.IsValid() {
126 expectedType = types.RawString(c.expectedValue.Type().String())
127 } else {
128 expectedType = c
129 }
130
131 return ctx.CollectError(&ctxerr.Error{
132 Message: "cannot check contains key",
133 Got: types.RawString(got.Type().String()),
134 Expected: expectedType,
135 })
136 }
137
138 func (c *tdContainsKey) String() string {
139 return "ContainsKey(" + util.ToString(c.expectedValue) + ")"
140 }