is_empty.py 913 B

1234567891011121314151617181920212223242526272829303132333435
  1. from hamcrest.core.base_matcher import BaseMatcher
  2. __author__ = "Chris Rose"
  3. __copyright__ = "Copyright 2012 hamcrest.org"
  4. __license__ = "BSD, see License.txt"
  5. class IsEmpty(BaseMatcher):
  6. def matches(self, item, mismatch_description=None):
  7. try:
  8. if len(item) == 0:
  9. return True
  10. if mismatch_description:
  11. mismatch_description \
  12. .append_text('has %d item(s)' % len(item))
  13. except TypeError:
  14. if mismatch_description:
  15. mismatch_description \
  16. .append_text('does not support length')
  17. return False
  18. def describe_to(self, description):
  19. description.append_text('an empty collection')
  20. def empty():
  21. """
  22. This matcher matches any collection-like object that responds to the
  23. __len__ method, and has a length of 0.
  24. """
  25. return IsEmpty()