feat: adding ErrorsAs

This commit is contained in:
Samuel Berthe
2022-10-02 21:51:02 +02:00
parent 31f3bc3a85
commit edda23923b
4 changed files with 62 additions and 0 deletions

View File

@@ -361,3 +361,27 @@ func TestTryCatchWithErrorValue(t *testing.T) {
})
is.False(caught)
}
type internalError struct {
foobar string
}
func (e *internalError) Error() string {
return fmt.Sprintf("internal error")
}
func TestErrorsAs(t *testing.T) {
is := assert.New(t)
err, ok := ErrorsAs[*internalError](fmt.Errorf("hello world"))
is.False(ok)
is.Nil(nil, err)
err, ok = ErrorsAs[*internalError](&internalError{foobar: "foobar"})
is.True(ok)
is.Equal(&internalError{foobar: "foobar"}, err)
err, ok = ErrorsAs[*internalError](nil)
is.False(ok)
is.Nil(nil, err)
}