From 74a35d55d577770d63ca654da5696f5bd39940e9 Mon Sep 17 00:00:00 2001 From: Luca Berneking Date: Fri, 8 Dec 2023 11:46:38 +0100 Subject: [PATCH] Support Pointer to Struct in EqualExportedValues This PRs allows to `EqualExportedValues` pointers to structs. --- assert/assertions.go | 11 +++++++++-- assert/assertions_test.go | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index bc15101b0..51226340e 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -578,12 +578,19 @@ func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs .. return Fail(t, fmt.Sprintf("Types expected to match exactly\n\t%v != %v", aType, bType), msgAndArgs...) } + if aType.Kind() == reflect.Ptr { + aType = aType.Elem() + } + if bType.Kind() == reflect.Ptr { + bType = bType.Elem() + } + if aType.Kind() != reflect.Struct { - return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", aType.Kind(), reflect.Struct), msgAndArgs...) + return Fail(t, fmt.Sprintf("Types expected to both be struct or pointer to struct \n\t%v != %v", aType.Kind(), reflect.Struct), msgAndArgs...) } if bType.Kind() != reflect.Struct { - return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", bType.Kind(), reflect.Struct), msgAndArgs...) + return Fail(t, fmt.Sprintf("Types expected to both be struct or pointer to struct \n\t%v != %v", bType.Kind(), reflect.Struct), msgAndArgs...) } expected = copyExportedFields(expected) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index d2a25c245..4757d2e57 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -413,6 +413,25 @@ func TestEqualExportedValues(t *testing.T) { value2: S{[2]int{1, 2}, Nested{2, nil}, nil, Nested{}}, expectedEqual: true, }, + { + value1: &S{1, Nested{2, 3}, 4, Nested{5, 6}}, + value2: &S{1, Nested{2, nil}, nil, Nested{}}, + expectedEqual: true, + }, + { + value1: &S{1, Nested{2, 3}, 4, Nested{5, 6}}, + value2: &S{1, Nested{1, nil}, nil, Nested{}}, + expectedEqual: false, + expectedFail: ` + Diff: + --- Expected + +++ Actual + @@ -3,3 +3,3 @@ + Exported2: (assert.Nested) { + - Exported: (int) 2, + + Exported: (int) 1, + notExported: (interface {}) `, + }, } for _, c := range cases {