describe_to()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

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