From 2f7efa2451c1a19be1672593eb2ab82640b1aa2c Mon Sep 17 00:00:00 2001 From: Zachary Becker Date: Mon, 25 Sep 2023 06:50:34 -0500 Subject: [PATCH] Fix bug where array is treated as slice in EqualExportedValues --- assert/assertions.go | 14 +++++++++++++- assert/assertions_test.go | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index c276316b4..4d669d734 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index e1245b421..9a44f95eb 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -408,6 +408,11 @@ func TestEqualExportedValues(t *testing.T) { + Exported: (int) 2, notExported: (interface {}) `, }, + { + 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 {