mwilliamson / python-precisely

Matcher library for Python
BSD 2-Clause "Simplified" License
238 stars 11 forks source link

Add contains_only matcher #10

Closed danodonovan closed 5 years ago

danodonovan commented 5 years ago

I hope I've addressed your suggestions, always matching an empty list as True seems counter-intuitive, but I admit I don't understand all the nuances of this library.

I use this all_elements feature in several repositories - having it available from master / Pypi would simplify some build processes for me!

mwilliamson commented 5 years ago

I think it's the most consistent with other systems e.g. "for all" in predicate logic, the behaviour of "all" in Python. For instance, in Python:

all([]) True

On Wed, 21 Nov 2018 05:05:30 -0800 danodonovan notifications@github.com wrote:

danodonovan commented on this pull request.

@@ -139,4 +139,30 @@ def describe(self):
))

+def contains_only(matcher):

  • return IsSequenceContainsOnly(matcher)
  • +class IsSequenceContainsOnly(Matcher):

  • def init(self, matcher):
  • self._matcher = matcher
  • def match(self, actual):
  • values = list(actual)
  • if len(values) == 0:
  • return unmatched(_empty_iterable_description)

I've updated the code, but this does feel a little counter intuitive to me - for example

     assert_that([], all_elements(equal_to(42)))

Can we really say that every item in an empty list is equal to anything? ie If I want to check every item in a list is 42, I would be surprised if an empty list matched.

mwilliamson commented 5 years ago

Looks good, thanks.