From f236a3b683bfd9bd44436578d4c7d2b32ffa7bce Mon Sep 17 00:00:00 2001 From: Zachary Becker Date: Mon, 25 Sep 2023 06:50:34 -0500 Subject: [PATCH 1/2] 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 719164e7e..74f172807 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -109,7 +109,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 40e372015..472029c66 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -402,6 +402,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 { From d3479b10680808736a803b6642c0e0ede362ff02 Mon Sep 17 00:00:00 2001 From: Zachary Becker Date: Sun, 15 Oct 2023 17:45:52 -0500 Subject: [PATCH 2/2] Combine switch cases, difference wrapped in if statement --- assert/assertions.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 4d669d734..1e55fbf54 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -114,20 +114,13 @@ func copyExportedFields(expected interface{}) interface{} { result.Elem().Set(reflect.ValueOf(unexportedRemoved)) return result.Interface() - 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)) + case reflect.Array, reflect.Slice: + var result reflect.Value + if expectedKind == reflect.Array { + result = reflect.New(reflect.ArrayOf(expectedValue.Len(), expectedType.Elem())).Elem() + } else { + result = reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len()) } - 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) if isNil(index) {