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:

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:

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
Interface example

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
Interface example

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
Interface example