src.hamcrest.library.text.StringMatchesPattern   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 2 1
A describe_to() 0 4 1
A _matches() 0 2 1
1
__author__ = "Chris Rose"
2
__copyright__ = "Copyright 2011 hamcrest.org"
3
__license__ = "BSD, see License.txt"
4
5
import re
6
7
import six
8
9
from hamcrest.core.base_matcher import BaseMatcher
10
from hamcrest.core.helpers.hasmethod import hasmethod
11
12
class StringMatchesPattern(BaseMatcher):
13
14
    def __init__(self, pattern):
15
        self.pattern = pattern
16
17
    def describe_to(self, description):
18
        description.append_text("a string matching '") \
19
                                   .append_text(self.pattern.pattern) \
20
                                   .append_text("'")
21
22
    def _matches(self, item):
23
        return self.pattern.search(item) is not None
24
25
26
def matches_regexp(pattern):
27
    """Matches if object is a string containing a match for a given regular
28
    expression.
29
30
    :param pattern: The regular expression to search for.
31
32
    This matcher first checks whether the evaluated object is a string. If so,
33
    it checks if the regular expression ``pattern`` matches anywhere within the
34
    evaluated object.
35
36
    """
37
    if isinstance(pattern, six.string_types):
38
        pattern = re.compile(pattern)
39
40
    return StringMatchesPattern(pattern)
41