List
func List(expectedValues ...any) TestDeep
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)) // succeeds
td.Cmp(t, []int{1, 9, 5}, td.List(td.Gt(0), td.Between(8, 9), td.Lt(5))) // succeeds
td.Cmp(t, []int{1, 9, 5}, td.List(1, 9)) // fails, 5 is extra
td.Cmp(t, []int{1, 9, 5}, td.List(1, 9, 5, 4)) // fails, 4 is missing
// works with slices/arrays of any type
td.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 structs
td.Cmp(t, personSlice, td.List(td.Flatten([]string{"Bob", "Alice"}, "Smuggle:Name")))
TypeBehind
method can return a non-nil
reflect.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.
See also List godoc.
Example
CmpList shortcut
func CmpList(t TestingT, got any, expectedValues []any, args ...any) bool
CmpList is a shortcut for:
td.Cmp(t, got, td.List(expectedValues...), 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 CmpList godoc.
Example
T.List shortcut
func (t *T) List(got any, expectedValues []any, args ...any) bool
List is a shortcut for:
t.Cmp(got, td.List(expectedValues...), 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.List godoc.