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.

isdict_containingvalue.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from hamcrest.core.base_matcher import BaseMatcher
  2. from hamcrest.core.helpers.hasmethod import hasmethod
  3. from hamcrest.core.helpers.wrap_matcher import wrap_matcher
  4. __author__ = "Jon Reid"
  5. __copyright__ = "Copyright 2011 hamcrest.org"
  6. __license__ = "BSD, see License.txt"
  7. class IsDictContainingValue(BaseMatcher):
  8. def __init__(self, value_matcher):
  9. self.value_matcher = value_matcher
  10. def _matches(self, dictionary):
  11. if hasmethod(dictionary, 'values'):
  12. for value in dictionary.values():
  13. if self.value_matcher.matches(value):
  14. return True
  15. return False
  16. def describe_to(self, description):
  17. description.append_text('a dictionary containing value ') \
  18. .append_description_of(self.value_matcher)
  19. def has_value(value):
  20. """Matches if dictionary contains an entry whose value satisfies a given
  21. matcher.
  22. :param value_match: The matcher to satisfy for the value, or an expected
  23. value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
  24. This matcher iterates the evaluated dictionary, searching for any key-value
  25. entry whose value satisfies the given matcher. If a matching entry is
  26. found, ``has_value`` is satisfied.
  27. Any argument that is not a matcher is implicitly wrapped in an
  28. :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
  29. equality.
  30. Examples::
  31. has_value(equal_to('bar'))
  32. has_value('bar')
  33. """
  34. return IsDictContainingValue(wrap_matcher(value))