1
|
|
|
__author__ = "Romilly Cocking"
|
2
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org"
|
3
|
|
|
__license__ = "BSD, see License.txt"
|
4
|
|
|
|
5
|
|
|
from hamcrest.core.base_matcher import BaseMatcher
|
6
|
|
|
from hamcrest.core.helpers.hasmethod import hasmethod
|
7
|
|
|
|
8
|
|
|
import six
|
9
|
|
|
|
10
|
|
|
class StringContainsInOrder(BaseMatcher):
|
11
|
|
|
|
12
|
|
|
def __init__(self, *substrings):
|
13
|
|
|
for substring in substrings:
|
14
|
|
|
if not isinstance(substring, six.string_types):
|
15
|
|
|
raise TypeError(self.__class__.__name__
|
16
|
|
|
+ ' requires string arguments')
|
17
|
|
|
self.substrings = substrings
|
18
|
|
|
|
19
|
|
|
def _matches(self, item):
|
20
|
|
|
if not hasmethod(item, 'find'):
|
21
|
|
|
return False
|
22
|
|
|
from_index = 0
|
23
|
|
|
for substring in self.substrings:
|
24
|
|
|
from_index = item.find(substring, from_index)
|
25
|
|
|
if from_index == -1:
|
26
|
|
|
return False
|
27
|
|
|
return True
|
28
|
|
|
|
29
|
|
|
def describe_to(self, description):
|
30
|
|
|
description.append_list('a string containing ', ', ', ' in order',
|
31
|
|
|
self.substrings)
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def string_contains_in_order(*substrings):
|
35
|
|
|
"""Matches if object is a string containing a given list of substrings in
|
36
|
|
|
relative order.
|
37
|
|
|
|
38
|
|
|
:param string1,...: A comma-separated list of strings.
|
39
|
|
|
|
40
|
|
|
This matcher first checks whether the evaluated object is a string. If so,
|
41
|
|
|
it checks whether it contains a given list of strings, in relative order to
|
42
|
|
|
each other. The searches are performed starting from the beginning of the
|
43
|
|
|
evaluated string.
|
44
|
|
|
|
45
|
|
|
Example::
|
46
|
|
|
|
47
|
|
|
string_contains_in_order("bc", "fg", "jkl")
|
48
|
|
|
|
49
|
|
|
will match "abcdefghijklm".
|
50
|
|
|
|
51
|
|
|
"""
|
52
|
|
|
return StringContainsInOrder(*substrings)
|
53
|
|
|
|