Array
func Array(model any, expectedEntries ArrayEntries) TestDeep
Array operator compares the contents of an array or a pointer on an
array against the values of model and the values of
expectedEntries. Entries with zero values of model are ignored
if the same entry is present in expectedEntries, otherwise they
are taken into account. An entry cannot be present in both model
and expectedEntries, except if it is a zero-value in model. At
the end, all entries are checked. To check only some entries of an
array, see SuperSliceOf
operator.
model must be the same type as compared data.
expectedEntries can be nil
, if no zero entries are expected and
no TestDeep operators are involved.
got := [3]int{12, 14, 17}
td.Cmp(t, got, td.Array([3]int{0, 14}, td.ArrayEntries{0: 12, 2: 17})) // succeeds
td.Cmp(t, &got,
td.Array(&[3]int{0, 14}, td.ArrayEntries{0: td.Gt(10), 2: td.Gt(15)})) // succeeds
TypeBehind
method returns the reflect.Type
of model.
See also Slice
and SuperSliceOf
.
See also Array godoc.
Examples
Array example
t := &testing.T{}
got := [3]int{42, 58, 26}
ok := td.Cmp(t, got,
td.Array([3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}),
"checks array %v", got)
fmt.Println("Simple array:", ok)
ok = td.Cmp(t, &got,
td.Array(&[3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}),
"checks array %v", got)
fmt.Println("Array pointer:", ok)
ok = td.Cmp(t, &got,
td.Array((*[3]int)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}),
"checks array %v", got)
fmt.Println("Array pointer, nil model:", ok)
// Output:
// Simple array: true
// Array pointer: true
// Array pointer, nil model: true
TypedArray example
t := &testing.T{}
type MyArray [3]int
got := MyArray{42, 58, 26}
ok := td.Cmp(t, got,
td.Array(MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}),
"checks typed array %v", got)
fmt.Println("Typed array:", ok)
ok = td.Cmp(t, &got,
td.Array(&MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}),
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array:", ok)
ok = td.Cmp(t, &got,
td.Array(&MyArray{}, td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}),
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array, empty model:", ok)
ok = td.Cmp(t, &got,
td.Array((*MyArray)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}),
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array, nil model:", ok)
// Output:
// Typed array: true
// Pointer on a typed array: true
// Pointer on a typed array, empty model: true
// Pointer on a typed array, nil model: true
CmpArray shortcut
func CmpArray(t TestingT, got, model any, expectedEntries ArrayEntries, args ...any) bool
CmpArray is a shortcut for:
td.Cmp(t, got, td.Array(model, expectedEntries), 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 CmpArray godoc.
Examples
Array example
t := &testing.T{}
got := [3]int{42, 58, 26}
ok := td.CmpArray(t, got, [3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks array %v", got)
fmt.Println("Simple array:", ok)
ok = td.CmpArray(t, &got, &[3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks array %v", got)
fmt.Println("Array pointer:", ok)
ok = td.CmpArray(t, &got, (*[3]int)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()},
"checks array %v", got)
fmt.Println("Array pointer, nil model:", ok)
// Output:
// Simple array: true
// Array pointer: true
// Array pointer, nil model: true
TypedArray example
t := &testing.T{}
type MyArray [3]int
got := MyArray{42, 58, 26}
ok := td.CmpArray(t, got, MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks typed array %v", got)
fmt.Println("Typed array:", ok)
ok = td.CmpArray(t, &got, &MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array:", ok)
ok = td.CmpArray(t, &got, &MyArray{}, td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()},
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array, empty model:", ok)
ok = td.CmpArray(t, &got, (*MyArray)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()},
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array, nil model:", ok)
// Output:
// Typed array: true
// Pointer on a typed array: true
// Pointer on a typed array, empty model: true
// Pointer on a typed array, nil model: true
T.Array shortcut
func (t *T) Array(got, model any, expectedEntries ArrayEntries, args ...any) bool
Array is a shortcut for:
t.Cmp(got, td.Array(model, expectedEntries), 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.Array godoc.
Examples
Array example
t := td.NewT(&testing.T{})
got := [3]int{42, 58, 26}
ok := t.Array(got, [3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks array %v", got)
fmt.Println("Simple array:", ok)
ok = t.Array(&got, &[3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks array %v", got)
fmt.Println("Array pointer:", ok)
ok = t.Array(&got, (*[3]int)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()},
"checks array %v", got)
fmt.Println("Array pointer, nil model:", ok)
// Output:
// Simple array: true
// Array pointer: true
// Array pointer, nil model: true
TypedArray example
t := td.NewT(&testing.T{})
type MyArray [3]int
got := MyArray{42, 58, 26}
ok := t.Array(got, MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks typed array %v", got)
fmt.Println("Typed array:", ok)
ok = t.Array(&got, &MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()},
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array:", ok)
ok = t.Array(&got, &MyArray{}, td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()},
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array, empty model:", ok)
ok = t.Array(&got, (*MyArray)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()},
"checks pointer on typed array %v", got)
fmt.Println("Pointer on a typed array, nil model:", ok)
// Output:
// Typed array: true
// Pointer on a typed array: true
// Pointer on a typed array, empty model: true
// Pointer on a typed array, nil model: true