Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,16 @@ func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}
//
// assert.NotNil(t, err)
func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
if !isNil(object) {
return true
}
if h, ok := t.(tHelper); ok {
h.Helper()
}
return Fail(t, "Expected value not to be nil.", msgAndArgs...)
if isNil(object) {
return Fail(t, "Expected value not to be nil.", msgAndArgs...)
}
if !isNillableKind(reflect.ValueOf(object).Kind()) {
return Fail(t, fmt.Sprintf("Expected value not to be nil, but got non-nillable type %T", object), msgAndArgs...)
}
return true
}

// isNil checks if a specified object is nil or not, without Failing.
Expand All @@ -722,15 +725,21 @@ func isNil(object interface{}) bool {
}

value := reflect.ValueOf(object)
switch value.Kind() {
if isNillableKind(value.Kind()) {
return value.IsNil()
}

return false
}

func isNillableKind(kind reflect.Kind) bool {
switch kind {
case
reflect.Chan, reflect.Func,
reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.UnsafePointer:

return value.IsNil()
return true
}

return false
}

Expand Down
Loading