ErrorIs

func ErrorIs(expected error) TestDeep

ErrorIs operator reports whether any error in an error’s chain matches expected.

_, err := os.Open("/unknown/file")
td.Cmp(t, err, os.ErrNotExist)             // fails
td.Cmp(t, err, td.ErrorIs(os.ErrNotExist)) // succeeds

err1 := fmt.Errorf("failure1")
err2 := fmt.Errorf("failure2: %w", err1)
err3 := fmt.Errorf("failure3: %w", err2)
err := fmt.Errorf("failure4: %w", err3)
td.Cmp(t, err, td.ErrorIs(err))  // succeeds
td.Cmp(t, err, td.ErrorIs(err1)) // succeeds
td.Cmp(t, err1, td.ErrorIs(err)) // fails

Behind the scene it uses errors.Is function.

Note that like errors.Is, expected can be nil: in this case the comparison succeeds when got is nil too.

See also CmpError and CmpNoError.

See also ErrorIs godoc.

Example

Base example

CmpErrorIs shortcut

func CmpErrorIs(t TestingT, got any, expected error, args ...any) bool

CmpErrorIs is a shortcut for:

td.Cmp(t, got, td.ErrorIs(expected), 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 CmpErrorIs godoc.

Example

Base example

T.CmpErrorIs shortcut

func (t *T) CmpErrorIs(got any, expected error, args ...any) bool

CmpErrorIs is a shortcut for:

t.Cmp(got, td.ErrorIs(expected), 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.CmpErrorIs godoc.

Example

Base example