2018-01-16 13:59:52 +00:00
|
|
|
package mlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
. "testing"
|
|
|
|
|
|
|
|
"github.com/ansel1/merry"
|
2018-07-19 18:19:26 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
2018-01-16 13:59:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrKV(t *T) {
|
2018-07-19 18:19:26 +00:00
|
|
|
assertErrKV := func(err error, exp KV) massert.Assertion {
|
2018-01-16 13:59:52 +00:00
|
|
|
got := KV(ErrKV(err).KV())
|
|
|
|
errSrc := got["errSrc"]
|
|
|
|
delete(got, "errSrc")
|
2018-07-19 18:19:26 +00:00
|
|
|
return massert.All(
|
|
|
|
massert.Not(massert.Nil(errSrc)),
|
|
|
|
massert.Equal(true, strings.HasPrefix(errSrc.(string), "mlog/errctx_test.go")),
|
|
|
|
massert.Equal(exp, got),
|
|
|
|
)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := merry.New("foo")
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, assertErrKV(err, KV{"err": err.Error()}))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
kv := KV{"a": "a"}
|
|
|
|
err2 := ErrWithKV(err, kv)
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertErrKV(err, KV{"err": err.Error()}),
|
|
|
|
assertErrKV(err2, KV{"err": err.Error(), "a": "a"}),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
// changing the kv now shouldn't do anything
|
|
|
|
kv["a"] = "b"
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertErrKV(err, KV{"err": err.Error()}),
|
|
|
|
assertErrKV(err2, KV{"err": err.Error(), "a": "a"}),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
// a new ErrWithKV shouldn't affect the previous one
|
|
|
|
err3 := ErrWithKV(err2, KV{"b": "b"})
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertErrKV(err, KV{"err": err.Error()}),
|
|
|
|
assertErrKV(err2, KV{"err": err2.Error(), "a": "a"}),
|
|
|
|
assertErrKV(err3, KV{"err": err3.Error(), "a": "a", "b": "b"}),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
// make sure precedence works
|
|
|
|
err4 := ErrWithKV(err3, KV{"b": "bb"})
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertErrKV(err, KV{"err": err.Error()}),
|
|
|
|
assertErrKV(err2, KV{"err": err2.Error(), "a": "a"}),
|
|
|
|
assertErrKV(err3, KV{"err": err3.Error(), "a": "a", "b": "b"}),
|
|
|
|
assertErrKV(err4, KV{"err": err4.Error(), "a": "a", "b": "bb"}),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCtxKV(t *T) {
|
|
|
|
ctx := context.Background()
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.Equal(KV{}, CtxKV(ctx)))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
kv := KV{"a": "a"}
|
|
|
|
ctx2 := CtxWithKV(ctx, kv)
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal(KV{}, CtxKV(ctx)),
|
|
|
|
massert.Equal(KV{"a": "a"}, CtxKV(ctx2)),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
// changing the kv now shouldn't do anything
|
|
|
|
kv["a"] = "b"
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal(KV{}, CtxKV(ctx)),
|
|
|
|
massert.Equal(KV{"a": "a"}, CtxKV(ctx2)),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
// a new CtxWithKV shouldn't affect the previous one
|
|
|
|
ctx3 := CtxWithKV(ctx2, KV{"b": "b"})
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal(KV{}, CtxKV(ctx)),
|
|
|
|
massert.Equal(KV{"a": "a"}, CtxKV(ctx2)),
|
|
|
|
massert.Equal(KV{"a": "a", "b": "b"}, CtxKV(ctx3)),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
// make sure precedence works
|
|
|
|
ctx4 := CtxWithKV(ctx3, KV{"b": "bb"})
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal(KV{}, CtxKV(ctx)),
|
|
|
|
massert.Equal(KV{"a": "a"}, CtxKV(ctx2)),
|
|
|
|
massert.Equal(KV{"a": "a", "b": "b"}, CtxKV(ctx3)),
|
|
|
|
massert.Equal(KV{"a": "a", "b": "bb"}, CtxKV(ctx4)),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|