Skip to content
Open
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
33 changes: 33 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,39 @@ func TestEqualFormatting(t *testing.T) {
}
}

func TestEqualFormattingWithPanic(t *testing.T) {
t.Parallel()

type structWithUnexportedMapWithArrayKey struct {
m interface{}
}

for _, c := range []struct {
a interface{}
b interface{}
}{
{
// from the issue https://github.com/stretchr/testify/pull/1816
a: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: nil,
{2}: nil,
},
},
b: structWithUnexportedMapWithArrayKey{},
},
} {

mockT := new(mockTestingT)
NotPanics(t, func() {
Equal(mockT, c.a, c.b)
}, "should not panic")

True(t, mockT.Failed(), "should have failed")
Contains(t, mockT.errorString(), "Not equal:", "error message should mention inequality")
}
}

func TestFormatUnequalValues(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 6 additions & 0 deletions internal/spew/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ func valueSortLess(a, b reflect.Value) bool {
for i := 0; i < l; i++ {
av := a.Index(i)
bv := b.Index(i)

if !av.CanInterface() || !bv.CanInterface() {
// Unexported fields would panic on Interface() call.
continue
}

if av.Interface() == bv.Interface() {
continue
}
Expand Down