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

test whether two slices have equal content #275

Closed
LarsFronius opened this issue Feb 16, 2016 · 7 comments
Closed

test whether two slices have equal content #275

LarsFronius opened this issue Feb 16, 2016 · 7 comments

Comments

@LarsFronius
Copy link

I wonder if this inside the scope of testify.
Basically I have two slices of a certain type, which can be of random order and I want to be able to compare, that any of the data of one slice appears on the other slice as well.
A reflect.deepequal does not get the job done, neither does assert.Equal, so is this something to consider as a feature for testify?
e.g.

type foobar struct {
    Key string,
    Value string
}

var foobar_slice []*foobar
foobar_slice = append(settings_slice, &pushservice.Setting{Key: "foobar", Value: "barbaz"})
foobar_slice = append(settings_slice, &pushservice.Setting{Key: "bazfoo", Value: "foobaz"})

var barbaz_slice []*foobar
barbaz_slice = append(settings_slice, &pushservice.Setting{Key: "bazfoo", Value: "foobaz"})
barbaz_slice = append(settings_slice, &pushservice.Setting{Key: "foobar", Value: "barbaz"})

assert.EqualSlice(t, foobar_slice, barbaz_slice)
@ernesto-jimenez
Copy link
Member

Hi @LarsFronius, assert.EqualSlice is a bit misleading, since two slices with different order are different.

Before finding the right name for the method there would need to be some clarification on the behaviour:

  • Is it just confirming that all events from the actual slice are contained in the expected slice and vice versa?
  • What would happen with duplicated elements? e.g: actual and equal have the same elements but actual has one that is duplicated. Would the assertion fail?

Sounds like you might be thinking about assert.ContainsSameElements.

@ernesto-jimenez
Copy link
Member

I think this would be out of scope.

It's probably better for the developer to sort both arrays and then call assert.Equal

@narg95
Copy link

narg95 commented Feb 17, 2017

assert.ContainsSameElements would be great!
In my case, I have a integration test and I get some data from the database that it may come in different order every time I run it, and I just want to assert that the elements that I expect are there. This function will make my assertion easier.
I know it is closed but +1.

emou added a commit to emou/testify that referenced this issue Aug 22, 2017
This name seemed like it would be easy to find.

Possible alternatives for the name:
- ElementsMatch
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
stretchr#275
but I decided to give it a shot as I needed it also.
emou added a commit to emou/testify that referenced this issue Aug 22, 2017
This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
stretchr#275
but I decided to give it a shot as I needed it also.
emou added a commit to emou/testify that referenced this issue Aug 22, 2017
An assertion that compares the elements of the slices/arrays disregarding the order,
i.e. it checks whether each element in the first array appears the same number of times in it
as in the second array.

This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
stretchr#275
but I decided to give it a shot as I needed it also.
emou added a commit to emou/testify that referenced this issue Aug 22, 2017
An assertion that compares the elements of the slices/arrays disregarding the order,
i.e. it checks whether each element in the first slice/array appears the same number of times in it
as in the second slice/array.

This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
stretchr#275
but I decided to give it a shot as I needed it also.
emou added a commit to emou/testify that referenced this issue Aug 22, 2017
An assertion that compares the elements of the slices/arrays disregarding the order,
i.e. it checks whether each element in the first slice/array appears the same number of times in it
as in the second slice/array.

This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
stretchr#275
but I decided to give it a shot as I needed it also.
emou added a commit to emou/testify that referenced this issue Aug 22, 2017
An assertion that compares the elements of the slices/arrays disregarding the order,
i.e. it checks whether each element in the first slice/array appears the same number of times in it
as in the second slice/array.

This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
stretchr#275
but I decided to give it a shot as I needed it also.
emou added a commit to emou/testify that referenced this issue Aug 22, 2017
An assertion that compares the elements of the slices/arrays disregarding the order,
i.e. it checks whether each element in the first slice/array appears the same number of times in it
as in the second slice/array.

This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
stretchr#275
but I decided to give it a shot as I needed it also.
ernesto-jimenez pushed a commit that referenced this issue Dec 30, 2017
An assertion that compares the elements of the slices/arrays disregarding the order,
i.e. it checks whether each element in the first slice/array appears the same number of times in it
as in the second slice/array.

This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
#275
but I decided to give it a shot as I needed it also.
@DennisMao
Copy link

Now it's the assert.ElementsMatch(t, expectedSlice, actualSlice)

@sarathsp06
Copy link
Contributor

@DennisMao is there a way to do the same in Mock.On

@ilovejs
Copy link

ilovejs commented Aug 15, 2023

...

Expected :[]string{"g1", "f3", "ze"}
Actual   :[]string{"f3", "g1", "ze"}

@fangpinsern
Copy link

@ilovejs if this is an issue with assert.ElementsMatch, i could not reproduce it

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

7 participants