1
|
|
|
__author__ = "Jon Reid" |
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
|
|
|
from hamcrest.core.helpers.wrap_matcher import wrap_matcher |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class MatchingInOrder(object): |
11
|
|
|
def __init__(self, matchers, mismatch_description): |
12
|
|
|
self.matchers = matchers |
13
|
|
|
self.mismatch_description = mismatch_description |
14
|
|
|
self.next_match_index = 0 |
15
|
|
|
|
16
|
|
|
def matches(self, item): |
17
|
|
|
return self.isnotsurplus(item) and self.ismatched(item) |
18
|
|
|
|
19
|
|
|
def isfinished(self): |
20
|
|
|
if self.next_match_index < len(self.matchers): |
21
|
|
|
if self.mismatch_description: |
22
|
|
|
self.mismatch_description.append_text('No item matched: ') \ |
23
|
|
|
.append_description_of(self.matchers[self.next_match_index]) |
24
|
|
|
return False |
25
|
|
|
return True |
26
|
|
|
|
27
|
|
|
def ismatched(self, item): |
28
|
|
|
matcher = self.matchers[self.next_match_index] |
29
|
|
|
if not matcher.matches(item): |
30
|
|
|
if self.mismatch_description: |
31
|
|
|
self.mismatch_description.append_text('item ' + str(self.next_match_index) + ': ') |
32
|
|
|
matcher.describe_mismatch(item, self.mismatch_description) |
33
|
|
|
return False |
34
|
|
|
self.next_match_index += 1 |
35
|
|
|
return True |
36
|
|
|
|
37
|
|
|
def isnotsurplus(self, item): |
38
|
|
|
if len(self.matchers) <= self.next_match_index: |
39
|
|
|
if self.mismatch_description: |
40
|
|
|
self.mismatch_description.append_text('Not matched: ') \ |
41
|
|
|
.append_description_of(item) |
42
|
|
|
return False |
43
|
|
|
return True |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
class IsSequenceContainingInOrder(BaseMatcher): |
47
|
|
|
|
48
|
|
|
def __init__(self, matchers): |
49
|
|
|
self.matchers = matchers |
50
|
|
|
|
51
|
|
|
def matches(self, sequence, mismatch_description=None): |
52
|
|
|
try: |
53
|
|
|
matchsequence = MatchingInOrder(self.matchers, mismatch_description) |
54
|
|
|
for item in sequence: |
55
|
|
|
if not matchsequence.matches(item): |
56
|
|
|
return False |
57
|
|
|
return matchsequence.isfinished() |
58
|
|
|
except TypeError: |
59
|
|
|
if mismatch_description: |
60
|
|
|
super(IsSequenceContainingInOrder, self) \ |
61
|
|
|
.describe_mismatch(sequence, mismatch_description) |
62
|
|
|
return False |
63
|
|
|
|
64
|
|
|
def describe_mismatch(self, item, mismatch_description): |
65
|
|
|
self.matches(item, mismatch_description) |
66
|
|
|
|
67
|
|
|
def describe_to(self, description): |
68
|
|
|
description.append_text('a sequence containing ') \ |
69
|
|
|
.append_list('[', ', ', ']', self.matchers) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def contains(*items): |
73
|
|
|
"""Matches if sequence's elements satisfy a given list of matchers, in order. |
74
|
|
|
|
75
|
|
|
:param match1,...: A comma-separated list of matchers. |
76
|
|
|
|
77
|
|
|
This matcher iterates the evaluated sequence and a given list of matchers, |
78
|
|
|
seeing if each element satisfies its corresponding matcher. |
79
|
|
|
|
80
|
|
|
Any argument that is not a matcher is implicitly wrapped in an |
81
|
|
|
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for |
82
|
|
|
equality. |
83
|
|
|
|
84
|
|
|
""" |
85
|
|
|
matchers = [] |
86
|
|
|
for item in items: |
87
|
|
|
matchers.append(wrap_matcher(item)) |
88
|
|
|
return IsSequenceContainingInOrder(matchers) |
89
|
|
|
|