All
func All(expectedValues ...any) TestDeep
All operator compares data against several expected values. During
a match, all of them have to match to succeed. Consider it
as a “AND” logical operator.
td.Cmp(t, "foobar", td.All(
td.Len(6),
td.HasPrefix("fo"),
td.HasSuffix("ar"),
)) // succeeds
Note Flatten function can be used to group or reuse some values or
operators and so avoid boring and inefficient copies:
stringOps := td.Flatten([]td.TestDeep{td.HasPrefix("fo"), td.HasSuffix("ar")})
td.Cmp(t, "foobar", td.All(
td.Len(6),
stringOps,
)) // succeeds
One can do the same with All operator itself:
stringOps := td.All(td.HasPrefix("fo"), td.HasSuffix("ar"))
td.Cmp(t, "foobar", td.All(
td.Len(6),
stringOps,
)) // succeeds
but if an error occurs in the nested All, the report is a bit more
complex to read due to the nested level. Flatten does not create
a new level, its slice is just flattened in the All parameters.
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 Any and None.
See also All godoc.
Example
Base example
t := &testing.T{}
got := "foo/bar"
// Checks got string against:
// "o/b" regexp *AND* "bar" suffix *AND* exact "foo/bar" string
ok := td.Cmp(t,
got,
td.All(td.Re("o/b"), td.HasSuffix("bar"), "foo/bar"),
"checks value %s", got)
fmt.Println(ok)
// Checks got string against:
// "o/b" regexp *AND* "bar" suffix *AND* exact "fooX/Ybar" string
ok = td.Cmp(t,
got,
td.All(td.Re("o/b"), td.HasSuffix("bar"), "fooX/Ybar"),
"checks value %s", got)
fmt.Println(ok)
// When some operators or values have to be reused and mixed between
// several calls, Flatten can be used to avoid boring and
// inefficient []any copies:
regOps := td.Flatten([]td.TestDeep{td.Re("o/b"), td.Re(`^fo`), td.Re(`ar$`)})
ok = td.Cmp(t,
got,
td.All(td.HasPrefix("foo"), regOps, td.HasSuffix("bar")),
"checks all operators against value %s", got)
fmt.Println(ok)
// Output:
// true
// false
// true
CmpAll shortcut
func CmpAll(t TestingT, got any, expectedValues []any, args ...any) bool
CmpAll is a shortcut for:
td.Cmp(t, got, td.All(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 CmpAll godoc.
Example
Base example
t := &testing.T{}
got := "foo/bar"
// Checks got string against:
// "o/b" regexp *AND* "bar" suffix *AND* exact "foo/bar" string
ok := td.CmpAll(t, got, []any{td.Re("o/b"), td.HasSuffix("bar"), "foo/bar"},
"checks value %s", got)
fmt.Println(ok)
// Checks got string against:
// "o/b" regexp *AND* "bar" suffix *AND* exact "fooX/Ybar" string
ok = td.CmpAll(t, got, []any{td.Re("o/b"), td.HasSuffix("bar"), "fooX/Ybar"},
"checks value %s", got)
fmt.Println(ok)
// When some operators or values have to be reused and mixed between
// several calls, Flatten can be used to avoid boring and
// inefficient []any copies:
regOps := td.Flatten([]td.TestDeep{td.Re("o/b"), td.Re(`^fo`), td.Re(`ar$`)})
ok = td.CmpAll(t, got, []any{td.HasPrefix("foo"), regOps, td.HasSuffix("bar")},
"checks all operators against value %s", got)
fmt.Println(ok)
// Output:
// true
// false
// true
T.All shortcut
func (t *T) All(got any, expectedValues []any, args ...any) bool
All is a shortcut for:
t.Cmp(got, td.All(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.All godoc.
Example
Base example
t := td.NewT(&testing.T{})
got := "foo/bar"
// Checks got string against:
// "o/b" regexp *AND* "bar" suffix *AND* exact "foo/bar" string
ok := t.All(got, []any{td.Re("o/b"), td.HasSuffix("bar"), "foo/bar"},
"checks value %s", got)
fmt.Println(ok)
// Checks got string against:
// "o/b" regexp *AND* "bar" suffix *AND* exact "fooX/Ybar" string
ok = t.All(got, []any{td.Re("o/b"), td.HasSuffix("bar"), "fooX/Ybar"},
"checks value %s", got)
fmt.Println(ok)
// When some operators or values have to be reused and mixed between
// several calls, Flatten can be used to avoid boring and
// inefficient []any copies:
regOps := td.Flatten([]td.TestDeep{td.Re("o/b"), td.Re(`^fo`), td.Re(`ar$`)})
ok = t.All(got, []any{td.HasPrefix("foo"), regOps, td.HasSuffix("bar")},
"checks all operators against value %s", got)
fmt.Println(ok)
// Output:
// true
// false
// true