List operator compares the contents of an array or a slice (or a
pointer on array/slice) with taking care of the order of items.
Array and Slice need to specify the type of array/slice being
compared then to index all expected items. List does not. It acts
as comparing a literal array/slice, but without having to specify
the type and allowing to easily use TestDeep operators:
td.Cmp(t,[]int{1,9,5},td.List(1,9,5))// succeedstd.Cmp(t,[]int{1,9,5},td.List(td.Gt(0),td.Between(8,9),td.Lt(5)))// succeedstd.Cmp(t,[]int{1,9,5},td.List(1,9))// fails, 5 is extratd.Cmp(t,[]int{1,9,5},td.List(1,9,5,4))// fails, 4 is missing// works with slices/arrays of any typetd.Cmp(t,personSlice,td.List(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:
expected:=[]int{1,2,1}td.Cmp(t,[]int{1,1,2},td.List(td.Flatten(expected)))// succeeds// = td.Cmp(t, []int{1, 1, 2}, td.List(1, 2, 1))// Compare only Name field of a slice of Person structstd.Cmp(t,personSlice,td.List(td.Flatten([]string{"Bob","Alice"},"Smuggle:Name")))
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,33,8,2}// Matches as all items are presentok:=td.Cmp(t,got,td.List(1,td.Between(32,34),td.Gt(7),2))fmt.Println("checks all items match, in this order:",ok)// Does not match as got does not use the same order as expectedok=td.Cmp(t,got,td.List(1,td.Gt(7),2,td.Between(32,34)))fmt.Println("checks all items match, in wrong order:",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{1,33,8}ok=td.Cmp(t,got,td.List(td.Flatten(expected),td.Lte(2)))fmt.Println("checks all expected items are present + last one ≤ 2:",ok)// Output:// checks all items match, in this order: true// checks all items match, in wrong order: false// checks all expected items are present + last one ≤ 2: 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,33,8,2}// Matches as all items are presentok:=td.CmpList(t,got,[]any{1,td.Between(32,34),td.Gt(7),2})fmt.Println("checks all items match, in this order:",ok)// Does not match as got does not use the same order as expectedok=td.CmpList(t,got,[]any{1,td.Gt(7),2,td.Between(32,34)})fmt.Println("checks all items match, in wrong order:",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{1,33,8}ok=td.CmpList(t,got,[]any{td.Flatten(expected),td.Lte(2)})fmt.Println("checks all expected items are present + last one ≤ 2:",ok)// Output:// checks all items match, in this order: true// checks all items match, in wrong order: false// checks all expected items are present + last one ≤ 2: 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,33,8,2}// Matches as all items are presentok:=t.List(got,[]any{1,td.Between(32,34),td.Gt(7),2})fmt.Println("checks all items match, in this order:",ok)// Does not match as got does not use the same order as expectedok=t.List(got,[]any{1,td.Gt(7),2,td.Between(32,34)})fmt.Println("checks all items match, in wrong order:",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{1,33,8}ok=t.List(got,[]any{td.Flatten(expected),td.Lte(2)})fmt.Println("checks all expected items are present + last one ≤ 2:",ok)// Output:// checks all items match, in this order: true// checks all items match, in wrong order: false// checks all expected items are present + last one ≤ 2: true