hardayal / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

add unordered collection equality matcher #163

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
When testing DAOs and data services, the test I most often perform is 
order-independent collection equality*.

This is because the database (via Hibernate) may return objects in an arbitrary 
order.

I wrote a Matcher that matches collections with the same elements regardless of 
order. It handles duplicate elements as well (the number of occurrences must 
match).

* aka bag-wise equality.

Original issue reported on code.google.com by me%jnova...@gtempaccount.com on 17 Oct 2011 at 1:05

GoogleCodeExporter commented 8 years ago
Posting my proof of concept, which I'm tracking here:

https://bitbucket.org/pdurph/hamcrest

Original comment by me%jnova...@gtempaccount.com on 17 Oct 2011 at 1:17

Attachments:

GoogleCodeExporter commented 8 years ago
If I am not mistaken, containsInAnyOrder does what you want.

Note that you must create the list of matchers for containsInAnyOrder as 
List<Matcher<? super Bar>>. If you simply use a List<Matcher<Bar>>
assertThat will complain:

"The method assertThat(T, Matcher<T>) in the type Assert is not applicable for 
the arguments (List<Bar>, Matcher<Iterable<? extends List<Matcher<Bar>>>>)"

Original comment by dietrich...@gmail.com on 26 Oct 2011 at 11:28

GoogleCodeExporter commented 8 years ago
I saw containsInAnyOrder(), but it merely ensures that the target object is a 
superset of the expected list, and would have to be combined with a size 
equality check. I'm looking to do both in 1 Matcher.

Original comment by jno...@adconion.com on 26 Oct 2011 at 1:36

GoogleCodeExporter commented 8 years ago
containsInAnyOrder() does what you need.  It is badly named.

Original comment by smgfree...@gmail.com on 25 Apr 2012 at 11:09

GoogleCodeExporter commented 8 years ago

Original comment by smgfree...@gmail.com on 25 Apr 2012 at 11:10