Skip to content

Commit

Permalink
has length panicked with nil actual
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcorbyeaglen committed Jan 21, 2018
1 parent 73c35b3 commit 190aad5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions has/haslength.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 190aad5

Please sign in to comment.