From 392c0bcd569e83015dedee1e6a6fed62ce1c94e0 Mon Sep 17 00:00:00 2001 From: egawata Date: Fri, 12 Sep 2025 14:45:56 +0900 Subject: [PATCH] add type to error message of assert.Same --- assert/assertions.go | 4 ++-- assert/assertions_test.go | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 3f75ee0eb..3d53611c3 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -544,8 +544,8 @@ func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) b if !same { // both are pointers but not the same type & pointing to the same address return Fail(t, fmt.Sprintf("Not same: \n"+ - "expected: %p %#[1]v\n"+ - "actual : %p %#[2]v", + "expected: %#[1]v (%[1]T)(%[1]p)\n"+ + "actual : %#[2]v (%[2]T)(%[2]p)", expected, actual), msgAndArgs...) } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 4db48f38b..0e3536eb1 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -635,7 +635,7 @@ func ptr(i int) *int { func TestSame(t *testing.T) { t.Parallel() - mockT := new(testing.T) + mockT := new(mockTestingT) if Same(mockT, ptr(1), ptr(1)) { t.Error("Same should return false") @@ -650,6 +650,22 @@ func TestSame(t *testing.T) { if !Same(mockT, p, p) { t.Error("Same should return true") } + + t.Run("same object, different type", func(t *testing.T) { + type s struct { + i int + } + type sPtr *s + ps := &s{1} + dps := sPtr(ps) + if Same(mockT, dps, ps) { + t.Error("Same should return false") + } + expPat := + `expected: &assert.s\{i:1\} \(assert.sPtr\)\((0x[a-f0-9]+)\)\s*\n` + + `\s+actual : &assert.s\{i:1\} \(\*assert.s\)\((0x[a-f0-9]+)\)` + Regexp(t, regexp.MustCompile(expPat), mockT.errorString()) + }) } func TestNotSame(t *testing.T) {