Contains

func Contains(expectedValue any) TestDeep

Contains is a smuggler operator to check if something is contained in another thing. Contains has to be applied on arrays, slices, maps or strings. It tries to be as smarter as possible.

If expectedValue is a TestDeep operator, each item of data array/slice/map/string (rune for strings) is compared to it. The use of a TestDeep operator as expectedValue works only in this way: item per item.

If data is a slice, and expectedValue has the same type, then expectedValue is searched as a sub-slice, otherwise expectedValue is compared to each slice value.

list := []int{12, 34, 28}
td.Cmp(t, list, td.Contains(34))                 // succeeds
td.Cmp(t, list, td.Contains(td.Between(30, 35))) // succeeds too
td.Cmp(t, list, td.Contains(35))                 // fails
td.Cmp(t, list, td.Contains([]int{34, 28}))      // succeeds

If data is an array or a map, each value is compared to expectedValue. Map keys are not checked: see ContainsKey to check map keys existence.

hash := map[string]int{"foo": 12, "bar": 34, "zip": 28}
td.Cmp(t, hash, td.Contains(34))                 // succeeds
td.Cmp(t, hash, td.Contains(td.Between(30, 35))) // succeeds too
td.Cmp(t, hash, td.Contains(35))                 // fails

array := [...]int{12, 34, 28}
td.Cmp(t, array, td.Contains(34))                 // succeeds
td.Cmp(t, array, td.Contains(td.Between(30, 35))) // succeeds too
td.Cmp(t, array, td.Contains(35))                 // fails

If data is a string (or convertible), []byte (or convertible), error or fmt.Stringer interface (error interface is tested before fmt.Stringer), expectedValue can be a string, a []byte, a rune or a byte. In this case, it tests if the got string contains this expected string, []byte, rune or byte.

got := "foo bar"
td.Cmp(t, got, td.Contains('o'))                  // succeeds
td.Cmp(t, got, td.Contains(rune('o')))            // succeeds
td.Cmp(t, got, td.Contains(td.Between('n', 'p'))) // succeeds
td.Cmp(t, got, td.Contains("bar"))                // succeeds
td.Cmp(t, got, td.Contains([]byte("bar")))        // succeeds

td.Cmp(t, []byte("foobar"), td.Contains("ooba")) // succeeds

type Foobar string
td.Cmp(t, Foobar("foobar"), td.Contains("ooba")) // succeeds

err := errors.New("error!")
td.Cmp(t, err, td.Contains("ror")) // succeeds

bstr := bytes.NewBufferString("fmt.Stringer!")
td.Cmp(t, bstr, td.Contains("String")) // succeeds

Pitfall: if you want to check if 2 words are contained in got, don’t do:

td.Cmp(t, "foobar", td.Contains(td.All("foo", "bar"))) // Bad!

as TestDeep operator All in Contains operates on each rune, so it does not work as expected, but do::

td.Cmp(t, "foobar", td.All(td.Contains("foo"), td.Contains("bar")))

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

num := 123
list := []*int{&num, nil}
td.Cmp(t, list, td.Contains(nil))         // succeeds → (*int)(nil)
td.Cmp(t, list, td.Contains((*int)(nil))) // succeeds
td.Cmp(t, list, td.Contains(td.Nil()))    // succeeds
// But...
td.Cmp(t, list, td.Contains((*byte)(nil))) // fails: (*byte)(nil) ≠ (*int)(nil)

As well as these ones:

hash := map[string]*int{"foo": nil, "bar": &num}
td.Cmp(t, hash, td.Contains(nil))         // succeeds → (*int)(nil)
td.Cmp(t, hash, td.Contains((*int)(nil))) // succeeds
td.Cmp(t, hash, td.Contains(td.Nil()))    // succeeds

See also ContainsKey.

See also Contains godoc.

Examples

ArraySlice example
Nil example
Map example
String example
Stringer example
Error example

CmpContains shortcut

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

CmpContains is a shortcut for:

td.Cmp(t, got, td.Contains(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 CmpContains godoc.

Examples

ArraySlice example
Nil example
Map example
String example
Stringer example
Error example

T.Contains shortcut

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

Contains is a shortcut for:

t.Cmp(got, td.Contains(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.Contains godoc.

Examples

ArraySlice example
Nil example
Map example
String example
Stringer example
Error example