Skip to content

Commit

Permalink
Merge pull request #1506 from stretchr/assert-simplify-isNil
Browse files Browse the repository at this point in the history
assert: simplify isNil implementation
  • Loading branch information
MovieStoreGuy authored Jan 21, 2024
2 parents 1f53b4e + cbcc423 commit 92533fa
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,34 +632,20 @@ func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
return Fail(t, "Expected value not to be nil.", msgAndArgs...)
}

// containsKind checks if a specified kind in the slice of kinds.
func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
for i := 0; i < len(kinds); i++ {
if kind == kinds[i] {
return true
}
}

return false
}

// isNil checks if a specified object is nil or not, without Failing.
func isNil(object interface{}) bool {
if object == nil {
return true
}

value := reflect.ValueOf(object)
kind := value.Kind()
isNilableKind := containsKind(
[]reflect.Kind{
reflect.Chan, reflect.Func,
reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.UnsafePointer},
kind)

if isNilableKind && value.IsNil() {
return true
switch value.Kind() {
case
reflect.Chan, reflect.Func,
reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.UnsafePointer:

return value.IsNil()
}

return false
Expand Down

0 comments on commit 92533fa

Please sign in to comment.