Skip to content

Commit 07d6799

Browse files
committed
Refactor toNever matcher
1 parent fd4f611 commit 07d6799

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

Sources/Nimble/Matchers/Async.swift

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,19 @@ extension AsyncDefaults {
1515
public static var PollInterval: TimeInterval = 0.01
1616
}
1717

18-
private func async<T>(style: ExpectationStyle, predicate: Predicate<T>, timeout: DispatchTimeInterval, poll: DispatchTimeInterval, fnName: String) -> Predicate<T> {
19-
return Predicate { actualExpression in
20-
let uncachedExpression = actualExpression.withoutCaching()
21-
let fnName = "expect(...).\(fnName)(...)"
22-
var lastPredicateResult: PredicateResult?
23-
let result = pollBlock(
24-
pollInterval: poll,
25-
timeoutInterval: timeout,
26-
file: actualExpression.location.file,
27-
line: actualExpression.location.line,
28-
fnName: fnName) {
29-
lastPredicateResult = try predicate.satisfies(uncachedExpression)
30-
return lastPredicateResult!.toBoolean(expectation: style)
31-
}
32-
switch result {
33-
case .completed: return lastPredicateResult!
34-
case .timedOut:
35-
let message = lastPredicateResult?.message ?? .fail("timed out before returning a value")
36-
return PredicateResult(status: .fail, message: message)
37-
case let .errorThrown(error):
38-
return PredicateResult(status: .fail, message: .fail("unexpected error thrown: <\(error)>"))
39-
case let .raisedException(exception):
40-
return PredicateResult(status: .fail, message: .fail("unexpected exception raised: \(exception)"))
41-
case .blockedRunLoop:
42-
let message = lastPredicateResult?.message.appended(message: " (timed out, but main run loop was unresponsive).") ??
43-
.fail("main run loop was unresponsive")
44-
return PredicateResult(status: .fail, message: message)
45-
case .incomplete:
46-
internalError("Reached .incomplete state for \(fnName)(...).")
47-
}
48-
}
18+
private enum AsyncMatchStyle {
19+
case eventually, never
4920
}
5021

51-
private func toNeverPredicate<T>(predicate: Predicate<T>, timeout: DispatchTimeInterval, poll: DispatchTimeInterval, fnName: String) -> Predicate<T> {
22+
// swiftlint:disable:next function_parameter_count
23+
private func async<T>(
24+
style: ExpectationStyle,
25+
matchStyle: AsyncMatchStyle,
26+
predicate: Predicate<T>,
27+
timeout: DispatchTimeInterval,
28+
poll: DispatchTimeInterval,
29+
fnName: String
30+
) -> Predicate<T> {
5231
return Predicate { actualExpression in
5332
let uncachedExpression = actualExpression.withoutCaching()
5433
let fnName = "expect(...).\(fnName)(...)"
@@ -60,22 +39,32 @@ private func toNeverPredicate<T>(predicate: Predicate<T>, timeout: DispatchTimeI
6039
line: actualExpression.location.line,
6140
fnName: fnName) {
6241
lastPredicateResult = try predicate.satisfies(uncachedExpression)
63-
return lastPredicateResult!.toBoolean(expectation: .toMatch)
42+
return lastPredicateResult!.toBoolean(expectation: style)
6443
}
6544
switch result {
6645
case .completed:
67-
return PredicateResult(
68-
status: .fail,
69-
message: lastPredicateResult?.message ?? .fail("matched the predicate when it shouldn't have")
70-
)
46+
switch matchStyle {
47+
case .eventually:
48+
return lastPredicateResult!
49+
case .never:
50+
return PredicateResult(
51+
status: .fail,
52+
message: lastPredicateResult?.message ?? .fail("matched the predicate when it shouldn't have")
53+
)
54+
}
7155
case .timedOut:
72-
return PredicateResult(status: .doesNotMatch, message: .expectedTo("never match the predicate"))
56+
switch matchStyle {
57+
case .eventually:
58+
let message = lastPredicateResult?.message ?? .fail("timed out before returning a value")
59+
return PredicateResult(status: .fail, message: message)
60+
case .never:
61+
return PredicateResult(status: .doesNotMatch, message: .expectedTo("never match the predicate"))
62+
}
7363
case let .errorThrown(error):
7464
return PredicateResult(status: .fail, message: .fail("unexpected error thrown: <\(error)>"))
7565
case let .raisedException(exception):
7666
return PredicateResult(status: .fail, message: .fail("unexpected exception raised: \(exception)"))
7767
case .blockedRunLoop:
78-
// swiftlint:disable:next line_length
7968
let message = lastPredicateResult?.message.appended(message: " (timed out, but main run loop was unresponsive).") ??
8069
.fail("main run loop was unresponsive")
8170
return PredicateResult(status: .fail, message: message)
@@ -105,7 +94,14 @@ extension Expectation {
10594
let (pass, msg) = execute(
10695
expression,
10796
.toMatch,
108-
async(style: .toMatch, predicate: predicate, timeout: timeout, poll: pollInterval, fnName: "toEventually"),
97+
async(
98+
style: .toMatch,
99+
matchStyle: .eventually,
100+
predicate: predicate,
101+
timeout: timeout,
102+
poll: pollInterval,
103+
fnName: "toEventually"
104+
),
109105
to: "to eventually",
110106
description: description,
111107
captureExceptions: false
@@ -127,6 +123,7 @@ extension Expectation {
127123
.toNotMatch,
128124
async(
129125
style: .toNotMatch,
126+
matchStyle: .eventually,
130127
predicate: predicate,
131128
timeout: timeout,
132129
poll: pollInterval,
@@ -163,7 +160,14 @@ extension Expectation {
163160
let (pass, msg) = execute(
164161
expression,
165162
.toNotMatch,
166-
toNeverPredicate(predicate: predicate, timeout: until, poll: pollInterval, fnName: "toNever"),
163+
async(
164+
style: .toMatch,
165+
matchStyle: .never,
166+
predicate: predicate,
167+
timeout: until,
168+
poll: pollInterval,
169+
fnName: "toNever"
170+
),
167171
to: "to never",
168172
description: description,
169173
captureExceptions: false

0 commit comments

Comments
 (0)