diff --git a/assert/assertions.go b/assert/assertions.go index 30742a0bb..c91fcbfc0 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -721,7 +721,11 @@ func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...inte func includeElement(list interface{}, element interface{}) (ok, found bool) { listValue := reflect.ValueOf(list) - listKind := reflect.TypeOf(list).Kind() + listType := reflect.TypeOf(list) + if listType == nil { + return false, false + } + listKind := listType.Kind() defer func() { if e := recover(); e != nil { ok = false diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 4fafb512d..c6fbb7679 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -592,6 +592,7 @@ func TestContainsNotContains(t *testing.T) { {"j", "k"}, } simpleMap := map[interface{}]interface{}{"Foo": "Bar"} + var zeroMap map[interface{}]interface{} cases := []struct { expected interface{} @@ -606,6 +607,7 @@ func TestContainsNotContains(t *testing.T) { {complexList, &A{"g", "e"}, false}, {simpleMap, "Foo", true}, {simpleMap, "Bar", false}, + {zeroMap, "Bar", false}, } for _, c := range cases { @@ -652,6 +654,22 @@ func TestContainsFailMessage(t *testing.T) { } } +func TestContainsNotContainsOnNilValue(t *testing.T) { + mockT := new(mockTestingT) + + Contains(mockT, nil, "key") + expectedFail := " could not be applied builtin len()" + actualFail := mockT.errorString() + if !strings.Contains(actualFail, expectedFail) { + t.Errorf("Contains failure should include %q but was %q", expectedFail, actualFail) + } + + NotContains(mockT, nil, "key") + if !strings.Contains(actualFail, expectedFail) { + t.Errorf("Contains failure should include %q but was %q", expectedFail, actualFail) + } +} + func TestSubsetNotSubset(t *testing.T) { // MTestCase adds a custom message to the case