Skip to content

Commit 82a2b83

Browse files
Use reflect.Value.Pointer() to compare pointers
Fixes #1076
1 parent 1333b5d commit 82a2b83

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

assert/assertions.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,17 +410,16 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}
410410
// they point to the same object
411411
func samePointers(first, second interface{}) bool {
412412
firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second)
413-
if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr {
413+
if firstPtr.Kind() != secondPtr.Kind() {
414414
return false
415415
}
416416

417-
firstType, secondType := reflect.TypeOf(first), reflect.TypeOf(second)
418-
if firstType != secondType {
417+
switch firstPtr.Kind() {
418+
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice:
419+
return firstPtr.Pointer() == secondPtr.Pointer()
420+
default:
419421
return false
420422
}
421-
422-
// compare pointer addresses
423-
return first == second
424423
}
425424

426425
// formatUnequalValues takes two values of arbitrary types and returns string

assert/assertions_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ func TestNotSame(t *testing.T) {
262262

263263
func Test_samePointers(t *testing.T) {
264264
p := ptr(2)
265+
c1, c2 := make(chan int), make(chan int)
266+
f1, f2 := func() {}, func() {}
267+
m1, m2 := map[int]int{1: 2}, map[int]int{1: 2}
268+
p1, p2 := ptr(3), ptr(3)
269+
s1, s2 := []int{4, 5}, []int{4, 5}
265270

266271
type args struct {
267272
first interface{}
@@ -297,6 +302,56 @@ func Test_samePointers(t *testing.T) {
297302
args: args{first: [2]int{1, 2}, second: []int{1, 2}},
298303
assertion: False,
299304
},
305+
{
306+
name: "chan(1) == chan(1)",
307+
args: args{first: c1, second: c1},
308+
assertion: True,
309+
},
310+
{
311+
name: "func(1) == func(1)",
312+
args: args{first: f1, second: f1},
313+
assertion: True,
314+
},
315+
{
316+
name: "map(1) == map(1)",
317+
args: args{first: m1, second: m1},
318+
assertion: True,
319+
},
320+
{
321+
name: "ptr(1) == ptr(1)",
322+
args: args{first: p1, second: p1},
323+
assertion: True,
324+
},
325+
{
326+
name: "slice(1) == slice(1)",
327+
args: args{first: s1, second: s1},
328+
assertion: True,
329+
},
330+
{
331+
name: "chan(1) != chan(2)",
332+
args: args{first: c1, second: c2},
333+
assertion: False,
334+
},
335+
{
336+
name: "func(1) != func(2)",
337+
args: args{first: f1, second: f2},
338+
assertion: False,
339+
},
340+
{
341+
name: "map(1) != map(2)",
342+
args: args{first: m1, second: m2},
343+
assertion: False,
344+
},
345+
{
346+
name: "ptr(1) != ptr(2)",
347+
args: args{first: p1, second: p2},
348+
assertion: False,
349+
},
350+
{
351+
name: "slice(1) != slice(2)",
352+
args: args{first: s1, second: s2},
353+
assertion: False,
354+
},
300355
}
301356
for _, tt := range tests {
302357
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)