From 190aad5060656f4c8a80c51fba0874cea11e3556 Mon Sep 17 00:00:00 2001 From: Matt Corby Date: Sun, 21 Jan 2018 20:48:38 +0000 Subject: [PATCH] has length panicked with nil actual --- has/haslength.go | 3 +++ matcher_test.go | 1 + 2 files changed, 4 insertions(+) diff --git a/has/haslength.go b/has/haslength.go index 1a33c55..9c65ee7 100644 --- a/has/haslength.go +++ b/has/haslength.go @@ -14,6 +14,9 @@ func Length(expected interface{}) *gocrest.Matcher { matcher := new(gocrest.Matcher) matcher.Describe = fmt.Sprintf(description, expected) matcher.Matches = func(actual interface{}) bool { + if actual == nil { + return false + } lenOfActual := reflect.ValueOf(actual).Len() matcher.Actual = fmt.Sprintf("length was %d", lenOfActual) typeMatcher, ok := expected.(*gocrest.Matcher) diff --git a/matcher_test.go b/matcher_test.go index fc8c667..1171b0b 100644 --- a/matcher_test.go +++ b/matcher_test.go @@ -21,6 +21,7 @@ func TestHasLengthMatchesOrNot(testing *testing.T) { expected interface{} shouldFail bool }{ + {actual: nil, expected: nil, shouldFail: true}, {actual: "", expected: 0, shouldFail: false}, {actual: "a", expected: 1, shouldFail: false}, {actual: "1", expected: 1, shouldFail: false},