From fb64a0ab08a14b72fcce748f8fd40119e0dac8ec Mon Sep 17 00:00:00 2001 From: Bryan Matsuo Date: Wed, 23 Sep 2015 23:22:23 -0700 Subject: [PATCH] Fix NotNil assertion for typed nil values The NotNil assertion had an error in its handling of typed nil values. This change makes use of the helper function isNil (used by the Nil assertion). The helper function has correct handling of typed nil values and when negated provides the expected semantics for `assert.NotNil(t, x)`. if x == nil { assert.Fail(t, "is nil", x) } --- assert/assertions.go | 20 +++----------------- assert/assertions_test.go | 5 ++++- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 023b1c3..22e3a6b 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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. diff --git a/assert/assertions_test.go b/assert/assertions_test.go index ce4ecae..afc2e41 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -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)") + } }