SuperBagOf operator compares the contents of an array or a slice (or a
pointer on array/slice) without taking care of the order of items.
During a match, each expected item should match in the compared
array/slice. But some items in the compared array/slice may not be
expected.
td.Cmp(t,[]int{1,1,2},td.SuperBagOf(1))// succeedstd.Cmp(t,[]int{1,1,2},td.SuperBagOf(1,1,1))// fails, one 1 is missing// works with slices/arrays of any typetd.Cmp(t,personSlice,td.SuperBagOf(Person{Name:"Bob",Age:32},Person{Name:"Alice",Age:26},))
To flatten a non-[]any slice/array, use Flatten function
and so avoid boring and inefficient copies:
TypeBehind method can return a non-nilreflect.Type if all items
known non-interface types are equal, or if only interface types
are found (mostly issued from Isa()) and they are equal.
t:=&testing.T{}got:=[]int{1,3,5,8,8,1,2}ok:=td.Cmp(t,got,td.SuperBagOf(8,5,8),"checks the items are present, in any order")fmt.Println(ok)ok=td.Cmp(t,got,td.SuperBagOf(td.Gt(5),td.Lte(2)),"checks at least 2 items of %v match",got)fmt.Println(ok)// When expected is already a non-[]any slice, it cannot be// flattened directly using expected... without copying it to a new// []any slice, then use td.Flatten!expected:=[]int{8,5,8}ok=td.Cmp(t,got,td.SuperBagOf(td.Flatten(expected)),"checks the expected items are present, in any order")fmt.Println(ok)// Output:// true// true// 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{}got:=[]int{1,3,5,8,8,1,2}ok:=td.CmpSuperBagOf(t,got,[]any{8,5,8},"checks the items are present, in any order")fmt.Println(ok)ok=td.CmpSuperBagOf(t,got,[]any{td.Gt(5),td.Lte(2)},"checks at least 2 items of %v match",got)fmt.Println(ok)// When expected is already a non-[]any slice, it cannot be// flattened directly using expected... without copying it to a new// []any slice, then use td.Flatten!expected:=[]int{8,5,8}ok=td.CmpSuperBagOf(t,got,[]any{td.Flatten(expected)},"checks the expected items are present, in any order")fmt.Println(ok)// Output:// true// true// 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{})got:=[]int{1,3,5,8,8,1,2}ok:=t.SuperBagOf(got,[]any{8,5,8},"checks the items are present, in any order")fmt.Println(ok)ok=t.SuperBagOf(got,[]any{td.Gt(5),td.Lte(2)},"checks at least 2 items of %v match",got)fmt.Println(ok)// When expected is already a non-[]any slice, it cannot be// flattened directly using expected... without copying it to a new// []any slice, then use td.Flatten!expected:=[]int{8,5,8}ok=t.SuperBagOf(got,[]any{td.Flatten(expected)},"checks the expected items are present, in any order")fmt.Println(ok)// Output:// true// true// true