1
|
|
|
from hamcrest.core.base_matcher import BaseMatcher |
2
|
|
|
import re |
3
|
|
|
|
4
|
|
|
__author__ = "Jon Reid" |
5
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
6
|
|
|
__license__ = "BSD, see License.txt" |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
ARG_PATTERN = re.compile('%([0-9]+)') |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class DescribedAs(BaseMatcher): |
13
|
|
|
|
14
|
|
|
def __init__(self, description_template, matcher, *values): |
15
|
|
|
self.template = description_template |
16
|
|
|
self.matcher = matcher |
17
|
|
|
self.values = values |
18
|
|
|
|
19
|
|
|
def matches(self, item, mismatch_description=None): |
20
|
|
|
return self.matcher.matches(item, mismatch_description) |
21
|
|
|
|
22
|
|
|
def describe_mismatch(self, item, mismatch_description): |
23
|
|
|
self.matcher.describe_mismatch(item, mismatch_description) |
24
|
|
|
|
25
|
|
|
def describe_to(self, description): |
26
|
|
|
text_start = 0 |
27
|
|
|
for match in re.finditer(ARG_PATTERN, self.template): |
28
|
|
|
description.append_text(self.template[text_start:match.start()]) |
29
|
|
|
arg_index = int(match.group()[1:]) |
30
|
|
|
description.append_description_of(self.values[arg_index]) |
31
|
|
|
text_start = match.end() |
32
|
|
|
|
33
|
|
|
if text_start < len(self.template): |
34
|
|
|
description.append_text(self.template[text_start:]) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def described_as(description, matcher, *values): |
38
|
|
|
"""Adds custom failure description to a given matcher. |
39
|
|
|
|
40
|
|
|
:param description: Overrides the matcher's description. |
41
|
|
|
:param matcher: The matcher to satisfy. |
42
|
|
|
:param value1,...: Optional comma-separated list of substitution values. |
43
|
|
|
|
44
|
|
|
The description may contain substitution placeholders %0, %1, etc. These |
45
|
|
|
will be replaced by any values that follow the matcher. |
46
|
|
|
|
47
|
|
|
""" |
48
|
|
|
return DescribedAs(description, matcher, *values) |
49
|
|
|
|