Skip to content

Commit 2f7efa2

Browse files
zrbeckerdolmen
authored andcommitted
Fix bug where array is treated as slice in EqualExportedValues
1 parent ce5c2b6 commit 2f7efa2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

assert/assertions.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,19 @@ func copyExportedFields(expected interface{}) interface{} {
114114
result.Elem().Set(reflect.ValueOf(unexportedRemoved))
115115
return result.Interface()
116116

117-
case reflect.Array, reflect.Slice:
117+
case reflect.Array:
118+
result := reflect.New(reflect.ArrayOf(expectedValue.Len(), expectedType.Elem())).Elem()
119+
for i := 0; i < expectedValue.Len(); i++ {
120+
index := expectedValue.Index(i)
121+
if isNil(index) {
122+
continue
123+
}
124+
unexportedRemoved := copyExportedFields(index.Interface())
125+
result.Index(i).Set(reflect.ValueOf(unexportedRemoved))
126+
}
127+
return result.Interface()
128+
129+
case reflect.Slice:
118130
result := reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len())
119131
for i := 0; i < expectedValue.Len(); i++ {
120132
index := expectedValue.Index(i)

assert/assertions_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ func TestEqualExportedValues(t *testing.T) {
408408
+ Exported: (int) 2,
409409
notExported: (interface {}) <nil>`,
410410
},
411+
{
412+
value1: S{[2]int{1, 2}, Nested{2, 3}, 4, Nested{5, 6}},
413+
value2: S{[2]int{1, 2}, Nested{2, nil}, nil, Nested{}},
414+
expectedEqual: true,
415+
},
411416
}
412417

413418
for _, c := range cases {

0 commit comments

Comments
 (0)