assertions.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package test
  2. import (
  3. "path/filepath"
  4. "reflect"
  5. "runtime"
  6. "testing"
  7. )
  8. func Equal(t *testing.T, expected, actual interface{}) {
  9. if !reflect.DeepEqual(expected, actual) {
  10. _, file, line, _ := runtime.Caller(1)
  11. t.Logf("\033[31m%s:%d:\n\n\t %#v (expected)\n\n\t!= %#v (actual)\033[39m\n\n",
  12. filepath.Base(file), line, expected, actual)
  13. t.FailNow()
  14. }
  15. }
  16. func NotEqual(t *testing.T, expected, actual interface{}) {
  17. if reflect.DeepEqual(expected, actual) {
  18. _, file, line, _ := runtime.Caller(1)
  19. t.Logf("\033[31m%s:%d:\n\n\tnexp: %#v\n\n\tgot: %#v\033[39m\n\n",
  20. filepath.Base(file), line, expected, actual)
  21. t.FailNow()
  22. }
  23. }
  24. func Nil(t *testing.T, object interface{}) {
  25. if !isNil(object) {
  26. _, file, line, _ := runtime.Caller(1)
  27. t.Logf("\033[31m%s:%d:\n\n\t <nil> (expected)\n\n\t!= %#v (actual)\033[39m\n\n",
  28. filepath.Base(file), line, object)
  29. t.FailNow()
  30. }
  31. }
  32. func NotNil(t *testing.T, object interface{}) {
  33. if isNil(object) {
  34. _, file, line, _ := runtime.Caller(1)
  35. t.Logf("\033[31m%s:%d:\n\n\tExpected value not to be <nil>\033[39m\n\n",
  36. filepath.Base(file), line)
  37. t.FailNow()
  38. }
  39. }
  40. func isNil(object interface{}) bool {
  41. if object == nil {
  42. return true
  43. }
  44. value := reflect.ValueOf(object)
  45. kind := value.Kind()
  46. if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
  47. return true
  48. }
  49. return false
  50. }