Skip to content

Commit 9f0ad86

Browse files
Merge pull request #1517 from Lucaber/equalExportedValues-ptr
Support Pointer to Struct in EqualExportedValues
2 parents 7c847e2 + 74a35d5 commit 9f0ad86

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

assert/assertions.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,19 @@ func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs ..
596596
return Fail(t, fmt.Sprintf("Types expected to match exactly\n\t%v != %v", aType, bType), msgAndArgs...)
597597
}
598598

599+
if aType.Kind() == reflect.Ptr {
600+
aType = aType.Elem()
601+
}
602+
if bType.Kind() == reflect.Ptr {
603+
bType = bType.Elem()
604+
}
605+
599606
if aType.Kind() != reflect.Struct {
600-
return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", aType.Kind(), reflect.Struct), msgAndArgs...)
607+
return Fail(t, fmt.Sprintf("Types expected to both be struct or pointer to struct \n\t%v != %v", aType.Kind(), reflect.Struct), msgAndArgs...)
601608
}
602609

603610
if bType.Kind() != reflect.Struct {
604-
return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", bType.Kind(), reflect.Struct), msgAndArgs...)
611+
return Fail(t, fmt.Sprintf("Types expected to both be struct or pointer to struct \n\t%v != %v", bType.Kind(), reflect.Struct), msgAndArgs...)
605612
}
606613

607614
expected = copyExportedFields(expected)

assert/assertions_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,25 @@ func TestEqualExportedValues(t *testing.T) {
430430
value2: S{[2]int{1, 2}, Nested{2, nil}, nil, Nested{}},
431431
expectedEqual: true,
432432
},
433+
{
434+
value1: &S{1, Nested{2, 3}, 4, Nested{5, 6}},
435+
value2: &S{1, Nested{2, nil}, nil, Nested{}},
436+
expectedEqual: true,
437+
},
438+
{
439+
value1: &S{1, Nested{2, 3}, 4, Nested{5, 6}},
440+
value2: &S{1, Nested{1, nil}, nil, Nested{}},
441+
expectedEqual: false,
442+
expectedFail: `
443+
Diff:
444+
--- Expected
445+
+++ Actual
446+
@@ -3,3 +3,3 @@
447+
Exported2: (assert.Nested) {
448+
- Exported: (int) 2,
449+
+ Exported: (int) 1,
450+
notExported: (interface {}) <nil>`,
451+
},
433452
}
434453

435454
for _, c := range cases {

0 commit comments

Comments
 (0)