Skip to content

Commit

Permalink
fix potential nil-pointer dereference
Browse files Browse the repository at this point in the history
the fix gracefully fails the assertion instead of panicking unresolved.
  • Loading branch information
mvrahden authored and boyan-soubachov committed Aug 24, 2021
1 parent e209ca8 commit 5c61ef9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c61ef9

Please sign in to comment.