Completed
Pull Request — master (#80)
by
unknown
33s
created

StringContains   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 2 1
A _matches() 0 4 2
A relationship() 0 2 1
1
from hamcrest.library.text.substringmatcher import SubstringMatcher
2
from hamcrest.core.helpers.hasmethod import hasmethod
3
4
__author__ = "Jon Reid"
5
__copyright__ = "Copyright 2011 hamcrest.org"
6
__license__ = "BSD, see License.txt"
7
8
9
class StringContains(SubstringMatcher):
10
11
    def __init__(self, substring):
12
        super(StringContains, self).__init__(substring)
13
14
    def _matches(self, item):
15
        if not hasmethod(item, 'find'):
16
            return False
17
        return item.find(self.substring) >= 0
18
19
    def relationship(self):
20
        return 'containing'
21
22
23
def contains_string(substring):
24
    """Matches if object is a string containing a given string.
25
26
    :param string: The string to search for.
27
28
    This matcher first checks whether the evaluated object is a string. If so,
29
    it checks whether it contains ``string``.
30
31
    Example::
32
33
        contains_string("def")
34
35
    will match "abcdefg".
36
37
    """
38
    return StringContains(substring)
39