assert.go raw

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