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.

issequence_containing.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. __author__ = "Jon Reid"
  2. __copyright__ = "Copyright 2011 hamcrest.org"
  3. __license__ = "BSD, see License.txt"
  4. from hamcrest.core.base_matcher import BaseMatcher
  5. from hamcrest.core.core.allof import all_of
  6. from hamcrest.core.helpers.hasmethod import hasmethod
  7. from hamcrest.core.helpers.wrap_matcher import wrap_matcher
  8. class IsSequenceContaining(BaseMatcher):
  9. def __init__(self, element_matcher):
  10. self.element_matcher = element_matcher
  11. def _matches(self, sequence):
  12. try:
  13. for item in sequence:
  14. if self.element_matcher.matches(item):
  15. return True
  16. except TypeError: # not a sequence
  17. return False
  18. def describe_to(self, description):
  19. description.append_text('a sequence containing ') \
  20. .append_description_of(self.element_matcher)
  21. # It'd be great to make use of all_of, but we can't be sure we won't
  22. # be seeing a one-time sequence here (like a generator); see issue #20
  23. # Instead, we wrap it inside a class that will convert the sequence into
  24. # a concrete list and then hand it off to the all_of matcher.
  25. class IsSequenceContainingEvery(BaseMatcher):
  26. def __init__(self, *element_matchers):
  27. delegates = [has_item(e) for e in element_matchers]
  28. self.matcher = all_of(*delegates)
  29. def _matches(self, sequence):
  30. try:
  31. return self.matcher.matches(list(sequence))
  32. except TypeError:
  33. return False
  34. def describe_mismatch(self, item, mismatch_description):
  35. self.matcher.describe_mismatch(item, mismatch_description)
  36. def describe_to(self, description):
  37. self.matcher.describe_to(description)
  38. def has_item(match):
  39. """Matches if any element of sequence satisfies a given matcher.
  40. :param match: The matcher to satisfy, or an expected value for
  41. :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
  42. This matcher iterates the evaluated sequence, searching for any element
  43. that satisfies a given matcher. If a matching element is found,
  44. ``has_item`` is satisfied.
  45. If the ``match`` argument is not a matcher, it is implicitly wrapped in an
  46. :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
  47. equality.
  48. """
  49. return IsSequenceContaining(wrap_matcher(match))
  50. def has_items(*items):
  51. """Matches if all of the given matchers are satisfied by any elements of
  52. the sequence.
  53. :param match1,...: A comma-separated list of matchers.
  54. This matcher iterates the given matchers, searching for any elements in the
  55. evaluated sequence that satisfy them. If each matcher is satisfied, then
  56. ``has_items`` is satisfied.
  57. Any argument that is not a matcher is implicitly wrapped in an
  58. :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
  59. equality.
  60. """
  61. matchers = []
  62. for item in items:
  63. matchers.append(wrap_matcher(item))
  64. return IsSequenceContainingEvery(*matchers)