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{}))// succeedstd.Cmp(t,time.Now(),td.Isa(&time.Time{}))// fails, as not a *time.Timetd.Cmp(t,got,td.Isa(map[string]time.Time{}))
For interfaces, it is a bit more complicated, as:
fmt.Stringer(nil)
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:
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).
t:=&testing.T{}typeTstStructstruct{Fieldint}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 matcherrGot=nilok=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 matchok=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
funcCmpIsa(tTestingT,got,modelany,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.
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.
t:=&testing.T{}typeTstStructstruct{Fieldint}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 matcherrGot=nilok=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 matchok=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,modelany,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.
t:=td.NewT(&testing.T{})typeTstStructstruct{Fieldint}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 matcherrGot=nilok=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 matchok=t.Isa(&errGot,(*error)(nil),"checks &errGot is a *error or implements error interface")fmt.Println(ok)// Output:// true// true// false// true