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

Complexity

Total Complexity 4

Size/Duplication

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _matches() 0 4 2
A relationship() 0 2 1
A __init__() 0 2 1
1
__author__ = "Jon Reid"
2
__copyright__ = "Copyright 2011 hamcrest.org"
3
__license__ = "BSD, see License.txt"
4
5
from hamcrest.library.text.substringmatcher import SubstringMatcher
6
from hamcrest.core.helpers.hasmethod import hasmethod
7
8
9
class StringStartsWith(SubstringMatcher):
10
11
    def __init__(self, substring):
12
        super(StringStartsWith, self).__init__(substring)
13
14
    def _matches(self, item):
15
        if not hasmethod(item, 'startswith'):
16
            return False
17
        return item.startswith(self.substring)
18
19
    def relationship(self):
20
        return 'starting with'
21
22
23
def starts_with(substring):
24
    """Matches if object is a string starting with 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 if ``string`` matches the beginning characters of the evaluated
30
    object.
31
32
    Example::
33
34
        starts_with("foo")
35
36
    will match "foobar".
37
38
    """
39
    return StringStartsWith(substring)
40