Add Iterable assertion builder with constraint-based order matching #302
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've come across a few test scenarios where I want to assert ordering on an iterable, but
contains
,containsExactly
, andcontainsExactlyInAnyOrder
don't quite cut it. Two particular cases that come to mind are asserting lifecycle callbacks and asserting connection state. What they have in common is that some aspects of their order matter, but not all.I've generally gone about solving these cases by either mangling the result list before asserting on it (e.g. filtering or reordering elements), or creating a combinatorial explosion of valid results and asserting that the result is one of them. Both leave my tests a lot uglier than I'd like.
This is my take on an assertion builder for iterables that can assert fuzzy ordering rules.
The API is a block in which the user asserts elements in the subject, and constraints for those elements' positions relative to other elements.
Here's an example of its usage, illustrating my most recent use case:
I realize this is a rather opinionated addition to the assertion library, so if this isn't a good fit, feel free to close.