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.
What:
Add Collection matchers, as requested in #346
AnyContaining(element)
: A container that containselement
AnyContainingAll(element_list)
: A container that contains every element ofelement_list
AnyIterable()
: Any iterableIterableWithElements(element_list)
: an iterable containing all the elements inelement_list
in the same order"AnyNotEmpty()
: An object wherelen()
does not evaluate to zero"AnyEmpty()
: An object wherelen()
evaluates to zero"Why:
Currently, the official matchers support Lists and Dicts, but not much in the way of abstract collections. This PR enables patterns like:
How:
I added these matchers to matchers.py alongside their List/Dict counterparts. I added unittests to matchers_unittest.py. Finally, I added documentation in patching/argument_matchers/.
Risks:
No risk of breaking existing code, since these are largely standalone, however there are some edge cases for these matchers that may or may not be intuitive to the user.
AnyEmpty
/AnyNotEmpty
rely onlen()
, so they will throw onAnyEmpty() == iter([1, 2, 3])
, for example.AnyContaining()
/AnyContainingAll()
rely onin
, so will try to match anything that implements__contains__
Checklist: