mirror of https://github.com/stretchr/testify.git
fix potential nil-pointer dereference
the fix gracefully fails the assertion instead of panicking unresolved.pull/1024/merge
parent
e209ca88af
commit
5c61ef97ae
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue