Isa
func Isa(model any) TestDeep
Isa operator checks the data type or whether data implements an
interface or not.
Typical type checks:
td.Cmp(t, time.Now(), td.Isa(time.Time{})) // succeeds
td.Cmp(t, time.Now(), td.Isa(&time.Time{})) // fails, as not a *time.Time
td.Cmp(t, got, td.Isa(map[string]time.Time{}))
For interfaces, it is a bit more complicated, as:
is not an interface, but just nil
… To bypass this golang
limitation, Isa accepts pointers on interfaces. So checking that
data implements fmt.Stringer
interface should be written as:
td.Cmp(t, bytes.Buffer{}, td.Isa((*fmt.Stringer)(nil))) // succeeds
Of course, in the latter case, if checked data type is
*fmt.Stringer
, Isa will match too (in fact before checking whether
it implements fmt.Stringer
or not).
TypeBehind
method returns the reflect.Type
of model.
See also Isa godoc.
Examples
Base example
t := &testing.T{}
type TstStruct struct {
Field int
}
got := TstStruct{Field: 1}
ok := td.Cmp(t, got, td.Isa(TstStruct{}), "checks got is a TstStruct")
fmt.Println(ok)
ok = td.Cmp(t, got, td.Isa(&TstStruct{}),
"checks got is a pointer on a TstStruct")
fmt.Println(ok)
ok = td.Cmp(t, &got, td.Isa(&TstStruct{}),
"checks &got is a pointer on a TstStruct")
fmt.Println(ok)
// Output:
// true
// false
// true
Interface example
t := &testing.T{}
got := bytes.NewBufferString("foobar")
ok := td.Cmp(t, got, td.Isa((*fmt.Stringer)(nil)),
"checks got implements fmt.Stringer interface")
fmt.Println(ok)
errGot := fmt.Errorf("An error #%d occurred", 123)
ok = td.Cmp(t, errGot, td.Isa((*error)(nil)),
"checks errGot is a *error or implements error interface")
fmt.Println(ok)
// As nil, is passed below, it is not an interface but nil… So it
// does not match
errGot = nil
ok = td.Cmp(t, errGot, td.Isa((*error)(nil)),
"checks errGot is a *error or implements error interface")
fmt.Println(ok)
// BUT if its address is passed, now it is OK as the types match
ok = td.Cmp(t, &errGot, td.Isa((*error)(nil)),
"checks &errGot is a *error or implements error interface")
fmt.Println(ok)
// Output:
// true
// true
// false
// true
CmpIsa shortcut
func CmpIsa(t TestingT, got, model any, args ...any) bool
CmpIsa is a shortcut for:
td.Cmp(t, got, td.Isa(model), 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 CmpIsa godoc.
Examples
Base example
t := &testing.T{}
type TstStruct struct {
Field int
}
got := TstStruct{Field: 1}
ok := td.CmpIsa(t, got, TstStruct{}, "checks got is a TstStruct")
fmt.Println(ok)
ok = td.CmpIsa(t, got, &TstStruct{},
"checks got is a pointer on a TstStruct")
fmt.Println(ok)
ok = td.CmpIsa(t, &got, &TstStruct{},
"checks &got is a pointer on a TstStruct")
fmt.Println(ok)
// Output:
// true
// false
// true
Interface example
t := &testing.T{}
got := bytes.NewBufferString("foobar")
ok := td.CmpIsa(t, got, (*fmt.Stringer)(nil),
"checks got implements fmt.Stringer interface")
fmt.Println(ok)
errGot := fmt.Errorf("An error #%d occurred", 123)
ok = td.CmpIsa(t, errGot, (*error)(nil),
"checks errGot is a *error or implements error interface")
fmt.Println(ok)
// As nil, is passed below, it is not an interface but nil… So it
// does not match
errGot = nil
ok = td.CmpIsa(t, errGot, (*error)(nil),
"checks errGot is a *error or implements error interface")
fmt.Println(ok)
// BUT if its address is passed, now it is OK as the types match
ok = td.CmpIsa(t, &errGot, (*error)(nil),
"checks &errGot is a *error or implements error interface")
fmt.Println(ok)
// Output:
// true
// true
// false
// true
T.Isa shortcut
func (t *T) Isa(got, model any, args ...any) bool
Isa is a shortcut for:
t.Cmp(got, td.Isa(model), 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.Isa godoc.
Examples
Base example
t := td.NewT(&testing.T{})
type TstStruct struct {
Field int
}
got := TstStruct{Field: 1}
ok := t.Isa(got, TstStruct{}, "checks got is a TstStruct")
fmt.Println(ok)
ok = t.Isa(got, &TstStruct{},
"checks got is a pointer on a TstStruct")
fmt.Println(ok)
ok = t.Isa(&got, &TstStruct{},
"checks &got is a pointer on a TstStruct")
fmt.Println(ok)
// Output:
// true
// false
// true
Interface example
t := td.NewT(&testing.T{})
got := bytes.NewBufferString("foobar")
ok := t.Isa(got, (*fmt.Stringer)(nil),
"checks got implements fmt.Stringer interface")
fmt.Println(ok)
errGot := fmt.Errorf("An error #%d occurred", 123)
ok = t.Isa(errGot, (*error)(nil),
"checks errGot is a *error or implements error interface")
fmt.Println(ok)
// As nil, is passed below, it is not an interface but nil… So it
// does not match
errGot = nil
ok = t.Isa(errGot, (*error)(nil),
"checks errGot is a *error or implements error interface")
fmt.Println(ok)
// BUT if its address is passed, now it is OK as the types match
ok = t.Isa(&errGot, (*error)(nil),
"checks &errGot is a *error or implements error interface")
fmt.Println(ok)
// Output:
// true
// true
// false
// true