123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- // +build go1.13
- package errors
- import (
- stderrors "errors"
- "fmt"
- "reflect"
- "testing"
- )
- func TestErrorChainCompat(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)
- }
- })
- }
- }
|