ContainsKey

func ContainsKey(expectedValue any) TestDeep

ContainsKey is a smuggler operator and works on maps only. It compares each key of map against expectedValue.

hash := map[string]int{"foo": 12, "bar": 34, "zip": 28}
td.Cmp(t, hash, td.ContainsKey("foo"))             // succeeds
td.Cmp(t, hash, td.ContainsKey(td.HasPrefix("z"))) // succeeds
td.Cmp(t, hash, td.ContainsKey(td.HasPrefix("x"))) // fails

hnum := map[int]string{1: "foo", 42: "bar"}
td.Cmp(t, hash, td.ContainsKey(42))                 // succeeds
td.Cmp(t, hash, td.ContainsKey(td.Between(40, 45))) // succeeds

When ContainsKey(nil) is used, nil is automatically converted to a typed nil on the fly to avoid confusion (if the map key type allows it of course.) So all following Cmp calls are equivalent (except the (*byte)(nil) one):

num := 123
hnum := map[*int]bool{&num: true, nil: true}
td.Cmp(t, hnum, td.ContainsKey(nil))         // succeeds → (*int)(nil)
td.Cmp(t, hnum, td.ContainsKey((*int)(nil))) // succeeds
td.Cmp(t, hnum, td.ContainsKey(td.Nil()))    // succeeds
// But...
td.Cmp(t, hnum, td.ContainsKey((*byte)(nil))) // fails: (*byte)(nil) ≠ (*int)(nil)

See also Contains.

See also ContainsKey godoc.

Examples

Base example
Nil example

CmpContainsKey shortcut

func CmpContainsKey(t TestingT, got, expectedValue any, args ...any) bool

CmpContainsKey is a shortcut for:

td.Cmp(t, got, td.ContainsKey(expectedValue), args...)

See above for details.

Returns true if the test is OK, false if it fails.

If t is a *T then its Config field is inherited.

args… are optional and allow to name the test. This name is used in case of failure to qualify the test. If len(args) > 1 and the first item of args is a string and contains a ‘%’ rune then fmt.Fprintf is used to compose the name, else args are passed to fmt.Fprint. Do not forget it is the name of the test, not the reason of a potential failure.

See also CmpContainsKey godoc.

Examples

Base example
Nil example

T.ContainsKey shortcut

func (t *T) ContainsKey(got, expectedValue any, args ...any) bool

ContainsKey is a shortcut for:

t.Cmp(got, td.ContainsKey(expectedValue), args...)

See above for details.

Returns true if the test is OK, false if it fails.

args… are optional and allow to name the test. This name is used in case of failure to qualify the test. If len(args) > 1 and the first item of args is a string and contains a ‘%’ rune then fmt.Fprintf is used to compose the name, else args are passed to fmt.Fprint. Do not forget it is the name of the test, not the reason of a potential failure.

See also T.ContainsKey godoc.

Examples

Base example
Nil example