Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert equal pointers #1076

Open
rthier opened this issue May 18, 2021 · 4 comments · May be fixed by #1296
Open

Assert equal pointers #1076

rthier opened this issue May 18, 2021 · 4 comments · May be fixed by #1296

Comments

@rthier
Copy link

rthier commented May 18, 2021

I try to test some properties of zap log and I got this:

assert.Same(t, zapcore.ISO8601TimeEncoder, got.EncoderConfig.EncodeTime)

       Error:      Not same:
        	       expected: 0x6bb840 (func(time.Time, zapcore.PrimitiveArrayEncoder))(0x6bb840)
        	       actual  : 0x6bb840 (zapcore.TimeEncoder)(0x6bb840)

In this test the object types are different, but the pointer (address) are same.
Internally the function Same call function samePointers, but this function compare two object pointers and not if the address of each object are equal.

func samePointers(first, second interface{}) bool {

For me its ok this behavior, but I propose a new assert, something like assert.SameAddress(t TestingT, referenceObject, actualObject interface{})

This new assert just check if de uintptr is the same value of referenceObject and actualObject using the function reflect.ValueOf(i interface{}).Pointer()

@ypotest
Copy link

ypotest commented Jan 9, 2022

go-testdeep allows this: https://go.dev/play/p/6QAY0tfAOoQ

@AlexanderYastrebov
Copy link

https://pkg.go.dev/reflect#Value.Pointer says

It panics if v's Kind is not Chan, Func, Map, Pointer, Slice, or UnsafePointer.

Also assert.SameAddress adds new exported API.

An improvement without adding new API might be to change samePointers to use Value.Pointer for Chan, Func, Map, Pointer, Slice, or UnsafePointer and == for others like before.

AlexanderYastrebov added a commit to AlexanderYastrebov/testify that referenced this issue Nov 8, 2022
AlexanderYastrebov added a commit to AlexanderYastrebov/testify that referenced this issue Nov 8, 2022
@AlexanderYastrebov
Copy link

I've created #1296

AlexanderYastrebov added a commit to AlexanderYastrebov/testify that referenced this issue Nov 8, 2022
@AlexanderYastrebov
Copy link

OTOH it works as documented

// Same asserts that two pointers reference the same object.
//
//    assert.Same(t, ptr1, ptr2)
//
// Both arguments must be pointer variables. Pointer variable sameness is
// determined based on the equality of both type and value.

and users may always do

assert.Equal(t, reflect.ValueOf(expected).Pointer(), reflect.ValueOf(actual).Pointer())

https://go.dev/play/p/6P1wnqKl4TV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants