42 lines
715 B
Go
Raw Normal View History

2023-07-05 13:46:19 +00:00
package assert
import (
"reflect"
)
// Nil asserts that the given parameter equals nil.
2023-08-31 15:03:52 +00:00
func Nil(t test, a any) {
2023-07-05 13:46:19 +00:00
if isNil(a) {
return
}
2023-08-31 15:03:52 +00:00
t.Errorf(oneParameter, file(), "Nil", a)
2023-07-05 13:46:19 +00:00
t.FailNow()
}
// NotNil asserts that the given parameter does not equal nil.
2023-08-31 15:03:52 +00:00
func NotNil(t test, a any) {
2023-07-05 13:46:19 +00:00
if !isNil(a) {
return
}
2023-08-31 15:03:52 +00:00
t.Errorf(oneParameter, file(), "NotNil", a)
2023-07-05 13:46:19 +00:00
t.FailNow()
}
// isNil returns true if the object is nil.
func isNil(object any) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return value.IsNil()
}
return false
}