2019-01-14 01:08:38 +00:00
|
|
|
package merr
|
|
|
|
|
|
|
|
import (
|
2019-02-09 19:08:30 +00:00
|
|
|
"context"
|
2019-01-14 01:08:38 +00:00
|
|
|
"errors"
|
|
|
|
. "testing"
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
2019-01-14 01:08:38 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestError(t *T) {
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t, massert.Nil(Wrap(nil)))
|
2019-02-27 18:05:51 +00:00
|
|
|
|
|
|
|
ctx := mctx.Annotate(context.Background(),
|
2019-02-09 19:08:30 +00:00
|
|
|
"a", "aaa aaa\n",
|
|
|
|
"c", "ccc\nccc\n",
|
2019-02-27 18:05:51 +00:00
|
|
|
"d\t", "weird key but ok")
|
|
|
|
|
|
|
|
{
|
|
|
|
e := New("foo", ctx)
|
|
|
|
exp := `foo
|
|
|
|
* a: aaa aaa
|
|
|
|
* c:
|
|
|
|
ccc
|
|
|
|
ccc
|
|
|
|
* d: weird key but ok
|
2019-03-10 23:23:37 +00:00
|
|
|
* errLoc: merr/merr_test.go:21`
|
|
|
|
massert.Require(t, massert.Equal(exp, e.Error()))
|
2019-02-27 18:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
e := Wrap(errors.New("foo"), ctx)
|
|
|
|
exp := `foo
|
2019-01-14 01:08:38 +00:00
|
|
|
* a: aaa aaa
|
|
|
|
* c:
|
|
|
|
ccc
|
|
|
|
ccc
|
2019-02-09 19:08:30 +00:00
|
|
|
* d: weird key but ok
|
2019-03-10 23:23:37 +00:00
|
|
|
* errLoc: merr/merr_test.go:33`
|
|
|
|
massert.Require(t, massert.Equal(exp, e.Error()))
|
2019-02-27 18:05:51 +00:00
|
|
|
}
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBase(t *T) {
|
|
|
|
errFoo, errBar := errors.New("foo"), errors.New("bar")
|
2019-02-27 18:05:51 +00:00
|
|
|
erFoo := Wrap(errFoo)
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-01-24 21:45:38 +00:00
|
|
|
massert.Nil(Base(nil)),
|
2019-01-14 01:08:38 +00:00
|
|
|
massert.Equal(errFoo, Base(erFoo)),
|
|
|
|
massert.Equal(errBar, Base(errBar)),
|
|
|
|
massert.Not(massert.Equal(errFoo, erFoo)),
|
|
|
|
massert.Not(massert.Equal(errBar, Base(erFoo))),
|
|
|
|
massert.Equal(true, Equal(errFoo, erFoo)),
|
|
|
|
massert.Equal(false, Equal(errBar, erFoo)),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
|
|
|
func TestValue(t *T) {
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Nil(WithValue(nil, "foo", "bar")),
|
|
|
|
massert.Nil(Value(nil, "foo")),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2019-02-27 18:05:51 +00:00
|
|
|
e1 := New("foo")
|
2019-02-09 19:08:30 +00:00
|
|
|
e1 = WithValue(e1, "a", "A")
|
|
|
|
e2 := WithValue(errors.New("bar"), "a", "A")
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Equal("A", Value(e1, "a")),
|
|
|
|
massert.Equal("A", Value(e2, "a")),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-02-09 19:08:30 +00:00
|
|
|
|
|
|
|
e3 := WithValue(e2, "a", "AAA")
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Equal("A", Value(e1, "a")),
|
|
|
|
massert.Equal("A", Value(e2, "a")),
|
|
|
|
massert.Equal("AAA", Value(e3, "a")),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mkErr(ctx context.Context, err error) error {
|
2019-02-27 18:05:51 +00:00
|
|
|
return Wrap(err, ctx)
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCtx(t *T) {
|
|
|
|
ctxA := mctx.Annotate(context.Background(), "0", "ZERO", "1", "one")
|
|
|
|
ctxB := mctx.Annotate(context.Background(), "1", "ONE", "2", "TWO")
|
|
|
|
|
|
|
|
// use mkErr so that it's easy to test that the stack info isn't overwritten
|
|
|
|
// when Wrap is called with ctxB.
|
|
|
|
e := mkErr(ctxA, errors.New("hello"))
|
2019-02-27 18:05:51 +00:00
|
|
|
e = Wrap(e, ctxB)
|
2019-02-09 19:08:30 +00:00
|
|
|
|
|
|
|
err := massert.Equal(map[string]string{
|
|
|
|
"0": "ZERO",
|
|
|
|
"1": "ONE",
|
|
|
|
"2": "TWO",
|
|
|
|
"err": "hello",
|
2019-03-10 23:23:37 +00:00
|
|
|
"errLoc": "merr/merr_test.go:82",
|
2019-02-09 19:08:30 +00:00
|
|
|
}, mctx.Annotations(Context(e)).StringMap()).Assert()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|