Completed
Pull Request — master (#75)
by Cézar
01:32
created

DescribedAs   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A describe_to() 0 10 3
A matches() 0 2 1
A describe_mismatch() 0 2 1
A __init__() 0 4 1
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