ErrorIs

func ErrorIs(expectedError any) TestDeep

ErrorIs is a smuggler operator. It reports whether any error in an error’s chain matches expectedError.

_, 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

var cerr myError
td.Cmp(t, err, td.ErrorIs(td.Catch(&cerr, td.String("my error..."))))

td.Cmp(t, err, td.ErrorIs(td.All(
  td.Isa(myError{}),
  td.String("my error..."),
)))

Behind the scene it uses errors.Is function if expectedError is an error and errors.As function if expectedError is a TestDeep operator.

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

See also CmpError and CmpNoError.

See also ErrorIs godoc.

Example

Base example

CmpErrorIs shortcut

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

CmpErrorIs is a shortcut for:

td.Cmp(t, got, td.ErrorIs(expectedError), 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, expectedError any, args ...any) bool

CmpErrorIs is a shortcut for:

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