Skip to content

assert.ObjectsAreEqual: improve list/map support#1058

Open
harryzcy wants to merge 12 commits into
stretchr:masterfrom
harryzcy:issue1057
Open

assert.ObjectsAreEqual: improve list/map support#1058
harryzcy wants to merge 12 commits into
stretchr:masterfrom
harryzcy:issue1057

Conversation

@harryzcy

@harryzcy harryzcy commented Mar 1, 2021

Copy link
Copy Markdown

Summary

Function ObjectsAreEqual now compares two objects of type array/slice/map by iterating over each element. When two objects contain the same value but have different types, the function will now return true.

Changes

  • The function ObjectsAreEqualValues in assertion.go is changed.
  • Two helper functions listsAreEqualValues and mapsAreEqualValues are added.
  • Tests are added accordingly.

Motivation

Currently, assert.EqualValues would fail when two objects are of type array/slice/map and contain values of different types.

// assert would fail
assert.EqualValues(t, []interface{}{1}, []interface{}{int64(1)})

But, assert.EqualValues should always compare objects by value. So the above code should idealy pass.

Related issues

Closes #1057

Function `ObjectsAreEqual` now compares two objects of type array/slice/map by iterating over each element. When two objects contain the same value but have different types, the function will now return `true`.
@dolmen dolmen added pkg-assert Change related to package testify/assert assert.EqualValues About equality labels Oct 31, 2023
@joselitofilho

Copy link
Copy Markdown

Hi @harryzcy,

Thanks for your fix. Do you have any idea when this fix will be released?

@harryzcy

harryzcy commented Mar 4, 2024

Copy link
Copy Markdown
Author

Hi @harryzcy,

Thanks for your fix. Do you have any idea when this fix will be released?

No, I don't know when maintainers will review them.

@brackendawson brackendawson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a similar change to #1586 and I think it can be a welcome one.

Currently the doc for EqualValues says:

EqualValues asserts that two objects are equal or convertible to the larger type and equal.

That would need to change because this behavior is quite different. Probably a second sentence describing the behavior for lists and maps. Though I've often been hesitant to have a function with different behavior based on the value of its arguments. Would it be better to have a new assertion for this? Genuine question as I'm undecided.

Apologies for the delay in this review. I advise you read my last comment first.

Comment thread assert/assertions.go Outdated
Comment thread assert/assertions.go Outdated
Comment thread assert/assertions.go Outdated
Comment thread assert/assertions.go Outdated
Comment thread assert/assertions.go Outdated
@dolmen dolmen changed the title Improve list/map support for ObjectsAreEqual ObjectsAreEqual: improve list/map support May 30, 2025
@dolmen dolmen changed the title ObjectsAreEqual: improve list/map support assert.ObjectsAreEqual: improve list/map support May 12, 2026
@harryzcy

harryzcy commented May 25, 2026

Copy link
Copy Markdown
Author

I am very late to addressing those comments. Apologies.

I have addresses those comments, and the PR is ready for review again.

Currently the doc for EqualValues says:

EqualValues asserts that two objects are equal or convertible to the larger type and equal.

That would need to change because this behavior is quite different. Probably a second sentence describing the behavior for lists and maps. Though I've often been hesitant to have a function with different behavior based on the value of its arguments. Would it be better to have a new assertion for this? Genuine question as I'm undecided.

I'm also not very sure on this. It does seems like slightly different behavior for different types are confusing. Would like to hear what other people think? If more people are leaning toward having a new assertion, I'm happy to do that instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assert.EqualValues About equality bug must-rebase pkg-assert Change related to package testify/assert

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Testify assert.EqualValues should equate nested []interface{} by values

4 participants