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 ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 7 | |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame^] | 8 | // AssertEquals compares two arbitrary values and performs a comparison. If the |
Jamie Hannaford | 2964aed | 2014-09-15 12:20:02 +0200 | [diff] [blame] | 9 | // comparison fails, a fatal error is raised that will fail the test |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame^] | 10 | func AssertEquals(t *testing.T, expected, actual interface{}) { |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 11 | if expected != actual { |
| 12 | t.Fatalf("Expected [%#v] but got [%#v]", expected, actual) |
| 13 | } |
| 14 | } |
| 15 | |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame^] | 16 | // CheckEquals is similar to AssertEquals, except with a non-fatal error |
| 17 | func CheckEquals(t *testing.T, expected, actual interface{}) { |
| 18 | if expected != actual { |
| 19 | t.Errorf("Expected [%#v] but got [%#v]", expected, actual) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // AssertDeepEquals - like Equals - performs a comparison - but on more complex |
Jamie Hannaford | 6bcf258 | 2014-09-15 12:52:51 +0200 | [diff] [blame] | 24 | // structures that requires deeper inspection |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame^] | 25 | func AssertDeepEquals(t *testing.T, actual, expected interface{}) { |
Jamie Hannaford | 6bcf258 | 2014-09-15 12:52:51 +0200 | [diff] [blame] | 26 | if !reflect.DeepEqual(actual, expected) { |
| 27 | t.Fatalf("Expected %#v but got %#v", expected, actual) |
| 28 | } |
| 29 | } |
| 30 | |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame^] | 31 | // CheckDeepEquals is similar to AssertDeepEquals, except with a non-fatal error |
| 32 | func CheckDeepEquals(t *testing.T, actual, expected interface{}) { |
| 33 | if !reflect.DeepEqual(actual, expected) { |
| 34 | t.Errorf("Expected %#v but got %#v", expected, actual) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // AssertNoErr is a convenience function for checking whether an error value is |
| 39 | // an actual error |
| 40 | func AssertNoErr(t *testing.T, e error) { |
Jamie Hannaford | b2b237f | 2014-09-15 12:17:47 +0200 | [diff] [blame] | 41 | if e != nil { |
| 42 | t.Fatalf("Unexpected error: %#v", e) |
| 43 | } |
| 44 | } |
Jamie Hannaford | 0f26e5c | 2014-09-15 15:46:58 +0200 | [diff] [blame^] | 45 | |
| 46 | // CheckNoErr is similar to AssertNoErr, except with a non-fatal error |
| 47 | func CheckNoErr(t *testing.T, e error) { |
| 48 | if e != nil { |
| 49 | t.Errorf("Unexpected error: %#v", e) |
| 50 | } |
| 51 | } |