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

Use reflect.Value.Pointer() to compare pointers #1296

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AlexanderYastrebov
Copy link

Summary

Use reflect.Value.Pointer() to compare pointers

Changes

Updates samePointers implementation and adds test cases

Motivation

This is needed to check if map, slice and other built-in types point to the same value

Related issues

Fixes #1076

Copy link
Contributor

@stevenh stevenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

assert/assertions.go Outdated Show resolved Hide resolved
assert/assertions.go Outdated Show resolved Hide resolved
@dolmen dolmen added enhancement pkg-assert Change related to package testify/assert internal/refactor Refactor internals with no external visible changes assert.EqualValues About equality Changes Requested labels Oct 16, 2023
@stevenh
Copy link
Contributor

stevenh commented Sep 9, 2024

@dolmen quick ping as @AlexanderYastrebov has updated based on your feedback

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring for Same would need to be changed:

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.
  • This now works for more than pointer variables.
  • This now works for types which are convertable to the same type.
  • It needs to be clear that only slices pointing to the start of the same array are the same, and not slices pointing to different indices of the same array:
var (
	a   [8]int
	sa0 = a[:]
	sa3 = a[3:]
)
assert.Same(t, sa0, sa3)

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a change in behaviour and we must decide if it's safe to do in v1.

No invocation of Same could previously pass with any non-pointer types.

Previously NotSame will always pass if non-pointer type/s are passed it, it can now fail of those reference the same object. In this case I think it would fix a test the author would have intended to fail.

I'd like some more opinions before merging this.

@@ -491,12 +491,12 @@ func validateEqualArgs(expected, actual interface{}) error {
return nil
}

// Same asserts that two pointers reference the same object.
// Same asserts that two arguments reference the same object.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ambiguous for slices in this case:

var (
	a   [8]int
	sa0 = a[:]
	sa3 = a[3:]
)
assert.Same(t, sa0, sa3)

sa0 and sa3 reference the same object if you take object to mean array, but not if you mean contiguous segment of an underlying array. The language specification uses both terms at various points. We should make it clear that Same only considers slices to be the same if they reference the same contiguous segment of an underlying array.

Copy link
Author

@AlexanderYastrebov AlexanderYastrebov Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO reverse is also surprising

var (
	a   [8]int
	sa0 = a[:]
	sa1 = a[:1]
)
assert.Same(t, sa0, sa1) // passes

@AlexanderYastrebov AlexanderYastrebov marked this pull request as draft October 7, 2024 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert.EqualValues About equality enhancement internal/refactor Refactor internals with no external visible changes pkg-assert Change related to package testify/assert
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assert equal pointers
4 participants