diff options
| author | Shulhan <ms@kilabit.info> | 2019-03-26 02:24:43 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-03-26 02:24:43 +0700 |
| commit | 4ebea25ba12d695e248519a662f74c73ec7238b0 (patch) | |
| tree | 435aeb8878c36e005c5f3df288c6bc45f868dd26 | |
| parent | a1b4db8bbecd4f68b81ae77cc25cecea0fcbff8d (diff) | |
| download | pakakeh.go-4ebea25ba12d695e248519a662f74c73ec7238b0.tar.xz | |
test: change the error format to print "got" before "expectation"
Following CodeReviewComments on Go wiki about Useful Test Failures [1],
the common idiom here is that the actual value is printed first followed
by the expected value.
[1] https://github.com/golang/go/wiki/CodeReviewComments#useful-test-failures
| -rw-r--r-- | lib/test/test.go | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/lib/test/test.go b/lib/test/test.go index 19ec4325..b48ab626 100644 --- a/lib/test/test.go +++ b/lib/test/test.go @@ -8,7 +8,6 @@ package test import ( - "os" "reflect" "runtime" "testing" @@ -40,41 +39,40 @@ func printStackTrace(t testing.TB, trace []byte) { // Assert will compare two interfaces: `exp` and `got` whether its same with // `equal` value. // -// If comparison result is not same with `equal`, it will terminate the test -// program. +// If comparison result is not same with `equal`, it will print the result and +// expectation and then terminate the test routine. // func Assert(t *testing.T, name string, exp, got interface{}, equal bool) { - if reflect.DeepEqual(exp, got) != equal { - trace := make([]byte, 1024) - runtime.Stack(trace, false) + if reflect.DeepEqual(exp, got) == equal { + return + } - printStackTrace(t, trace) + trace := make([]byte, 1024) + runtime.Stack(trace, false) - t.Fatalf(">>> Expecting %s,\n"+ - "'%+v'\n"+ - " got,\n"+ - "'%+v'\n", name, exp, got) - os.Exit(1) - } + printStackTrace(t, trace) + + t.Fatalf(">>> Got %s:\n\t'%+v';\n"+ + " want:\n\t'%+v'\n", name, got, exp) } // // AssertBench will compare two interfaces: `exp` and `got` whether its same // with `equal` value. // -// If comparison result is not same with `equal`, it will terminate the test -// program. +// If comparison result is not same with `equal`, it will print the result and +// expectation and then terminate the test routine. // func AssertBench(b *testing.B, name string, exp, got interface{}, equal bool) { - if reflect.DeepEqual(exp, got) != equal { - trace := make([]byte, 1024) - runtime.Stack(trace, false) + if reflect.DeepEqual(exp, got) == equal { + return + } - printStackTrace(b, trace) + trace := make([]byte, 1024) + runtime.Stack(trace, false) - b.Fatalf("\n"+ - ">>> Expecting %s '%+v'\n"+ - " got '%+v'\n", name, exp, got) - os.Exit(1) - } + printStackTrace(b, trace) + + b.Fatalf(">>> Got %s:\n\t'%+v';\n"+ + " want:\n\t'%+v'\n", name, got, exp) } |
