You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

package test
import (
"path/filepath"
"reflect"
"runtime"
"testing"
)
// Equal asserts two parameters are equal by using reflect.DeepEqual.
func Equal(t *testing.T, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
_, file, line, _ := runtime.Caller(1)
t.Logf("\033[31m%s:%d:\n\n\t %#v (expected)\n\n\t!= %#v (actual)\033[39m\n\n",
filepath.Base(file), line, expected, actual)
t.FailNow()
}
}
// NotEqual asserts two parameters are not equal by using reflect.DeepEqual.
func NotEqual(t *testing.T, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
_, file, line, _ := runtime.Caller(1)
t.Logf("\033[31m%s:%d:\n\n\tvalue should not equal %#v\033[39m\n\n",
filepath.Base(file), line, actual)
t.FailNow()
}
}
// Nil asserts the parameter is nil.
func Nil(t *testing.T, object interface{}) {
if !isNil(object) {
_, file, line, _ := runtime.Caller(1)
t.Logf("\033[31m%s:%d:\n\n\t <nil> (expected)\n\n\t!= %#v (actual)\033[39m\n\n",
filepath.Base(file), line, object)
t.FailNow()
}
}
// NotNil asserts the parameter is not nil.
func NotNil(t *testing.T, object interface{}) {
if isNil(object) {
_, file, line, _ := runtime.Caller(1)
t.Logf("\033[31m%s:%d:\n\n\tExpected value not to be <nil>\033[39m\n\n",
filepath.Base(file), line)
t.FailNow()
}
}
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}