Completed
Pull Request — master (#80)
by
unknown
02:25
created

src.hamcrest.core.BaseMatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %
Metric Value
dl 0
loc 25
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseMatcher._matches() 0 2 1
A BaseMatcher.__str__() 0 2 1
A BaseMatcher.describe_mismatch() 0 2 1
A BaseMatcher.matches() 0 5 3
1
from __future__ import absolute_import
2
__author__ = "Jon Reid"
3
__copyright__ = "Copyright 2011 hamcrest.org"
4
__license__ = "BSD, see License.txt"
5
6
from hamcrest.core.matcher import Matcher
7
from hamcrest.core.string_description import tostring
8
9
10
class BaseMatcher(Matcher):
11
    """Base class for all :py:class:`~hamcrest.core.matcher.Matcher`
12
    implementations.
13
14
    Most implementations can just implement :py:obj:`_matches`, leaving the
15
    handling of any mismatch description to the ``matches`` method. But if it
16
    makes more sense to generate the mismatch description during the matching,
17
    override :py:meth:`~hamcrest.core.matcher.Matcher.matches` instead.
18
19
    """
20
21
    def __str__(self):
22
        return tostring(self)
23
24
    def _matches(self, item):
25
        raise NotImplementedError('_matches')
26
27
    def matches(self, item, mismatch_description=None):
28
        match_result = self._matches(item)
29
        if not match_result and mismatch_description:
30
            self.describe_mismatch(item, mismatch_description)
31
        return match_result
32
33
    def describe_mismatch(self, item, mismatch_description):
34
        mismatch_description.append_text('was ').append_description_of(item)
35