Skip to content

Commit b9167da

Browse files
Merge pull request #1792 from egawata/assert_same_message_with_type
add type to error message of assert.Same
2 parents f03d037 + 392c0bc commit b9167da

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

assert/assertions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) b
544544
if !same {
545545
// both are pointers but not the same type & pointing to the same address
546546
return Fail(t, fmt.Sprintf("Not same: \n"+
547-
"expected: %p %#[1]v\n"+
548-
"actual : %p %#[2]v",
547+
"expected: %#[1]v (%[1]T)(%[1]p)\n"+
548+
"actual : %#[2]v (%[2]T)(%[2]p)",
549549
expected, actual), msgAndArgs...)
550550
}
551551

assert/assertions_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ func ptr(i int) *int {
635635
func TestSame(t *testing.T) {
636636
t.Parallel()
637637

638-
mockT := new(testing.T)
638+
mockT := new(mockTestingT)
639639

640640
if Same(mockT, ptr(1), ptr(1)) {
641641
t.Error("Same should return false")
@@ -650,6 +650,22 @@ func TestSame(t *testing.T) {
650650
if !Same(mockT, p, p) {
651651
t.Error("Same should return true")
652652
}
653+
654+
t.Run("same object, different type", func(t *testing.T) {
655+
type s struct {
656+
i int
657+
}
658+
type sPtr *s
659+
ps := &s{1}
660+
dps := sPtr(ps)
661+
if Same(mockT, dps, ps) {
662+
t.Error("Same should return false")
663+
}
664+
expPat :=
665+
`expected: &assert.s\{i:1\} \(assert.sPtr\)\((0x[a-f0-9]+)\)\s*\n` +
666+
`\s+actual : &assert.s\{i:1\} \(\*assert.s\)\((0x[a-f0-9]+)\)`
667+
Regexp(t, regexp.MustCompile(expPat), mockT.errorString())
668+
})
653669
}
654670

655671
func TestNotSame(t *testing.T) {

0 commit comments

Comments
 (0)