mirror of
https://github.com/stretchr/testify.git
synced 2025-05-31 11:42:44 +00:00
Merge pull request #220 from bmatsuo/fix-notnil-for-typed-nil-values
Fix NotNil assertion for typed nil values
This commit is contained in:
commit
bcd32dcc8b
@ -287,24 +287,10 @@ func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}
|
|||||||
//
|
//
|
||||||
// Returns whether the assertion was successful (true) or not (false).
|
// Returns whether the assertion was successful (true) or not (false).
|
||||||
func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
|
func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
if !isNil(object) {
|
||||||
success := true
|
return true
|
||||||
|
|
||||||
if object == nil {
|
|
||||||
success = false
|
|
||||||
} else {
|
|
||||||
value := reflect.ValueOf(object)
|
|
||||||
kind := value.Kind()
|
|
||||||
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
|
|
||||||
success = false
|
|
||||||
}
|
}
|
||||||
}
|
return Fail(t, "Expected value not to be nil.", msgAndArgs...)
|
||||||
|
|
||||||
if !success {
|
|
||||||
Fail(t, "Expected value not to be nil.", msgAndArgs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return success
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// isNil checks if a specified object is nil or not, without Failing.
|
// isNil checks if a specified object is nil or not, without Failing.
|
||||||
|
@ -198,6 +198,9 @@ func TestNotNil(t *testing.T) {
|
|||||||
if NotNil(mockT, nil) {
|
if NotNil(mockT, nil) {
|
||||||
t.Error("NotNil should return false: object is nil")
|
t.Error("NotNil should return false: object is nil")
|
||||||
}
|
}
|
||||||
|
if NotNil(mockT, (*struct{})(nil)) {
|
||||||
|
t.Error("NotNil should return false: object is (*struct{})(nil)")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,6 +211,9 @@ func TestNil(t *testing.T) {
|
|||||||
if !Nil(mockT, nil) {
|
if !Nil(mockT, nil) {
|
||||||
t.Error("Nil should return true: object is nil")
|
t.Error("Nil should return true: object is nil")
|
||||||
}
|
}
|
||||||
|
if !Nil(mockT, (*struct{})(nil)) {
|
||||||
|
t.Error("Nil should return true: object is (*struct{})(nil)")
|
||||||
|
}
|
||||||
if Nil(mockT, new(AssertionTesterConformingObject)) {
|
if Nil(mockT, new(AssertionTesterConformingObject)) {
|
||||||
t.Error("Nil should return false: object is not nil")
|
t.Error("Nil should return false: object is not nil")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user