mirror of
https://github.com/neilotoole/sq.git
synced 2024-12-24 08:36:43 +03:00
c778c8ced5
* test linting * test linting * test linting * test linting
41 lines
909 B
Go
41 lines
909 B
Go
package errz_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestIs(t *testing.T) {
|
|
err := errz.Wrap(sql.ErrNoRows, "wrap")
|
|
|
|
require.Equal(t, "wrap: "+sql.ErrNoRows.Error(), err.Error())
|
|
require.True(t, errors.Is(err, sql.ErrNoRows))
|
|
}
|
|
|
|
func TestAs(t *testing.T) {
|
|
var originalErr error //nolint:gosimple
|
|
originalErr = &CustomError{msg: "huzzah"}
|
|
|
|
err := errz.Wrap(errz.Wrap(originalErr, "wrap"), "wrap")
|
|
require.Equal(t, "wrap: wrap: huzzah", err.Error())
|
|
|
|
var gotCustomErr *CustomError
|
|
require.True(t, errors.As(err, &gotCustomErr))
|
|
require.Equal(t, "huzzah", gotCustomErr.msg)
|
|
|
|
gotUnwrap := errz.Cause(err)
|
|
require.Equal(t, *originalErr.(*CustomError), *gotUnwrap.(*CustomError)) //nolint:errorlint
|
|
}
|
|
|
|
type CustomError struct {
|
|
msg string
|
|
}
|
|
|
|
func (e *CustomError) Error() string {
|
|
return e.msg
|
|
}
|