1
|
|
|
from __future__ import absolute_import |
2
|
|
|
from .selfdescribing import SelfDescribing |
3
|
|
|
|
4
|
|
|
__author__ = "Jon Reid" |
5
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
6
|
|
|
__license__ = "BSD, see License.txt" |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Matcher(SelfDescribing): |
10
|
|
|
"""A matcher over acceptable values. |
11
|
|
|
|
12
|
|
|
A matcher is able to describe itself to give feedback when it fails. |
13
|
|
|
|
14
|
|
|
Matcher implementations should *not* directly implement this protocol. |
15
|
|
|
Instead, *extend* the :py:class:`~hamcrest.core.base_matcher.BaseMatcher` |
16
|
|
|
class, which will ensure that the |
17
|
|
|
:py:class:`~hamcrest.core.matcher.Matcher` API can grow to support new |
18
|
|
|
features and remain compatible with all |
19
|
|
|
:py:class:`~hamcrest.core.matcher.Matcher` implementations. |
20
|
|
|
|
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
def matches(self, item, mismatch_description=None): |
24
|
|
|
"""Evaluates the matcher for argument item. |
25
|
|
|
|
26
|
|
|
If a mismatch is detected and argument ``mismatch_description`` is |
27
|
|
|
provided, it will generate a description of why the matcher has not |
28
|
|
|
accepted the item. |
29
|
|
|
|
30
|
|
|
:param item: The object against which the matcher is evaluated. |
31
|
|
|
:returns: ``True`` if ``item`` matches, otherwise ``False``. |
32
|
|
|
|
33
|
|
|
""" |
34
|
|
|
raise NotImplementedError('matches') |
35
|
|
|
|
36
|
|
|
def describe_mismatch(self, item, mismatch_description): |
37
|
|
|
"""Generates a description of why the matcher has not accepted the |
38
|
|
|
item. |
39
|
|
|
|
40
|
|
|
The description will be part of a larger description of why a matching |
41
|
|
|
failed, so it should be concise. |
42
|
|
|
|
43
|
|
|
This method assumes that ``matches(item)`` is ``False``, but will not |
44
|
|
|
check this. |
45
|
|
|
|
46
|
|
|
:param item: The item that the |
47
|
|
|
:py:class:`~hamcrest.core.matcher.Matcher` has rejected. |
48
|
|
|
:param mismatch_description: The description to be built or appended |
49
|
|
|
to. |
50
|
|
|
|
51
|
|
|
""" |
52
|
|
|
raise NotImplementedError('describe_mismatch') |
53
|
|
|
|