You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

match_equality.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from hamcrest.core.string_description import tostring
  2. from hamcrest.core.helpers.wrap_matcher import wrap_matcher
  3. __author__ = "Chris Rose"
  4. __copyright__ = "Copyright 2011 hamcrest.org"
  5. __license__ = "BSD, see License.txt"
  6. __unittest = True
  7. class EqualityWrapper(object):
  8. def __init__(self, matcher):
  9. self.matcher = matcher
  10. def __eq__(self, object):
  11. return self.matcher.matches(object)
  12. def __str__(self):
  13. return repr(self)
  14. def __repr__(self):
  15. return tostring(self.matcher)
  16. def match_equality(matcher):
  17. """Wraps a matcher to define equality in terms of satisfying the matcher.
  18. ``match_equality`` allows Hamcrest matchers to be used in libraries that
  19. are not Hamcrest-aware. They might use the equality operator::
  20. assert match_equality(matcher) == object
  21. Or they might provide a method that uses equality for its test::
  22. library.method_that_tests_eq(match_equality(matcher))
  23. One concrete example is integrating with the ``assert_called_with`` methods
  24. in Michael Foord's `mock <http://www.voidspace.org.uk/python/mock/>`_
  25. library.
  26. """
  27. return EqualityWrapper(wrap_matcher(matcher))