blob: 7da37883fb07221af42e88f1a672cd7b5cd983c5 [file] [log] [blame]
// +build go1.13
package errors
import (
stderrors "errors"
"fmt"
"reflect"
"testing"
)
func TestCauseErrorChainCompat(t *testing.T) {
err := stderrors.New("the cause!")
// Wrap error using the standard library
wrapped := fmt.Errorf("wrapped with stdlib: %w", err)
if Cause(wrapped) != err {
t.Errorf("Cause does not support Go 1.13 error chains")
}
// Wrap in another layer using pkg/errors
wrapped = WithMessage(wrapped, "wrapped with pkg/errors")
if Cause(wrapped) != err {
t.Errorf("Cause does not support Go 1.13 error chains")
}
// Wrap in another layer using the standard library
wrapped = fmt.Errorf("wrapped with stdlib: %w", wrapped)
if Cause(wrapped) != err {
t.Errorf("Cause does not support Go 1.13 error chains")
}
}
func TestWrapErrorChainCompat(t *testing.T) {
err := stderrors.New("error that gets wrapped")
wrapped := Wrap(err, "wrapped up")
if !stderrors.Is(wrapped, err) {
t.Errorf("Wrap does not support Go 1.13 error chains")
}
}
func TestIs(t *testing.T) {
err := New("test")
type args struct {
err error
target error
}
tests := []struct {
name string
args args
want bool
}{
{
name: "with stack",
args: args{
err: WithStack(err),
target: err,
},
want: true,
},
{
name: "with message",
args: args{
err: WithMessage(err, "test"),
target: err,
},
want: true,
},
{
name: "with message format",
args: args{
err: WithMessagef(err, "%s", "test"),
target: err,
},
want: true,
},
{
name: "std errors compatibility",
args: args{
err: fmt.Errorf("wrap it: %w", err),
target: err,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Is(tt.args.err, tt.args.target); got != tt.want {
t.Errorf("Is() = %v, want %v", got, tt.want)
}
})
}
}
type customErr struct {
msg string
}
func (c customErr) Error() string { return c.msg }
func TestAs(t *testing.T) {
var err = customErr{msg: "test message"}
type args struct {
err error
target interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "with stack",
args: args{
err: WithStack(err),
target: new(customErr),
},
want: true,
},
{
name: "with message",
args: args{
err: WithMessage(err, "test"),
target: new(customErr),
},
want: true,
},
{
name: "with message format",
args: args{
err: WithMessagef(err, "%s", "test"),
target: new(customErr),
},
want: true,
},
{
name: "std errors compatibility",
args: args{
err: fmt.Errorf("wrap it: %w", err),
target: new(customErr),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := As(tt.args.err, tt.args.target); got != tt.want {
t.Errorf("As() = %v, want %v", got, tt.want)
}
ce := tt.args.target.(*customErr)
if !reflect.DeepEqual(err, *ce) {
t.Errorf("set target error failed, target error is %v", *ce)
}
})
}
}
func TestUnwrap(t *testing.T) {
err := New("test")
type args struct {
err error
}
tests := []struct {
name string
args args
want error
}{
{
name: "with stack",
args: args{err: WithStack(err)},
want: err,
},
{
name: "with message",
args: args{err: WithMessage(err, "test")},
want: err,
},
{
name: "with message format",
args: args{err: WithMessagef(err, "%s", "test")},
want: err,
},
{
name: "std errors compatibility",
args: args{err: fmt.Errorf("wrap: %w", err)},
want: err,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Unwrap(tt.args.err); !reflect.DeepEqual(err, tt.want) {
t.Errorf("Unwrap() error = %v, want %v", err, tt.want)
}
})
}
}