mediocre-go-lib/mlog/errctx_test.go

45 lines
1.1 KiB
Go
Raw Normal View History

package mlog
import (
"context"
. "testing"
2018-07-19 18:19:26 +00:00
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
)
func TestCtxKV(t *T) {
ctx := context.Background()
2018-07-19 18:19:26 +00:00
massert.Fatal(t, massert.Equal(KV{}, CtxKV(ctx)))
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)),
))
// 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)),
))
// 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)),
))
// 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)),
))
}