Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 1 | package testhelper |
| 2 | |
Jamie Hannaford | 6bcf258 | 2014-09-15 12:52:51 +0200 | [diff] [blame] | 3 | import ( |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 4 | "fmt" |
| 5 | "path/filepath" |
Jamie Hannaford | 6bcf258 | 2014-09-15 12:52:51 +0200 | [diff] [blame] | 6 | "reflect" |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 7 | "runtime" |
Jamie Hannaford | 6bcf258 | 2014-09-15 12:52:51 +0200 | [diff] [blame] | 8 | "testing" |
| 9 | ) |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 10 | |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 11 | func prefix() string { |
| 12 | _, file, line, _ := runtime.Caller(3) |
| 13 | return fmt.Sprintf("Failure in %s, line %d:", filepath.Base(file), line) |
| 14 | } |
| 15 | |
| 16 | func green(str interface{}) string { |
| 17 | return fmt.Sprintf("\033[0m\033[1;32m%#v\033[0m\033[1;31m", str) |
| 18 | } |
| 19 | |
| 20 | func yellow(str interface{}) string { |
| 21 | return fmt.Sprintf("\033[0m\033[1;33m%#v\033[0m\033[1;31m", str) |
| 22 | } |
| 23 | |
| 24 | func logFatal(t *testing.T, str string) { |
| 25 | t.Fatalf("\033[1;31m%s %s\033[0m", prefix(), str) |
| 26 | } |
| 27 | |
| 28 | func logError(t *testing.T, str string) { |
| 29 | t.Errorf("\033[1;31m%s %s\033[0m", prefix(), str) |
| 30 | } |
| 31 | |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 32 | // AssertEquals compares two arbitrary values and performs a comparison. If the |
Jamie Hannaford | 2964aed | 2014-09-15 12:20:02 +0200 | [diff] [blame] | 33 | // comparison fails, a fatal error is raised that will fail the test |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 34 | func AssertEquals(t *testing.T, expected, actual interface{}) { |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 35 | if expected != actual { |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 36 | logFatal(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual))) |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 40 | // CheckEquals is similar to AssertEquals, except with a non-fatal error |
| 41 | func CheckEquals(t *testing.T, expected, actual interface{}) { |
| 42 | if expected != actual { |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 43 | logError(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual))) |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | // AssertDeepEquals - like Equals - performs a comparison - but on more complex |
Jamie Hannaford | 6bcf258 | 2014-09-15 12:52:51 +0200 | [diff] [blame] | 48 | // structures that requires deeper inspection |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 49 | func 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 Hannaford | 6bcf258 | 2014-09-15 12:52:51 +0200 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 55 | // CheckDeepEquals is similar to AssertDeepEquals, except with a non-fatal error |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 56 | func 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 Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | // AssertNoErr is a convenience function for checking whether an error value is |
| 63 | // an actual error |
| 64 | func AssertNoErr(t *testing.T, e error) { |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 65 | if e != nil { |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 66 | logFatal(t, fmt.Sprintf("unexpected error %s", yellow(e))) |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 67 | } |
| 68 | } |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 69 | |
| 70 | // CheckNoErr is similar to AssertNoErr, except with a non-fatal error |
| 71 | func CheckNoErr(t *testing.T, e error) { |
| 72 | if e != nil { |
Jamie Hannaford | 6d8dcd0 | 2014-09-25 13:55:09 +0200 | [diff] [blame^] | 73 | logError(t, fmt.Sprintf("unexpected error %s", yellow(e))) |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame] | 74 | } |
| 75 | } |