Total Complexity | 6 |
Total Lines | 25 |
Duplicated Lines | 0 % |
1 | from __future__ import absolute_import |
||
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 |