Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Add msg parameter to Equals function in testutil #398

Merged
merged 2 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to print the message before exp and got. Else it might get tedious trying to get to the end and find the message. @krasi-georgiev what do you say?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes tested it locally and you are right.

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()
}
}

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:]...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit)
As we are already checking for msgAndArgs[0] to be string, we can reuse the value from that and avoid additional fmt.Sprintf.

Maybe something like

if msg, ok := msgAndArgs[0].(string); ok {
	return fmt.Sprintf("\n\nmsg: "+msg, msgAndArgs[1:]...)
}
return ""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}
5 changes: 3 additions & 2 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down