Fix bug where array is treated as slice in EqualExportedValues

pull/1385/head
Zachary Becker 2023-09-25 06:50:34 -05:00 committed by Olivier Mengué
parent ce5c2b684b
commit 2f7efa2451
2 changed files with 18 additions and 1 deletions

View File

@ -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)

View File

@ -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 {