Merge pull request #220 from bmatsuo/fix-notnil-for-typed-nil-values

Fix NotNil assertion for typed nil values
pull/222/head
Ernesto Jiménez 2015-09-24 18:06:42 +01:00
commit bcd32dcc8b
2 changed files with 10 additions and 18 deletions

View File

@ -287,24 +287,10 @@ func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}
//
// Returns whether the assertion was successful (true) or not (false).
func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
success := 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
}
if !isNil(object) {
return true
}
if !success {
Fail(t, "Expected value not to be nil.", msgAndArgs...)
}
return success
return Fail(t, "Expected value not to be nil.", msgAndArgs...)
}
// isNil checks if a specified object is nil or not, without Failing.

View File

@ -11,7 +11,7 @@ import (
)
var (
i interface{}
i interface{}
zeros = []interface{}{
false,
byte(0),
@ -198,6 +198,9 @@ func TestNotNil(t *testing.T) {
if NotNil(mockT, 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) {
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)) {
t.Error("Nil should return false: object is not nil")
}