matcher.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from __future__ import absolute_import
  2. from .selfdescribing import SelfDescribing
  3. __author__ = "Jon Reid"
  4. __copyright__ = "Copyright 2011 hamcrest.org"
  5. __license__ = "BSD, see License.txt"
  6. class Matcher(SelfDescribing):
  7. """A matcher over acceptable values.
  8. A matcher is able to describe itself to give feedback when it fails.
  9. Matcher implementations should *not* directly implement this protocol.
  10. Instead, *extend* the :py:class:`~hamcrest.core.base_matcher.BaseMatcher`
  11. class, which will ensure that the
  12. :py:class:`~hamcrest.core.matcher.Matcher` API can grow to support new
  13. features and remain compatible with all
  14. :py:class:`~hamcrest.core.matcher.Matcher` implementations.
  15. """
  16. def matches(self, item, mismatch_description=None):
  17. """Evaluates the matcher for argument item.
  18. If a mismatch is detected and argument ``mismatch_description`` is
  19. provided, it will generate a description of why the matcher has not
  20. accepted the item.
  21. :param item: The object against which the matcher is evaluated.
  22. :returns: ``True`` if ``item`` matches, otherwise ``False``.
  23. """
  24. raise NotImplementedError('matches')
  25. def describe_mismatch(self, item, mismatch_description):
  26. """Generates a description of why the matcher has not accepted the
  27. item.
  28. The description will be part of a larger description of why a matching
  29. failed, so it should be concise.
  30. This method assumes that ``matches(item)`` is ``False``, but will not
  31. check this.
  32. :param item: The item that the
  33. :py:class:`~hamcrest.core.matcher.Matcher` has rejected.
  34. :param mismatch_description: The description to be built or appended
  35. to.
  36. """
  37. raise NotImplementedError('describe_mismatch')