-
Notifications
You must be signed in to change notification settings - Fork 30
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
Please add support for containsInAnyOrder(T... items) #37
Comments
This is the same as
You can turn the method reference and a list of expected elements to a Matcher with the expression A factory function that is overloaded for varargs is a one-liner. There are many, many methods that make useful Hamkrest matchers. Far too many to support them all in the library. The |
Is there not a difference if the collection under test contains more elements than the specified expectation? Hence, a custom util Matcher would have to be a bit more complicated than |
As @MalteHartwig points out, unfortunately this passes: assertThat(listOf("foo", "bar"), List<String>::containsAll, emptyList()) It's possible to get closer to Unless, that is, someone can come up with some inspired use of the standard library for doing this. |
you could add an extension function to List:
and use that:
(not fully tested against the hamcrest version) However it would be nice to have this functionality built in. |
@corbym re:
Bear in mind that this doesn't work (at least not in the way I would expect) with some cases of lists with duplicate elements, for example:
I propose this implementation to handle the case where there are duplicates: fun <T> List<T>.containsInAnyOrder(elements: List<T>) =
groupingBy { it }.eachCount() == elements.groupingBy { it }.eachCount() |
I expect it depends on how you interpret the hamcrest version (if, indeed, you care about such parity). The documentation states:
-- http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#containsInAnyOrder(T...) .. by this I would expect to get "true" back for your example Of course I'd have to examine the implementation of the hamcrest version to be sure. However, it's your library so feel free to give whatever recommended solution you feel is right 😊 |
http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#containsInAnyOrder(T...)
assertThat(listOf("foo", "bar"), containsInAnyOrder("bar", "foo"))
The text was updated successfully, but these errors were encountered: