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

hamcrest.contains_inanyorder is order sensitive #118

Open
robertwb opened this issue Oct 17, 2019 · 5 comments
Open

hamcrest.contains_inanyorder is order sensitive #118

robertwb opened this issue Oct 17, 2019 · 5 comments

Comments

@robertwb
Copy link

robertwb commented Oct 17, 2019

E.g. assert_that([1, 2], contains_inanyorder(anything(), 1)) fails.

@brunns
Copy link
Member

brunns commented Oct 21, 2019

Hmmmmm. I can see what's going on here. It's not that contains_inanyorder is order sensitive, it's that overgeneral matchers can consume items from the actuals, leaving more specific matchers to miss.

In your example, the anything() matches the 1 from the actual list. The contains_inanyorder matcher pairs them off, drops them, then moves on, leaving the 2 and the 1 from the matcher list, which of course don't match.

I'm not sure how I'd approach fixing this. Any ideas, @robertwb?

@brunns
Copy link
Member

brunns commented Oct 21, 2019

In the meantime, the matcher is working as documented, and you can support your use case by specifying the more specific matchers first, then the less specific.

@robertwb
Copy link
Author

Yes, in this toy example it's clear to see what the order could be, but that's not always the case.

This could be solved generally with https://en.wikipedia.org/wiki/Maximum_cardinality_matching

@brunns
Copy link
Member

brunns commented Oct 22, 2019

Interesting, but I'm not sure I think the complexity is worthwhile. How often is this an issue? If we implemented a maximum cardinality matching algorithm, how would that work with consume-once actuals, such as generators?

In any case, I won't get to look at it until #117 is in.

@robertwb
Copy link
Author

I've run into it a couple of times, the latest because the set of matchers is derived from a container with non-deterministic ordering. For consume-once actuals, one would either run it against all matchers while iterating (rather than just until one finds a match) or reify it into a list.

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

2 participants