Skip to content

Commit

Permalink
Fix bug where array is treated as slice in EqualExportedValues
Browse files Browse the repository at this point in the history
  • Loading branch information
zrbecker authored and dolmen committed Oct 15, 2023
1 parent ce5c2b6 commit 2f7efa2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,19 @@ func copyExportedFields(expected interface{}) interface{} {
result.Elem().Set(reflect.ValueOf(unexportedRemoved))
return result.Interface()

case reflect.Array, reflect.Slice:
case reflect.Array:
result := reflect.New(reflect.ArrayOf(expectedValue.Len(), expectedType.Elem())).Elem()
for i := 0; i < expectedValue.Len(); i++ {
index := expectedValue.Index(i)
if isNil(index) {
continue
}
unexportedRemoved := copyExportedFields(index.Interface())
result.Index(i).Set(reflect.ValueOf(unexportedRemoved))
}
return result.Interface()

case reflect.Slice:
result := reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len())
for i := 0; i < expectedValue.Len(); i++ {
index := expectedValue.Index(i)
Expand Down
5 changes: 5 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ func TestEqualExportedValues(t *testing.T) {
+ Exported: (int) 2,
notExported: (interface {}) <nil>`,
},
{
value1: S{[2]int{1, 2}, Nested{2, 3}, 4, Nested{5, 6}},
value2: S{[2]int{1, 2}, Nested{2, nil}, nil, Nested{}},
expectedEqual: true,
},
}

for _, c := range cases {
Expand Down

0 comments on commit 2f7efa2

Please sign in to comment.