minireader/internal/pkg/errx/details_test.go
2024-08-10 11:01:50 +07:00

102 lines
1.7 KiB
Go

package errx
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)
type DetailsSuite struct {
suite.Suite
}
func (suite *DetailsSuite) TestUnwrap() {
err := errors.New("test")
tests := []struct {
err error
want error
}{
{
err: &detailedError{},
want: nil,
},
{
err: WithDetails(err),
want: err,
},
}
for _, test := range tests {
suite.Run("", func() {
assert.Equal(suite.T(), test.want, errors.Unwrap(test.err))
})
}
}
func (suite *DetailsSuite) TestIs() {
errSame := errors.New("test same")
errWrong := errors.New("test wrong")
tests := []struct {
err error
want bool
}{
{
err: WithDetails(errSame),
want: true,
},
{
err: WithDetails(errWrong),
want: false,
},
}
for _, test := range tests {
suite.Run("", func() {
assert.Equal(suite.T(), test.want, errors.Is(test.err, errSame))
})
}
}
func (suite *DetailsSuite) TestDetails() {
err := errors.New("test")
tests := []struct {
err error
want []any
}{
{
err: WithDetails(err),
want: nil,
},
{
err: WithDetails(err, 1, 2, 3),
want: []any{1, 2, 3},
},
{
err: WithDetails(WithDetails(err, 4), 5, 6),
want: []any{4, 5, 6},
},
}
for _, test := range tests {
suite.Run("", func() {
assert.Equal(suite.T(), test.want, Details(test.err))
})
}
}
func (suite *DetailsSuite) TestLookupDetails() {
err := WithDetails(errors.New("test"), true, 2, "3")
assert.Equal(suite.T(), true, LookupDetails[bool](err))
assert.Equal(suite.T(), 2, LookupDetails[int](err))
assert.Equal(suite.T(), "3", LookupDetails[string](err))
}
func TestDetails(t *testing.T) {
suite.Run(t, new(DetailsSuite))
}