Contains

func Contains(expectedValue interface{}) TestDeep

Contains is a smuggler operator with a little convenient exception for strings. Contains has to be applied on arrays, slices, maps or strings. It compares each item of data array/slice/map/string (rune for strings) against expectedValue.

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

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

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

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}
Cmp(t, list, Contains(nil))         // succeeds → (*int)(nil)
Cmp(t, list, Contains((*int)(nil))) // succeeds
Cmp(t, list, Contains(Nil()))       // succeeds
// But...
Cmp(t, list, Contains((*byte)(nil))) // fails: (*byte)(nil)  (*int)(nil)

As well as these ones:

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

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

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

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

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

See also Contains godoc.

Examples

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

CmpContains shortcut

func CmpContains(t TestingT, got interface{}, expectedValue interface{}, args ...interface{}) bool

CmpContains is a shortcut for:

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

Examples

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

T.Contains shortcut

func (t *T) Contains(got interface{}, expectedValue interface{}, args ...interface{}) bool

Contains is a shortcut for:

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