Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1083 (Nil-pointer dereference) #1084

Merged
merged 5 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions assert/assertion_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestGreater(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Greater(out, currCase.less, currCase.greater))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Greater")
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestGreaterOrEqual(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.GreaterOrEqual")
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestLess(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Less(out, currCase.greater, currCase.less))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Less")
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestLessOrEqual(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, LessOrEqual(out, currCase.greater, currCase.less))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.LessOrEqual")
}
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestPositive(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Positive(out, currCase.e))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Positive")
}
}
Expand Down Expand Up @@ -351,7 +351,7 @@ func TestNegative(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Negative(out, currCase.e))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Negative")
}
}
Expand Down Expand Up @@ -386,7 +386,7 @@ func Test_compareTwoValuesNotComparableValues(t *testing.T) {
}{
{v1: CompareStruct{}, v2: CompareStruct{}},
{v1: map[string]int{}, v2: map[string]int{}},
{v1: make([]int, 5, 5), v2: make([]int, 5, 5)},
{v1: make([]int, 5), v2: make([]int, 5)},
} {
compareResult := compareTwoValues(mockT, currCase.v1, currCase.v2, []CompareType{compareLess, compareEqual, compareGreater}, "testFailMessage")
False(t, compareResult)
Expand Down
8 changes: 4 additions & 4 deletions assert/assertion_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestIsIncreasing(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, IsIncreasing(out, currCase.collection))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func TestIsNonIncreasing(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, IsNonIncreasing(out, currCase.collection))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func TestIsDecreasing(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, IsDecreasing(out, currCase.collection))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
}
}

Expand Down Expand Up @@ -181,6 +181,6 @@ func TestIsNonDecreasing(t *testing.T) {
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, IsNonDecreasing(out, currCase.collection))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.buf.String(), currCase.msg)
}
}
26 changes: 15 additions & 11 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,14 @@ func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...inte
// return (false, false) if impossible.
// return (true, false) if element was not found.
// return (true, true) if element was found.
func includeElement(list interface{}, element interface{}) (ok, found bool) {
func containsElement(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 Expand Up @@ -764,7 +768,7 @@ func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bo
h.Helper()
}

ok, found := includeElement(s, contains)
ok, found := containsElement(s, contains)
if !ok {
return Fail(t, fmt.Sprintf("%#v could not be applied builtin len()", s), msgAndArgs...)
}
Expand All @@ -787,7 +791,7 @@ func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{})
h.Helper()
}

ok, found := includeElement(s, contains)
ok, found := containsElement(s, contains)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", s), msgAndArgs...)
}
Expand Down Expand Up @@ -831,7 +835,7 @@ func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok

for i := 0; i < subsetValue.Len(); i++ {
element := subsetValue.Index(i).Interface()
ok, found := includeElement(list, element)
ok, found := containsElement(list, element)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
}
Expand All @@ -852,7 +856,7 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
h.Helper()
}
if subset == nil {
return Fail(t, fmt.Sprintf("nil is the empty set which is a subset of every set"), msgAndArgs...)
return Fail(t, "nil is the empty set which is a subset of every set", msgAndArgs...)
}

subsetValue := reflect.ValueOf(subset)
Expand All @@ -875,7 +879,7 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})

for i := 0; i < subsetValue.Len(); i++ {
element := subsetValue.Index(i).Interface()
ok, found := includeElement(list, element)
ok, found := containsElement(list, element)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
}
Expand Down Expand Up @@ -1161,15 +1165,15 @@ func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs
bf, bok := toFloat(actual)

if !aok || !bok {
return Fail(t, fmt.Sprintf("Parameters must be numerical"), msgAndArgs...)
return Fail(t, "Parameters must be numerical", msgAndArgs...)
}

if math.IsNaN(af) && math.IsNaN(bf) {
return true
}

if math.IsNaN(af) {
return Fail(t, fmt.Sprintf("Expected must not be NaN"), msgAndArgs...)
return Fail(t, "Expected must not be NaN", msgAndArgs...)
}

if math.IsNaN(bf) {
Expand All @@ -1192,7 +1196,7 @@ func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAn
if expected == nil || actual == nil ||
reflect.TypeOf(actual).Kind() != reflect.Slice ||
reflect.TypeOf(expected).Kind() != reflect.Slice {
return Fail(t, fmt.Sprintf("Parameters must be slice"), msgAndArgs...)
return Fail(t, "Parameters must be slice", msgAndArgs...)
}

actualSlice := reflect.ValueOf(actual)
Expand Down Expand Up @@ -1302,7 +1306,7 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m
if expected == nil || actual == nil ||
reflect.TypeOf(actual).Kind() != reflect.Slice ||
reflect.TypeOf(expected).Kind() != reflect.Slice {
return Fail(t, fmt.Sprintf("Parameters must be slice"), msgAndArgs...)
return Fail(t, "Parameters must be slice", msgAndArgs...)
}

actualSlice := reflect.ValueOf(actual)
Expand Down
47 changes: 32 additions & 15 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 Expand Up @@ -714,53 +732,53 @@ func TestNotSubsetNil(t *testing.T) {
}
}

func Test_includeElement(t *testing.T) {
func Test_containsElement(t *testing.T) {

list1 := []string{"Foo", "Bar"}
list2 := []int{1, 2}
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}

ok, found := includeElement("Hello World", "World")
ok, found := containsElement("Hello World", "World")
True(t, ok)
True(t, found)

ok, found = includeElement(list1, "Foo")
ok, found = containsElement(list1, "Foo")
True(t, ok)
True(t, found)

ok, found = includeElement(list1, "Bar")
ok, found = containsElement(list1, "Bar")
True(t, ok)
True(t, found)

ok, found = includeElement(list2, 1)
ok, found = containsElement(list2, 1)
True(t, ok)
True(t, found)

ok, found = includeElement(list2, 2)
ok, found = containsElement(list2, 2)
True(t, ok)
True(t, found)

ok, found = includeElement(list1, "Foo!")
ok, found = containsElement(list1, "Foo!")
True(t, ok)
False(t, found)

ok, found = includeElement(list2, 3)
ok, found = containsElement(list2, 3)
True(t, ok)
False(t, found)

ok, found = includeElement(list2, "1")
ok, found = containsElement(list2, "1")
True(t, ok)
False(t, found)

ok, found = includeElement(simpleMap, "Foo")
ok, found = containsElement(simpleMap, "Foo")
True(t, ok)
True(t, found)

ok, found = includeElement(simpleMap, "Bar")
ok, found = containsElement(simpleMap, "Bar")
True(t, ok)
False(t, found)

ok, found = includeElement(1433, "1")
ok, found = containsElement(1433, "1")
False(t, ok)
False(t, found)
}
Expand Down Expand Up @@ -1557,8 +1575,7 @@ func testAutogeneratedFunction() {
t := struct {
io.Closer
}{}
var c io.Closer
c = t
c := t
c.Close()
}

Expand Down Expand Up @@ -2206,7 +2223,7 @@ func ExampleValueAssertionFunc() {

dumbParse := func(input string) interface{} {
var x interface{}
json.Unmarshal([]byte(input), &x)
_ = json.Unmarshal([]byte(input), &x)
return x
}

Expand Down
2 changes: 1 addition & 1 deletion assert/http_assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestHTTPStatusesWrapper(t *testing.T) {

func httpHelloName(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
_, _ = w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
}

func TestHTTPRequestWithNoParams(t *testing.T) {
Expand Down