src.hamcrest.library.object.HasString   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 11
Duplicated Lines 0 %
Metric Value
dl 0
loc 11
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A describe_to() 0 3 1
A _matches() 0 2 1
A __init__() 0 2 1
1
from hamcrest.core.base_matcher import BaseMatcher
2
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
3
4
__author__ = "Jon Reid"
5
__copyright__ = "Copyright 2011 hamcrest.org"
6
__license__ = "BSD, see License.txt"
7
8
9
class HasString(BaseMatcher):
10
11
    def __init__(self, str_matcher):
12
        self.str_matcher = str_matcher
13
14
    def _matches(self, item):
15
        return self.str_matcher.matches(str(item))
16
17
    def describe_to(self, description):
18
        description.append_text('an object with str ')          \
19
                    .append_description_of(self.str_matcher)
20
21
22
def has_string(match):
23
    """Matches if ``str(item)`` satisfies a given matcher.
24
25
    :param match: The matcher to satisfy, or an expected value for
26
        :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
27
28
    This matcher invokes the :py:func:`str` function on the evaluated object to
29
    get its length, passing the result to a given matcher for evaluation. If
30
    the ``match`` argument is not a matcher, it is implicitly wrapped in an
31
    :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
32
    equality.
33
34
    Examples::
35
36
        has_string(starts_with('foo'))
37
        has_string('bar')
38
39
    """
40
    return HasString(wrap_matcher(match))
41