From 5cb00b8ac23b98b57a17a508e1e42f6f6f53187d Mon Sep 17 00:00:00 2001 From: Camille Janicki Date: Thu, 27 Sep 2018 19:08:35 -0700 Subject: [PATCH 1/2] Add msg parameter to Equals function in testutil Co-authored-by: Chris Marchbanks Signed-off-by: Camille Janicki --- testutil/testutil.go | 14 ++++++++++++-- wal/wal_test.go | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/testutil/testutil.go b/testutil/testutil.go index cde0e4f9..390c049e 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -58,10 +58,20 @@ func NotOk(tb testing.TB, err error) { } // Equals fails the test if exp is not equal to act. -func Equals(tb testing.TB, exp, act interface{}) { +func Equals(tb testing.TB, exp, act interface{}, msgAndArgs ...interface{}) { if !reflect.DeepEqual(exp, act) { _, file, line, _ := runtime.Caller(1) - fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act) + fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v%s\033[39m\n\n", filepath.Base(file), line, exp, act, formatMessage(msgAndArgs)) tb.FailNow() } } + +func formatMessage(msgAndArgs []interface{}) string { + if len(msgAndArgs) == 0 { + return "" + } + if _, ok := msgAndArgs[0].(string); !ok { + return "" + } + return fmt.Sprintf(fmt.Sprintf("\n\nmsg: %s", msgAndArgs[0]), msgAndArgs[1:]...) +} diff --git a/wal/wal_test.go b/wal/wal_test.go index 72f46253..6152a90f 100644 --- a/wal/wal_test.go +++ b/wal/wal_test.go @@ -144,7 +144,7 @@ func TestReader(t *testing.T) { if j >= len(c.exp) { t.Fatal("received more records than inserted") } - testutil.Equals(t, c.exp[j], rec) + testutil.Equals(t, c.exp[j], rec, "Bytes within record did not match expected Bytes") } if !c.fail && r.Err() != nil { t.Fatalf("unexpected error: %s", r.Err()) @@ -256,6 +256,7 @@ func TestWAL_Repair(t *testing.T) { // We create 3 segments with 3 records each and then corrupt the 2nd record // of the 2nd segment. // As a result we want a repaired WAL with the first 4 records intact. + intactRecords := 4 w, err := NewSize(nil, nil, dir, 3*pageSize) testutil.Ok(t, err) @@ -306,7 +307,7 @@ func TestWAL_Repair(t *testing.T) { result = append(result, append(b, r.Record()...)) } testutil.Ok(t, r.Err()) - testutil.Equals(t, 4, len(result)) + testutil.Equals(t, intactRecords, len(result), "Wrong number of intact records") for i, r := range result { if !bytes.Equal(records[i], r) { From 522352a869f5d2c1d6840f6b2b2ff483629192f9 Mon Sep 17 00:00:00 2001 From: Camille Janicki Date: Tue, 2 Oct 2018 19:45:16 -0700 Subject: [PATCH 2/2] Swap print message to come before exp and got Signed-off-by: Camille Janicki --- testutil/testutil.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/testutil/testutil.go b/testutil/testutil.go index 390c049e..06b9747c 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -61,7 +61,7 @@ func NotOk(tb testing.TB, err error) { func Equals(tb testing.TB, exp, act interface{}, msgAndArgs ...interface{}) { if !reflect.DeepEqual(exp, act) { _, file, line, _ := runtime.Caller(1) - fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v%s\033[39m\n\n", filepath.Base(file), line, exp, act, formatMessage(msgAndArgs)) + fmt.Printf("\033[31m%s:%d:%s\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, formatMessage(msgAndArgs), exp, act) tb.FailNow() } } @@ -70,8 +70,9 @@ func formatMessage(msgAndArgs []interface{}) string { if len(msgAndArgs) == 0 { return "" } - if _, ok := msgAndArgs[0].(string); !ok { - return "" + + if msg, ok := msgAndArgs[0].(string); ok { + return fmt.Sprintf("\n\nmsg: "+msg, msgAndArgs[1:]...) } - return fmt.Sprintf(fmt.Sprintf("\n\nmsg: %s", msgAndArgs[0]), msgAndArgs[1:]...) + return "" }