src.hamcrest.core.core.AllOf.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
from hamcrest.core.base_matcher import BaseMatcher
2
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
3
4
__author__ = "Jon Reid"
5
__copyright__ = "Copyright 2011 hamcrest.org"
6
__license__ = "BSD, see License.txt"
7
8
9
class AllOf(BaseMatcher):
10
11
    def __init__(self, *matchers):
12
        self.matchers = matchers
13
14
    def matches(self, item, mismatch_description=None):
15
        for matcher in self.matchers:
16
            if not matcher.matches(item):
17
                if mismatch_description:
18
                    mismatch_description.append_description_of(matcher) \
19
                                        .append_text(' ')
20
                    matcher.describe_mismatch(item, mismatch_description)
21
                return False
22
        return True
23
24
    def describe_mismatch(self, item, mismatch_description):
25
        self.matches(item, mismatch_description)
26
27
    def describe_to(self, description):
28
        description.append_list('(', ' and ', ')', self.matchers)
29
30
31
def all_of(*items):
32
    """Matches if all of the given matchers evaluate to ``True``.
33
34
    :param matcher1,...:  A comma-separated list of matchers.
35
36
    The matchers are evaluated from left to right using short-circuit
37
    evaluation, so evaluation stops as soon as a matcher returns ``False``.
38
39
    Any argument that is not a matcher is implicitly wrapped in an
40
    :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
41
    equality.
42
43
    """
44
    return AllOf(*[wrap_matcher(item) for item in items])
45