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

Fix NotNil assertion for typed nil values
This commit is contained in:
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). // 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.

View File

@ -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")
} }