mlog: refactor tests to use massert

This commit is contained in:
Brian Picciano 2018-07-19 18:19:26 +00:00
parent 58fd73d540
commit f0032a19d1
2 changed files with 126 additions and 82 deletions

View File

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

View File

@ -10,35 +10,44 @@ import (
"time" "time"
"github.com/mediocregopher/mediocre-go-lib/mtest/massert" "github.com/mediocregopher/mediocre-go-lib/mtest/massert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestTruncate(t *T) { func TestTruncate(t *T) {
assert.Equal(t, "abc", Truncate("abc", 4)) massert.Fatal(t, massert.All(
assert.Equal(t, "abc", Truncate("abc", 3)) massert.Equal("abc", Truncate("abc", 4)),
assert.Equal(t, "ab...", Truncate("abc", 2)) massert.Equal("abc", Truncate("abc", 3)),
massert.Equal("ab...", Truncate("abc", 2)),
))
} }
func TestKV(t *T) { func TestKV(t *T) {
var kv KV var kv KV
assert.NotNil(t, kv.KV()) massert.Fatal(t, massert.All(
assert.Empty(t, kv.KV()) massert.Not(massert.Nil(kv.KV())),
massert.Len(kv.KV(), 0),
))
// test that the KV method returns a new KV instance
kv = KV{"foo": "a"} kv = KV{"foo": "a"}
kv2 := KV(kv.KV()) kv2 := kv.KV()
kv["bar"] = "b" kv["bur"] = "b"
kv2["bar"] = "bb" kv2["bar"] = "bb"
assert.Equal(t, KV{"foo": "a", "bar": "b"}, kv) massert.Fatal(t, massert.All(
assert.Equal(t, KV{"foo": "a", "bar": "bb"}, kv2) massert.Equal(KV{"foo": "a", "bur": "b"}, kv),
massert.Equal(KV{"foo": "a", "bar": "bb"}, kv2),
))
// test that the Set method returns a new KV instance
kv = KV{"foo": "a"} kv = KV{"foo": "a"}
kv2 = kv.Set("bar", "wat") kv2 = kv.Set("bar", "wat")
assert.Equal(t, KV{"foo": "a"}, kv) kv["bur"] = "ok"
assert.Equal(t, KV{"foo": "a", "bar": "wat"}, kv2) massert.Fatal(t, massert.All(
massert.Equal(KV{"foo": "a", "bur": "ok"}, kv),
massert.Equal(KV{"foo": "a", "bar": "wat"}, kv2),
))
} }
func TestLLog(t *T) { func TestLogger(t *T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
l := NewLogger(struct { l := NewLogger(struct {
io.Writer io.Writer
@ -49,15 +58,17 @@ func TestLLog(t *T) {
}) })
l.testMsgWrittenCh = make(chan struct{}, 10) l.testMsgWrittenCh = make(chan struct{}, 10)
assertOut := func(expected string) { assertOut := func(expected string) massert.Assertion {
select { select {
case <-l.testMsgWrittenCh: case <-l.testMsgWrittenCh:
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
t.Fatal("waited too long for msg to write") t.Fatal("waited too long for msg to write")
} }
out, err := buf.ReadString('\n') out, err := buf.ReadString('\n')
require.NoError(t, err) return massert.All(
assert.Equal(t, expected, out) massert.Nil(err),
massert.Equal(expected, out),
)
} }
// Default max level should be INFO // Default max level should be INFO
@ -65,9 +76,11 @@ func TestLLog(t *T) {
l.Log(InfoLevel, "bar") l.Log(InfoLevel, "bar")
l.Log(WarnLevel, "baz") l.Log(WarnLevel, "baz")
l.Log(ErrorLevel, "buz") l.Log(ErrorLevel, "buz")
assertOut("~ INFO -- bar\n") massert.Fatal(t, massert.All(
assertOut("~ WARN -- baz\n") assertOut("~ INFO -- bar\n"),
assertOut("~ ERROR -- buz\n") assertOut("~ WARN -- baz\n"),
assertOut("~ ERROR -- buz\n"),
))
{ {
l := l.WithMaxLevel(WarnLevel) l := l.WithMaxLevel(WarnLevel)
@ -75,8 +88,10 @@ func TestLLog(t *T) {
l.Log(InfoLevel, "bar") l.Log(InfoLevel, "bar")
l.Log(WarnLevel, "baz") l.Log(WarnLevel, "baz")
l.Log(ErrorLevel, "buz", KV{"a": "b"}) l.Log(ErrorLevel, "buz", KV{"a": "b"})
assertOut("~ WARN -- baz\n") massert.Fatal(t, massert.All(
assertOut("~ ERROR -- buz -- a=\"b\"\n") assertOut("~ WARN -- baz\n"),
assertOut("~ ERROR -- buz -- a=\"b\"\n"),
))
} }
{ {
@ -87,61 +102,72 @@ func TestLLog(t *T) {
l2.Log(InfoLevel, "bar") l2.Log(InfoLevel, "bar")
l2.Log(WarnLevel, "baz") l2.Log(WarnLevel, "baz")
l.Log(ErrorLevel, "buz") l.Log(ErrorLevel, "buz")
assertOut("~ INFO -- BAR\n") massert.Fatal(t, massert.All(
assertOut("~ WARN -- BAZ\n") assertOut("~ INFO -- BAR\n"),
assertOut("~ ERROR -- buz\n") assertOut("~ WARN -- BAZ\n"),
assertOut("~ ERROR -- buz\n"),
))
} }
} }
func TestDefaultWriteFn(t *T) { func TestDefaultWriteFn(t *T) {
assertFormat := func(postfix string, msg Message) { assertFormat := func(postfix string, msg Message) massert.Assertion {
expectedRegex := regexp.MustCompile(`^~ ` + postfix + `\n$`) expectedRegex := regexp.MustCompile(`^~ ` + postfix + `\n$`)
buf := bytes.NewBuffer(make([]byte, 0, 128)) buf := bytes.NewBuffer(make([]byte, 0, 128))
assert.NoError(t, DefaultWriteFn(buf, msg)) writeErr := DefaultWriteFn(buf, msg)
line, err := buf.ReadString('\n') line, err := buf.ReadString('\n')
require.NoError(t, err) return massert.Comment(
assert.True(t, expectedRegex.MatchString(line), "regex: %q line: %q", expectedRegex.String(), line) massert.All(
massert.Nil(writeErr),
massert.Nil(err),
massert.Equal(true, expectedRegex.MatchString(line)),
),
"line:%q", line,
)
} }
msg := Message{Level: InfoLevel, Msg: "this is a test"} msg := Message{Level: InfoLevel, Msg: "this is a test"}
assertFormat("INFO -- this is a test", msg) massert.Fatal(t, assertFormat("INFO -- this is a test", msg))
msg.KV = KV{}.KV() msg.KV = KV{}.KV()
assertFormat("INFO -- this is a test", msg) massert.Fatal(t, assertFormat("INFO -- this is a test", msg))
msg.KV = KV{"foo": "a"}.KV() msg.KV = KV{"foo": "a"}.KV()
assertFormat("INFO -- this is a test -- foo=\"a\"", msg) massert.Fatal(t, assertFormat("INFO -- this is a test -- foo=\"a\"", msg))
msg.KV = KV{"foo": "a", "bar": "b"}.KV() msg.KV = KV{"foo": "a", "bar": "b"}.KV()
assertFormat("INFO -- this is a test -- bar=\"b\" foo=\"a\"", msg) massert.Fatal(t,
assertFormat("INFO -- this is a test -- bar=\"b\" foo=\"a\"", msg))
} }
func TestMerge(t *T) { func TestMerge(t *T) {
assertMerge := func(exp KV, kvs ...KVer) { assertMerge := func(exp KV, kvs ...KVer) massert.Assertion {
got := merge(kvs...) got := merge(kvs...)
assert.Equal(t, exp, got) return massert.Equal(exp, got)
} }
assertMerge(KV{}) massert.Fatal(t, massert.All(
assertMerge(KV{}, nil) assertMerge(KV{}),
assertMerge(KV{}, nil, nil) assertMerge(KV{}, nil),
assertMerge(KV{}, nil, nil),
assertMerge(KV{"a": "a"}, KV{"a": "a"}) assertMerge(KV{"a": "a"}, KV{"a": "a"}),
assertMerge(KV{"a": "a"}, nil, KV{"a": "a"}) assertMerge(KV{"a": "a"}, nil, KV{"a": "a"}),
assertMerge(KV{"a": "a"}, KV{"a": "a"}, nil) assertMerge(KV{"a": "a"}, KV{"a": "a"}, nil),
assertMerge( assertMerge(
KV{"a": "a", "b": "b"}, KV{"a": "a", "b": "b"},
KV{"a": "a"}, KV{"b": "b"}, KV{"a": "a"}, KV{"b": "b"},
) ),
assertMerge( assertMerge(
KV{"a": "a", "b": "b"}, KV{"a": "a", "b": "b"},
KV{"a": "a"}, KV{"b": "b"}, KV{"a": "a"}, KV{"b": "b"},
) ),
assertMerge( assertMerge(
KV{"a": "b"}, KV{"a": "b"},
KV{"a": "a"}, KV{"a": "b"}, KV{"a": "a"}, KV{"a": "b"},
) ),
))
} }
func TestPrefix(t *T) { func TestPrefix(t *T) {