From f0032a19d1d320edfc38b355204eba073811a28f Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 19 Jul 2018 18:19:26 +0000 Subject: [PATCH] mlog: refactor tests to use massert --- mlog/errctx_test.go | 76 +++++++++++++++---------- mlog/mlog_test.go | 132 ++++++++++++++++++++++++++------------------ 2 files changed, 126 insertions(+), 82 deletions(-) diff --git a/mlog/errctx_test.go b/mlog/errctx_test.go index 72ce0da..832121b 100644 --- a/mlog/errctx_test.go +++ b/mlog/errctx_test.go @@ -6,70 +6,88 @@ import ( . "testing" "github.com/ansel1/merry" - "github.com/stretchr/testify/assert" + "github.com/mediocregopher/mediocre-go-lib/mtest/massert" ) func TestErrKV(t *T) { - assertErrKV := func(err error, exp KV) { + assertErrKV := func(err error, exp KV) massert.Assertion { got := KV(ErrKV(err).KV()) errSrc := got["errSrc"] - assert.NotEmpty(t, errSrc) - assert.True(t, strings.HasPrefix(errSrc.(string), "mlog/errctx_test.go")) 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") - assertErrKV(err, KV{"err": err.Error()}) + massert.Fatal(t, assertErrKV(err, KV{"err": err.Error()})) kv := KV{"a": "a"} err2 := ErrWithKV(err, kv) - assertErrKV(err, KV{"err": err.Error()}) - assertErrKV(err2, KV{"err": err.Error(), "a": "a"}) + massert.Fatal(t, massert.All( + assertErrKV(err, KV{"err": err.Error()}), + assertErrKV(err2, KV{"err": err.Error(), "a": "a"}), + )) // changing the kv now shouldn't do anything kv["a"] = "b" - assertErrKV(err, KV{"err": err.Error()}) - assertErrKV(err2, KV{"err": err.Error(), "a": "a"}) + massert.Fatal(t, massert.All( + assertErrKV(err, KV{"err": err.Error()}), + assertErrKV(err2, KV{"err": err.Error(), "a": "a"}), + )) // a new ErrWithKV shouldn't affect the previous one err3 := ErrWithKV(err2, KV{"b": "b"}) - assertErrKV(err, KV{"err": err.Error()}) - assertErrKV(err2, KV{"err": err2.Error(), "a": "a"}) - assertErrKV(err3, KV{"err": err3.Error(), "a": "a", "b": "b"}) + 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"}), + )) // make sure precedence works err4 := ErrWithKV(err3, KV{"b": "bb"}) - 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"}) + 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"}), + )) } func TestCtxKV(t *T) { ctx := context.Background() - assert.Equal(t, KV{}, CtxKV(ctx)) + massert.Fatal(t, massert.Equal(KV{}, CtxKV(ctx))) kv := KV{"a": "a"} ctx2 := CtxWithKV(ctx, kv) - assert.Equal(t, KV{}, CtxKV(ctx)) - assert.Equal(t, KV{"a": "a"}, CtxKV(ctx2)) + 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" - assert.Equal(t, KV{}, CtxKV(ctx)) - assert.Equal(t, KV{"a": "a"}, CtxKV(ctx2)) + 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"}) - assert.Equal(t, KV{}, CtxKV(ctx)) - assert.Equal(t, KV{"a": "a"}, CtxKV(ctx2)) - assert.Equal(t, KV{"a": "a", "b": "b"}, CtxKV(ctx3)) + 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"}) - assert.Equal(t, KV{}, CtxKV(ctx)) - assert.Equal(t, KV{"a": "a"}, CtxKV(ctx2)) - assert.Equal(t, KV{"a": "a", "b": "b"}, CtxKV(ctx3)) - assert.Equal(t, KV{"a": "a", "b": "bb"}, CtxKV(ctx4)) + 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)), + )) } diff --git a/mlog/mlog_test.go b/mlog/mlog_test.go index b4072e8..7dddad1 100644 --- a/mlog/mlog_test.go +++ b/mlog/mlog_test.go @@ -10,35 +10,44 @@ import ( "time" "github.com/mediocregopher/mediocre-go-lib/mtest/massert" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestTruncate(t *T) { - assert.Equal(t, "abc", Truncate("abc", 4)) - assert.Equal(t, "abc", Truncate("abc", 3)) - assert.Equal(t, "ab...", Truncate("abc", 2)) + massert.Fatal(t, massert.All( + massert.Equal("abc", Truncate("abc", 4)), + massert.Equal("abc", Truncate("abc", 3)), + massert.Equal("ab...", Truncate("abc", 2)), + )) } func TestKV(t *T) { var kv KV - assert.NotNil(t, kv.KV()) - assert.Empty(t, kv.KV()) + massert.Fatal(t, massert.All( + 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"} - kv2 := KV(kv.KV()) - kv["bar"] = "b" + kv2 := kv.KV() + kv["bur"] = "b" kv2["bar"] = "bb" - assert.Equal(t, KV{"foo": "a", "bar": "b"}, kv) - assert.Equal(t, KV{"foo": "a", "bar": "bb"}, kv2) + massert.Fatal(t, massert.All( + 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"} kv2 = kv.Set("bar", "wat") - assert.Equal(t, KV{"foo": "a"}, kv) - assert.Equal(t, KV{"foo": "a", "bar": "wat"}, kv2) + kv["bur"] = "ok" + 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) l := NewLogger(struct { io.Writer @@ -49,15 +58,17 @@ func TestLLog(t *T) { }) l.testMsgWrittenCh = make(chan struct{}, 10) - assertOut := func(expected string) { + assertOut := func(expected string) massert.Assertion { select { case <-l.testMsgWrittenCh: case <-time.After(1 * time.Second): t.Fatal("waited too long for msg to write") } out, err := buf.ReadString('\n') - require.NoError(t, err) - assert.Equal(t, expected, out) + return massert.All( + massert.Nil(err), + massert.Equal(expected, out), + ) } // Default max level should be INFO @@ -65,9 +76,11 @@ func TestLLog(t *T) { l.Log(InfoLevel, "bar") l.Log(WarnLevel, "baz") l.Log(ErrorLevel, "buz") - assertOut("~ INFO -- bar\n") - assertOut("~ WARN -- baz\n") - assertOut("~ ERROR -- buz\n") + massert.Fatal(t, massert.All( + assertOut("~ INFO -- bar\n"), + assertOut("~ WARN -- baz\n"), + assertOut("~ ERROR -- buz\n"), + )) { l := l.WithMaxLevel(WarnLevel) @@ -75,8 +88,10 @@ func TestLLog(t *T) { l.Log(InfoLevel, "bar") l.Log(WarnLevel, "baz") l.Log(ErrorLevel, "buz", KV{"a": "b"}) - assertOut("~ WARN -- baz\n") - assertOut("~ ERROR -- buz -- a=\"b\"\n") + massert.Fatal(t, massert.All( + assertOut("~ WARN -- baz\n"), + assertOut("~ ERROR -- buz -- a=\"b\"\n"), + )) } { @@ -87,61 +102,72 @@ func TestLLog(t *T) { l2.Log(InfoLevel, "bar") l2.Log(WarnLevel, "baz") l.Log(ErrorLevel, "buz") - assertOut("~ INFO -- BAR\n") - assertOut("~ WARN -- BAZ\n") - assertOut("~ ERROR -- buz\n") + massert.Fatal(t, massert.All( + assertOut("~ INFO -- BAR\n"), + assertOut("~ WARN -- BAZ\n"), + assertOut("~ ERROR -- buz\n"), + )) } } func TestDefaultWriteFn(t *T) { - assertFormat := func(postfix string, msg Message) { + assertFormat := func(postfix string, msg Message) massert.Assertion { expectedRegex := regexp.MustCompile(`^~ ` + postfix + `\n$`) buf := bytes.NewBuffer(make([]byte, 0, 128)) - assert.NoError(t, DefaultWriteFn(buf, msg)) + writeErr := DefaultWriteFn(buf, msg) line, err := buf.ReadString('\n') - require.NoError(t, err) - assert.True(t, expectedRegex.MatchString(line), "regex: %q line: %q", expectedRegex.String(), line) + return massert.Comment( + 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"} - assertFormat("INFO -- this is a test", msg) + massert.Fatal(t, assertFormat("INFO -- this is a test", msg)) 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() - 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() - 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) { - assertMerge := func(exp KV, kvs ...KVer) { + assertMerge := func(exp KV, kvs ...KVer) massert.Assertion { got := merge(kvs...) - assert.Equal(t, exp, got) + return massert.Equal(exp, got) } - assertMerge(KV{}) - assertMerge(KV{}, nil) - assertMerge(KV{}, nil, nil) + massert.Fatal(t, massert.All( + assertMerge(KV{}), + assertMerge(KV{}, nil), + assertMerge(KV{}, nil, nil), - assertMerge(KV{"a": "a"}, 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"}), + assertMerge(KV{"a": "a"}, nil, KV{"a": "a"}), + assertMerge(KV{"a": "a"}, KV{"a": "a"}, nil), - assertMerge( - KV{"a": "a", "b": "b"}, - KV{"a": "a"}, KV{"b": "b"}, - ) - assertMerge( - KV{"a": "a", "b": "b"}, - KV{"a": "a"}, KV{"b": "b"}, - ) - assertMerge( - KV{"a": "b"}, - KV{"a": "a"}, KV{"a": "b"}, - ) + assertMerge( + KV{"a": "a", "b": "b"}, + KV{"a": "a"}, KV{"b": "b"}, + ), + assertMerge( + KV{"a": "a", "b": "b"}, + KV{"a": "a"}, KV{"b": "b"}, + ), + assertMerge( + KV{"a": "b"}, + KV{"a": "a"}, KV{"a": "b"}, + ), + )) } func TestPrefix(t *T) {