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.Same() for map #1691

Open
tturbs opened this issue Jan 3, 2025 · 5 comments
Open

assert.Same() for map #1691

tturbs opened this issue Jan 3, 2025 · 5 comments

Comments

@tturbs
Copy link

tturbs commented Jan 3, 2025

Hello everyone,

I’d like to ask a quick question informally.
118fb83#diff-cfb0727d3ab618e656775b3bb99089481b38a25f8ece2b10913e2d88e492a165R531

I have some concerns regarding this commit. While the changes to the assert.Same() function seem appropriate for pointer comparisons, they are not suitable for comparing maps. In my case, it has become impossible to compare a map with its cloned copy.

If this change is considered appropriate, I believe there should be a separate function specifically for comparing pointer-like objects.

Thanks in advance.

@william-snyders-appfolio

This is also an issue for me. We have many tests that are verifying cloning of maps, and those tests now fail with this error. This feels like a breaking change.

@william-snyders-appfolio
Copy link

william-snyders-appfolio commented Jan 8, 2025

After testing things out, it seems like NotSame was not testing what we thought it was. (The below is using the previous version v1.9.0)

func TestSameMap(t *testing.T) {
	a := map[string]string{"z": "z"}
	b := map[string]string{"z": "z"}

  // succeeds
	assert.Equal(t, a, b)

	// Both of these succeed
	assert.NotSame(t, a, a)
	assert.NotSame(t, a, b)

	// Both of these fail
	assert.Same(t, a, a)
	assert.Same(t, a, b)
}

Interestingly, assert.Same(t, a, a) fails with the following error


        	Error:      Not same:
        	           	expected: 0x14000108f30 map[string]string{"z":"z"}
        	           	actual  : 0x14000108f30 map[string]string{"z":"z"}

I would hope this function would work with maps though.

@atiixx
Copy link

atiixx commented Jan 10, 2025

Same is for comparing pointers. If you copy a map it is not pointing to the same memory address and thats why it is NotSame.

You can use Equal to compare maps.

func TestMaps(t *testing.T) {
	a := map[string]string{"z": "z"}
	b := a
	c := map[string]string{"y": "y"}

	fmt.Printf("Address of a: %p\n", &a)
	fmt.Printf("Address of b: %p\n", &b)

	assert.Equal(t, a, b, "They are Equal")
	assert.NotEqual(t, a, c, "They are not Equal")
	assert.NotSame(t, &a, &b)
}
Address of a: 0xc000062090
Address of b: 0xc000062098
PASS

@tturbs
Copy link
Author

tturbs commented Jan 13, 2025

@atiixx

I want to copy a map to ensure that modifying the copied map does not affect the original one. Therefore, I need to verify that the original and the copied maps are not the same, even if Equal() == true. What would be the best way to achieve this?

@atiixx
Copy link

atiixx commented Jan 13, 2025

Hey @tturbs , you could try the DeepEqual method and check the result:

func TestMaps(t *testing.T) {
	a := map[string]string{"z": "z"}
	b := a
	eq := reflect.DeepEqual(a, b)
	assert.Equal(t, eq, true)
}

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

No branches or pull requests

3 participants