Completed
Pull Request — master (#80)
by
unknown
36s
created

BaseMatcher.describe_mismatch()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
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