fix potential nil-pointer dereference

the fix gracefully fails the assertion instead of panicking unresolved.
pull/1024/merge
Menno 2021-05-30 21:05:06 +02:00 committed by Boyan Soubachov
parent e209ca88af
commit 5c61ef97ae
2 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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 := "<nil> 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