blob: 85cb9ec9f1d75efa064a51c6f870f71f5ce1f021 [file] [log] [blame]
Jamie Hannafordb2b237f2014-09-15 12:17:47 +02001package testhelper
2
Jamie Hannaford6bcf2582014-09-15 12:52:51 +02003import (
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +02004 "fmt"
5 "path/filepath"
Jamie Hannaford6bcf2582014-09-15 12:52:51 +02006 "reflect"
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +02007 "runtime"
Jamie Hannaford6bcf2582014-09-15 12:52:51 +02008 "testing"
9)
Jamie Hannafordb2b237f2014-09-15 12:17:47 +020010
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +020011func prefix() string {
12 _, file, line, _ := runtime.Caller(3)
13 return fmt.Sprintf("Failure in %s, line %d:", filepath.Base(file), line)
14}
15
16func green(str interface{}) string {
17 return fmt.Sprintf("\033[0m\033[1;32m%#v\033[0m\033[1;31m", str)
18}
19
20func yellow(str interface{}) string {
21 return fmt.Sprintf("\033[0m\033[1;33m%#v\033[0m\033[1;31m", str)
22}
23
24func logFatal(t *testing.T, str string) {
25 t.Fatalf("\033[1;31m%s %s\033[0m", prefix(), str)
26}
27
28func logError(t *testing.T, str string) {
29 t.Errorf("\033[1;31m%s %s\033[0m", prefix(), str)
30}
31
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020032// AssertEquals compares two arbitrary values and performs a comparison. If the
Jamie Hannaford2964aed2014-09-15 12:20:02 +020033// comparison fails, a fatal error is raised that will fail the test
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020034func AssertEquals(t *testing.T, expected, actual interface{}) {
Jamie Hannafordb2b237f2014-09-15 12:17:47 +020035 if expected != actual {
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +020036 logFatal(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
Jamie Hannafordb2b237f2014-09-15 12:17:47 +020037 }
38}
39
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020040// CheckEquals is similar to AssertEquals, except with a non-fatal error
41func CheckEquals(t *testing.T, expected, actual interface{}) {
42 if expected != actual {
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +020043 logError(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020044 }
45}
46
47// AssertDeepEquals - like Equals - performs a comparison - but on more complex
Jamie Hannaford6bcf2582014-09-15 12:52:51 +020048// structures that requires deeper inspection
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +020049func AssertDeepEquals(t *testing.T, expected, actual interface{}) {
50 if !reflect.DeepEqual(expected, actual) {
51 logFatal(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
Jamie Hannaford6bcf2582014-09-15 12:52:51 +020052 }
53}
54
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020055// CheckDeepEquals is similar to AssertDeepEquals, except with a non-fatal error
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +020056func CheckDeepEquals(t *testing.T, expected, actual interface{}) {
57 if !reflect.DeepEqual(expected, actual) {
58 logError(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020059 }
60}
61
62// AssertNoErr is a convenience function for checking whether an error value is
63// an actual error
64func AssertNoErr(t *testing.T, e error) {
Jamie Hannafordb2b237f2014-09-15 12:17:47 +020065 if e != nil {
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +020066 logFatal(t, fmt.Sprintf("unexpected error %s", yellow(e)))
Jamie Hannafordb2b237f2014-09-15 12:17:47 +020067 }
68}
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020069
70// CheckNoErr is similar to AssertNoErr, except with a non-fatal error
71func CheckNoErr(t *testing.T, e error) {
72 if e != nil {
Jamie Hannaford6d8dcd02014-09-25 13:55:09 +020073 logError(t, fmt.Sprintf("unexpected error %s", yellow(e)))
Jamie Hannaford0f26e5c2014-09-15 15:46:58 +020074 }
75}