assert.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package utils
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. )
  7. func isNil(object interface{}) bool {
  8. if object == nil {
  9. return true
  10. }
  11. value := reflect.ValueOf(object)
  12. kind := value.Kind()
  13. isNilableKind := containsKind(
  14. []reflect.Kind{
  15. reflect.Chan, reflect.Func,
  16. reflect.Interface, reflect.Map,
  17. reflect.Ptr, reflect.Slice},
  18. kind)
  19. if isNilableKind && value.IsNil() {
  20. return true
  21. }
  22. return false
  23. }
  24. func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
  25. for i := 0; i < len(kinds); i++ {
  26. if kind == kinds[i] {
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. func AssertEqual(t *testing.T, a, b interface{}) {
  33. if !reflect.DeepEqual(a, b) {
  34. t.Errorf("%v != %v", a, b)
  35. }
  36. }
  37. func AssertNil(t *testing.T, object interface{}) {
  38. if !isNil(object) {
  39. t.Errorf("%v is not nil", object)
  40. }
  41. }
  42. func AssertNotNil(t *testing.T, object interface{}) {
  43. if isNil(object) {
  44. t.Errorf("%v is nil", object)
  45. }
  46. }
  47. func AssertContains(t *testing.T, contains string, msgAndArgs ...string) {
  48. for _, value := range msgAndArgs {
  49. if ok := strings.Contains(contains, value); !ok {
  50. t.Errorf("%s does not contain %s", contains, value)
  51. }
  52. }
  53. }