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

Compare structs with unsorted slices #806

Open
LawYard opened this issue Aug 20, 2019 · 9 comments
Open

Compare structs with unsorted slices #806

LawYard opened this issue Aug 20, 2019 · 9 comments
Milestone

Comments

@LawYard
Copy link

LawYard commented Aug 20, 2019

Hi!
Are here any assert func to compare two struct contains same slices with the same elements but in differs order?

For example:

type Test struct {
    Names []string
}

c1 := Test{
    Names: []string{ "Joe", "Rick" }
}

c2 := Test{
    Names: []string{ "Rick", "Joe" }
}

I need some func that can compare that two structs ignoring slice elems order in it.

@galecore
Copy link

galecore commented Aug 21, 2019

It would also be amazing if we we could compare 2 json-strings this way - almost like JsonEq, but using ElementsMatch when comparing slices. We could name this new method JsonContentsMatch or something along these lines.

Example usage:

expectedEvent := `{
"participants": ["Joe", "Rick"], 
"event": "Birthday party"
}`

actualEvent := `{
"event": "Birthday party",
"participants": ["Rick", "Joe"]
}`

assert.JsonContentsMatch(t, expectedEvent, actualEvent)

@dharmjit
Copy link

dharmjit commented Feb 7, 2020

Is it in the raodmap?

@glesica
Copy link
Collaborator

glesica commented Feb 7, 2020

We can certainly add it. I'm going to spruce up the milestones this morning, I'll slot this in.

@alexrudd
Copy link

Hey @glesica just checking if this got added to the milestones? It would be an incredibly useful feature

@christopher-taormina-zocdoc

I just ran into this with a slice nested down inside some structs. I'm using assert.EqualValues on the structs and the assertion starts to fail when the slice order differs, which gets annoying in my case that conditionally adds values to the slice in question.

@boyan-soubachov boyan-soubachov added this to the v1.7.0 milestone Jul 27, 2020
@ShitaoFu
Copy link

i have the same problem。assert.EqualValues use reflect.DeepEqual , it's hard to do what i want

@mchlp
Copy link
Contributor

mchlp commented Nov 28, 2022

This use case seems to be addressed by the assertions.ElementsMatch method.

image

https://pkg.go.dev/github.com/stretchr/testify/assert?utm_source=godoc#ElementsMatch

@lordzsolt
Copy link

@mchlp Yes and no...

It does solve it in the case the OP presented.


Now imagine you have struct, with 20+ fields. And one of those fields is a slice.
You cannot write:

assert.Equal(t, expectedResponse, receivedResponse)

But you have write:

assert.Equal(t, expectedResponse.field1, receivedResponse.field1)
assert.Equal(t, expectedResponse.field2, receivedResponse.field2)
...
assert.ElementsMatch(t, expectedResponse.field20, receivedResponse.field20)

And this is only the simple case without any nesting....

If I have to write this all out by hand, why am I even using an assertion library?

@brackendawson
Copy link
Collaborator

Potentially could be addressed by: #843 (comment)

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

No branches or pull requests