Skip to content

Commit 74a35d5

Browse files
committed
Support Pointer to Struct in EqualExportedValues
This PRs allows to `EqualExportedValues` pointers to structs.
1 parent db8608e commit 74a35d5

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

assert/assertions.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,19 @@ func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs ..
578578
return Fail(t, fmt.Sprintf("Types expected to match exactly\n\t%v != %v", aType, bType), msgAndArgs...)
579579
}
580580

581+
if aType.Kind() == reflect.Ptr {
582+
aType = aType.Elem()
583+
}
584+
if bType.Kind() == reflect.Ptr {
585+
bType = bType.Elem()
586+
}
587+
581588
if aType.Kind() != reflect.Struct {
582-
return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", aType.Kind(), reflect.Struct), msgAndArgs...)
589+
return Fail(t, fmt.Sprintf("Types expected to both be struct or pointer to struct \n\t%v != %v", aType.Kind(), reflect.Struct), msgAndArgs...)
583590
}
584591

585592
if bType.Kind() != reflect.Struct {
586-
return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", bType.Kind(), reflect.Struct), msgAndArgs...)
593+
return Fail(t, fmt.Sprintf("Types expected to both be struct or pointer to struct \n\t%v != %v", bType.Kind(), reflect.Struct), msgAndArgs...)
587594
}
588595

589596
expected = copyExportedFields(expected)

assert/assertions_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,25 @@ func TestEqualExportedValues(t *testing.T) {
413413
value2: S{[2]int{1, 2}, Nested{2, nil}, nil, Nested{}},
414414
expectedEqual: true,
415415
},
416+
{
417+
value1: &S{1, Nested{2, 3}, 4, Nested{5, 6}},
418+
value2: &S{1, Nested{2, nil}, nil, Nested{}},
419+
expectedEqual: true,
420+
},
421+
{
422+
value1: &S{1, Nested{2, 3}, 4, Nested{5, 6}},
423+
value2: &S{1, Nested{1, nil}, nil, Nested{}},
424+
expectedEqual: false,
425+
expectedFail: `
426+
Diff:
427+
--- Expected
428+
+++ Actual
429+
@@ -3,3 +3,3 @@
430+
Exported2: (assert.Nested) {
431+
- Exported: (int) 2,
432+
+ Exported: (int) 1,
433+
notExported: (interface {}) <nil>`,
434+
},
416435
}
417436

418437
for _, c := range cases {

0 commit comments

Comments
 (0)