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.
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.
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):
t:=&testing.T{}ok:=td.Cmp(t,[...]int{11,22,33,44},td.Contains(22))fmt.Println("array contains 22:",ok)ok=td.Cmp(t,[...]int{11,22,33,44},td.Contains(td.Between(20,25)))fmt.Println("array contains at least one item in [20 .. 25]:",ok)ok=td.Cmp(t,[]int{11,22,33,44},td.Contains(22))fmt.Println("slice contains 22:",ok)ok=td.Cmp(t,[]int{11,22,33,44},td.Contains(td.Between(20,25)))fmt.Println("slice contains at least one item in [20 .. 25]:",ok)ok=td.Cmp(t,[]int{11,22,33,44},td.Contains([]int{22,33}))fmt.Println("slice contains the sub-slice [22, 33]:",ok)// Output:// array contains 22: true// array contains at least one item in [20 .. 25]: true// slice contains 22: true// slice contains at least one item in [20 .. 25]: true// slice contains the sub-slice [22, 33]: true
t:=&testing.T{}ok:=td.Cmp(t,map[string]int{"foo":11,"bar":22,"zip":33},td.Contains(22))fmt.Println("map contains value 22:",ok)ok=td.Cmp(t,map[string]int{"foo":11,"bar":22,"zip":33},td.Contains(td.Between(20,25)))fmt.Println("map contains at least one value in [20 .. 25]:",ok)// Output:// map contains value 22: true// map contains at least one value in [20 .. 25]: true
String example
t:=&testing.T{}got:="foobar"ok:=td.Cmp(t,got,td.Contains("oob"),"checks %s",got)fmt.Println("contains `oob` string:",ok)ok=td.Cmp(t,got,td.Contains([]byte("oob")),"checks %s",got)fmt.Println("contains `oob` []byte:",ok)ok=td.Cmp(t,got,td.Contains('b'),"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=td.Cmp(t,got,td.Contains(byte('a')),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=td.Cmp(t,got,td.Contains(td.Between('n','p')),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains `oob` []byte: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
Stringer example
t:=&testing.T{}// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foobar")ok:=td.Cmp(t,got,td.Contains("oob"),"checks %s",got)fmt.Println("contains `oob` string:",ok)ok=td.Cmp(t,got,td.Contains('b'),"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=td.Cmp(t,got,td.Contains(byte('a')),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=td.Cmp(t,got,td.Contains(td.Between('n','p')),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
Error example
t:=&testing.T{}got:=errors.New("foobar")ok:=td.Cmp(t,got,td.Contains("oob"),"checks %s",got)fmt.Println("contains `oob` string:",ok)ok=td.Cmp(t,got,td.Contains('b'),"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=td.Cmp(t,got,td.Contains(byte('a')),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=td.Cmp(t,got,td.Contains(td.Between('n','p')),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
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.
t:=&testing.T{}ok:=td.CmpContains(t,[...]int{11,22,33,44},22)fmt.Println("array contains 22:",ok)ok=td.CmpContains(t,[...]int{11,22,33,44},td.Between(20,25))fmt.Println("array contains at least one item in [20 .. 25]:",ok)ok=td.CmpContains(t,[]int{11,22,33,44},22)fmt.Println("slice contains 22:",ok)ok=td.CmpContains(t,[]int{11,22,33,44},td.Between(20,25))fmt.Println("slice contains at least one item in [20 .. 25]:",ok)ok=td.CmpContains(t,[]int{11,22,33,44},[]int{22,33})fmt.Println("slice contains the sub-slice [22, 33]:",ok)// Output:// array contains 22: true// array contains at least one item in [20 .. 25]: true// slice contains 22: true// slice contains at least one item in [20 .. 25]: true// slice contains the sub-slice [22, 33]: true
t:=&testing.T{}ok:=td.CmpContains(t,map[string]int{"foo":11,"bar":22,"zip":33},22)fmt.Println("map contains value 22:",ok)ok=td.CmpContains(t,map[string]int{"foo":11,"bar":22,"zip":33},td.Between(20,25))fmt.Println("map contains at least one value in [20 .. 25]:",ok)// Output:// map contains value 22: true// map contains at least one value in [20 .. 25]: true
String example
t:=&testing.T{}got:="foobar"ok:=td.CmpContains(t,got,"oob","checks %s",got)fmt.Println("contains `oob` string:",ok)ok=td.CmpContains(t,got,[]byte("oob"),"checks %s",got)fmt.Println("contains `oob` []byte:",ok)ok=td.CmpContains(t,got,'b',"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=td.CmpContains(t,got,byte('a'),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=td.CmpContains(t,got,td.Between('n','p'),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains `oob` []byte: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
Stringer example
t:=&testing.T{}// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foobar")ok:=td.CmpContains(t,got,"oob","checks %s",got)fmt.Println("contains `oob` string:",ok)ok=td.CmpContains(t,got,'b',"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=td.CmpContains(t,got,byte('a'),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=td.CmpContains(t,got,td.Between('n','p'),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
Error example
t:=&testing.T{}got:=errors.New("foobar")ok:=td.CmpContains(t,got,"oob","checks %s",got)fmt.Println("contains `oob` string:",ok)ok=td.CmpContains(t,got,'b',"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=td.CmpContains(t,got,byte('a'),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=td.CmpContains(t,got,td.Between('n','p'),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
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.
t:=td.NewT(&testing.T{})ok:=t.Contains([...]int{11,22,33,44},22)fmt.Println("array contains 22:",ok)ok=t.Contains([...]int{11,22,33,44},td.Between(20,25))fmt.Println("array contains at least one item in [20 .. 25]:",ok)ok=t.Contains([]int{11,22,33,44},22)fmt.Println("slice contains 22:",ok)ok=t.Contains([]int{11,22,33,44},td.Between(20,25))fmt.Println("slice contains at least one item in [20 .. 25]:",ok)ok=t.Contains([]int{11,22,33,44},[]int{22,33})fmt.Println("slice contains the sub-slice [22, 33]:",ok)// Output:// array contains 22: true// array contains at least one item in [20 .. 25]: true// slice contains 22: true// slice contains at least one item in [20 .. 25]: true// slice contains the sub-slice [22, 33]: true
t:=td.NewT(&testing.T{})ok:=t.Contains(map[string]int{"foo":11,"bar":22,"zip":33},22)fmt.Println("map contains value 22:",ok)ok=t.Contains(map[string]int{"foo":11,"bar":22,"zip":33},td.Between(20,25))fmt.Println("map contains at least one value in [20 .. 25]:",ok)// Output:// map contains value 22: true// map contains at least one value in [20 .. 25]: true
String example
t:=td.NewT(&testing.T{})got:="foobar"ok:=t.Contains(got,"oob","checks %s",got)fmt.Println("contains `oob` string:",ok)ok=t.Contains(got,[]byte("oob"),"checks %s",got)fmt.Println("contains `oob` []byte:",ok)ok=t.Contains(got,'b',"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=t.Contains(got,byte('a'),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=t.Contains(got,td.Between('n','p'),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains `oob` []byte: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
Stringer example
t:=td.NewT(&testing.T{})// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foobar")ok:=t.Contains(got,"oob","checks %s",got)fmt.Println("contains `oob` string:",ok)ok=t.Contains(got,'b',"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=t.Contains(got,byte('a'),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=t.Contains(got,td.Between('n','p'),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true
Error example
t:=td.NewT(&testing.T{})got:=errors.New("foobar")ok:=t.Contains(got,"oob","checks %s",got)fmt.Println("contains `oob` string:",ok)ok=t.Contains(got,'b',"checks %s",got)fmt.Println("contains 'b' rune:",ok)ok=t.Contains(got,byte('a'),"checks %s",got)fmt.Println("contains 'a' byte:",ok)ok=t.Contains(got,td.Between('n','p'),"checks %s",got)fmt.Println("contains at least one character ['n' .. 'p']:",ok)// Output:// contains `oob` string: true// contains 'b' rune: true// contains 'a' byte: true// contains at least one character ['n' .. 'p']: true