ArrayEach
func ArrayEach(expectedValue any) TestDeep
ArrayEach operator has to be applied on arrays or slices or on
pointers on array/slice. It compares each item of data array/slice
against expectedValue. During a match, all items have to match to
succeed.
got := [3]string{"foo", "bar", "biz"}
td.Cmp(t, got, td.ArrayEach(td.Len(3))) // succeeds
td.Cmp(t, got, td.ArrayEach(td.HasPrefix("b"))) // fails coz "foo"
Works on slices as well:
got := []Person{
{Name: "Bob", Age: 42},
{Name: "Alice", Age: 24},
}
td.Cmp(t, got, td.ArrayEach(
td.Struct(Person{}, td.StructFields{
Age: td.Between(20, 45),
})),
) // succeeds, each Person has Age field between 20 and 45
See also ArrayEach godoc.
Examples
Array example
t := &testing.T{}
got := [3]int{42, 58, 26}
ok := td.Cmp(t, got, td.ArrayEach(td.Between(25, 60)),
"checks each item of array %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
TypedArray example
t := &testing.T{}
type MyArray [3]int
got := MyArray{42, 58, 26}
ok := td.Cmp(t, got, td.ArrayEach(td.Between(25, 60)),
"checks each item of typed array %v is in [25 .. 60]", got)
fmt.Println(ok)
ok = td.Cmp(t, &got, td.ArrayEach(td.Between(25, 60)),
"checks each item of typed array pointer %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
// true
Slice example
t := &testing.T{}
got := []int{42, 58, 26}
ok := td.Cmp(t, got, td.ArrayEach(td.Between(25, 60)),
"checks each item of slice %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
TypedSlice example
t := &testing.T{}
type MySlice []int
got := MySlice{42, 58, 26}
ok := td.Cmp(t, got, td.ArrayEach(td.Between(25, 60)),
"checks each item of typed slice %v is in [25 .. 60]", got)
fmt.Println(ok)
ok = td.Cmp(t, &got, td.ArrayEach(td.Between(25, 60)),
"checks each item of typed slice pointer %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
// true
CmpArrayEach shortcut
func CmpArrayEach(t TestingT, got, expectedValue any, args ...any) bool
CmpArrayEach is a shortcut for:
td.Cmp(t, got, td.ArrayEach(expectedValue), 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 CmpArrayEach godoc.
Examples
Array example
t := &testing.T{}
got := [3]int{42, 58, 26}
ok := td.CmpArrayEach(t, got, td.Between(25, 60),
"checks each item of array %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
TypedArray example
t := &testing.T{}
type MyArray [3]int
got := MyArray{42, 58, 26}
ok := td.CmpArrayEach(t, got, td.Between(25, 60),
"checks each item of typed array %v is in [25 .. 60]", got)
fmt.Println(ok)
ok = td.CmpArrayEach(t, &got, td.Between(25, 60),
"checks each item of typed array pointer %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
// true
Slice example
t := &testing.T{}
got := []int{42, 58, 26}
ok := td.CmpArrayEach(t, got, td.Between(25, 60),
"checks each item of slice %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
TypedSlice example
t := &testing.T{}
type MySlice []int
got := MySlice{42, 58, 26}
ok := td.CmpArrayEach(t, got, td.Between(25, 60),
"checks each item of typed slice %v is in [25 .. 60]", got)
fmt.Println(ok)
ok = td.CmpArrayEach(t, &got, td.Between(25, 60),
"checks each item of typed slice pointer %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
// true
T.ArrayEach shortcut
func (t *T) ArrayEach(got, expectedValue any, args ...any) bool
ArrayEach is a shortcut for:
t.Cmp(got, td.ArrayEach(expectedValue), 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.ArrayEach godoc.
Examples
Array example
t := td.NewT(&testing.T{})
got := [3]int{42, 58, 26}
ok := t.ArrayEach(got, td.Between(25, 60),
"checks each item of array %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
TypedArray example
t := td.NewT(&testing.T{})
type MyArray [3]int
got := MyArray{42, 58, 26}
ok := t.ArrayEach(got, td.Between(25, 60),
"checks each item of typed array %v is in [25 .. 60]", got)
fmt.Println(ok)
ok = t.ArrayEach(&got, td.Between(25, 60),
"checks each item of typed array pointer %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
// true
Slice example
t := td.NewT(&testing.T{})
got := []int{42, 58, 26}
ok := t.ArrayEach(got, td.Between(25, 60),
"checks each item of slice %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
TypedSlice example
t := td.NewT(&testing.T{})
type MySlice []int
got := MySlice{42, 58, 26}
ok := t.ArrayEach(got, td.Between(25, 60),
"checks each item of typed slice %v is in [25 .. 60]", got)
fmt.Println(ok)
ok = t.ArrayEach(&got, td.Between(25, 60),
"checks each item of typed slice pointer %v is in [25 .. 60]", got)
fmt.Println(ok)
// Output:
// true
// true