mediocre-go-lib/merr/merr_test.go
2021-04-09 17:30:59 -06:00

161 lines
3.2 KiB
Go

package merr
import (
"context"
"errors"
"fmt"
"testing"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mtest/massert"
)
func TestError(t *testing.T) {
massert.Require(t, massert.Nil(Wrap(context.Background(), nil)))
ctx := mctx.Annotate(context.Background(),
"a", "aaa aaa\n",
"c", "ccc\nccc\n",
"d\t", "weird key but ok")
{
e := New(ctx, "foo")
exp := `foo
* a: aaa aaa
* c:
ccc
ccc
* d: weird key but ok
* line: merr/merr_test.go:22`
massert.Require(t, massert.Equal(exp, e.Error()))
}
{
e := Wrap(ctx, errors.New("foo"))
exp := `foo
* a: aaa aaa
* c:
ccc
ccc
* d: weird key but ok
* line: merr/merr_test.go:34`
massert.Require(t, massert.Equal(exp, e.Error()))
}
}
func TestAs(t *testing.T) {
ctxA := mctx.Annotate(context.Background(), "a", "1")
ctxB := mctx.Annotate(context.Background(), "b", "2")
errFoo := errors.New("foo")
type test struct {
in error
expAs error
expIs error
}
tests := []test{
{
in: nil,
expAs: nil,
expIs: nil,
},
{
in: errors.New("bar"),
expAs: nil,
},
{
in: Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
},
expAs: Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
},
expIs: errFoo,
},
{
in: fmt.Errorf("bar: %w", Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
}),
expAs: Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
},
expIs: errFoo,
},
{
in: Wrap(ctxB, Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
}),
expAs: Error{
Err: Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
},
Ctx: mctx.MergeAnnotations(ctxA, ctxB),
Stacktrace: Stacktrace{frames: []uintptr{666}},
},
expIs: errFoo,
},
{
in: Wrap(ctxB, fmt.Errorf("%w", Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
})),
expAs: Error{
Err: fmt.Errorf("%w", Error{
Err: errFoo,
Ctx: ctxA,
Stacktrace: Stacktrace{frames: []uintptr{666}},
}),
Ctx: mctx.MergeAnnotations(ctxA, ctxB),
Stacktrace: Stacktrace{frames: []uintptr{666}},
},
expIs: errFoo,
},
}
for i, test := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
var in Error
ok := errors.As(test.in, &in)
massert.Require(t, massert.Comment(
massert.Equal(test.expAs != nil, ok),
"test.in:%#v ok:%v", test.in, ok,
))
if test.expAs == nil {
return
}
expAs := test.expAs.(Error)
inAA := mctx.EvaluateAnnotations(in.Ctx, nil)
expAsAA := mctx.EvaluateAnnotations(expAs.Ctx, nil)
in.Ctx = nil
expAs.Ctx = nil
massert.Require(t,
massert.Equal(expAsAA, inAA),
massert.Equal(expAs, in),
massert.Comment(
massert.Equal(true, errors.Is(test.in, test.expIs)),
"errors.Is(\ntest.in:%#v,\ntest.expIs:%#v,\n)", test.in, test.expIs,
),
)
})
}
}