-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSequenceMatchers.swift
176 lines (154 loc) · 6.67 KB
/
SequenceMatchers.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
public func empty<T: Collection>() -> Matcher<T> {
return describedAs("empty", hasCount(0))
}
public func hasCount<T: Collection>(_ matcher: Matcher<Int>) -> Matcher<T> {
return Matcher("has count " + matcher.description) { (value: T) -> MatchResult in
let n = value.count
return delegateMatching(n, matcher) {
return "count " + describeActualValue(n, $0)
}
}
}
public func hasCount<T: Collection>(_ expectedCount: Int) -> Matcher<T> {
return hasCount(equalToWithoutDescription(expectedCount))
}
public func everyItem<T, S: Sequence>(_ matcher: Matcher<T>) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence where every item \(matcher.description)") { (values: S) -> MatchResult in
var mismatchDescriptions: [String?] = []
for value in values {
switch delegateMatching(value, matcher, { (mismatchDescription: String?) -> String? in
"mismatch: \(value)" + (mismatchDescription.map {" (\($0))"} ?? "")
}) {
case let .mismatch(mismatchDescription):
mismatchDescriptions.append(mismatchDescription)
default:
break
}
}
return MatchResult(mismatchDescriptions.isEmpty, joinDescriptions(mismatchDescriptions))
}
}
private func hasItem<T, S: Sequence>(_ matcher: Matcher<T>, _ values: S) -> Bool where S.Iterator.Element == T {
for value in values {
if matcher.matches(value).boolValue {
return true
}
}
return false
}
public func hasItem<T, S: Sequence>(_ matcher: Matcher<T>) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence containing \(matcher.description)") { (values: S) -> Bool in
hasItem(matcher, values)
}
}
public func hasItem<T: Equatable, S: Sequence>(_ expectedValue: T) -> Matcher<S> where S.Iterator.Element == T {
return hasItem(equalToWithoutDescription(expectedValue))
}
private func hasItem<T, S: Sequence>(matcher: Matcher<T>, values: S, atIndex: Int) -> Bool where S.Iterator.Element == T {
for (index, value) in values.enumerated() {
if matcher.matches(value).boolValue && index == atIndex {
return true
}
}
return false
}
public func hasItem<T, S: Sequence>(_ matcher: Matcher<T>, atIndex index: Int) -> Matcher<S> where S.Iterator.Element == T {
Matcher("a sequence containing \(matcher.description) at index \(index)") { (values: S) -> Bool in
hasItem(matcher: matcher, values: values, atIndex: index)
}
}
public func hasItem<T: Equatable, S: Sequence>(_ expectedValue: T, atIndex index: Int) -> Matcher<S> where S.Iterator.Element == T {
hasItem(equalToWithoutDescription(expectedValue), atIndex: index)
}
private func hasItems<T, S: Sequence>(_ matchers: [Matcher<T>]) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence containing \(joinMatcherDescriptions(matchers))") { (values: S) -> MatchResult in
var missingItems = [] as [Matcher<T>]
for matcher in matchers {
if !hasItem(matcher, values) {
missingItems.append(matcher)
}
}
switch missingItems.count {
case 0:
return .match
case 1:
return .mismatch("missing item \(missingItems[0].description)")
default:
return .mismatch("missing items " + joinDescriptions(matchers.map({$0.description})))
}
}
}
public func hasItems<T, S: Sequence>(_ matchers: Matcher<T>...) -> Matcher<S> where S.Iterator.Element == T {
return hasItems(matchers)
}
public func hasItems<T: Equatable, S: Sequence>(_ expectedValues: T...) -> Matcher<S> where S.Iterator.Element == T {
return hasItems(expectedValues.map {equalToWithoutDescription($0)})
}
private func contains<T, S: Sequence>(_ matchers: [Matcher<T>]) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence containing " + joinDescriptions(matchers.map({$0.description}))) { (values: S) -> MatchResult in
return applyMatchers(matchers, values: values)
}
}
public func contains<T, S: Sequence>(_ matchers: Matcher<T>...) -> Matcher<S> where S.Iterator.Element == T {
return contains(matchers)
}
public func contains<T: Equatable, S: Sequence>(_ expectedValues: T...) -> Matcher<S> where S.Iterator.Element == T {
return contains(expectedValues.map {equalToWithoutDescription($0)})
}
private func containsInAnyOrder<T, S: Sequence>(_ matchers: [Matcher<T>]) -> Matcher<S> where S.Iterator.Element == T {
let descriptions = joinDescriptions(matchers.map({$0.description}))
return Matcher("a sequence containing in any order " + descriptions) { (values: S) -> MatchResult in
var unmatchedValues: [T] = []
var remainingMatchers = matchers
values:
for value in values {
var i = 0
for matcher in remainingMatchers {
if matcher.matches(value).boolValue {
remainingMatchers.remove(at: i)
continue values
}
i += 1
}
unmatchedValues.append(value)
}
let isMatch = remainingMatchers.isEmpty && unmatchedValues.isEmpty
if !isMatch {
return applyMatchers(remainingMatchers, values: unmatchedValues)
} else {
return .match
}
}
}
public func containsInAnyOrder<T, S: Sequence>(_ matchers: Matcher<T>...) -> Matcher<S> where S.Iterator.Element == T {
return containsInAnyOrder(matchers)
}
public func containsInAnyOrder<T: Equatable, S: Sequence>(_ expectedValues: T...) -> Matcher<S> where S.Iterator.Element == T {
return containsInAnyOrder(expectedValues.map {equalToWithoutDescription($0)})
}
public func applyMatchers<T, S: Sequence>(_ matchers: [Matcher<T>], values: S) -> MatchResult where S.Iterator.Element == T {
var mismatchDescriptions: [String?] = []
var i = 0
for (value, matcher) in zip(values, matchers) {
switch delegateMatching(value, matcher, {
"mismatch: " + describeMismatch(value, matcher.description, $0)
}) {
case let .mismatch(mismatchDescription):
mismatchDescriptions.append(mismatchDescription)
default:
break
}
i += 1
}
var j = 0
for value in values {
if j >= i {
mismatchDescriptions.append("unmatched item \(describe(value))")
}
j += 1
}
for matcher in matchers[i..<matchers.count] {
mismatchDescriptions.append("missing item \(matcher.description)")
}
return MatchResult(mismatchDescriptions.isEmpty, joinDescriptions(mismatchDescriptions))
}