Skip to content

Commit

Permalink
Refactor toNever matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesyo committed May 9, 2021
1 parent dbcb1d8 commit 013c0fc
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions Sources/Nimble/Matchers/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,19 @@ extension AsyncDefaults {
public static var PollInterval: TimeInterval = 0.01
}

private func async<T>(style: ExpectationStyle, predicate: Predicate<T>, timeout: DispatchTimeInterval, poll: DispatchTimeInterval, fnName: String) -> Predicate<T> {
return Predicate { actualExpression in
let uncachedExpression = actualExpression.withoutCaching()
let fnName = "expect(...).\(fnName)(...)"
var lastPredicateResult: PredicateResult?
let result = pollBlock(
pollInterval: poll,
timeoutInterval: timeout,
file: actualExpression.location.file,
line: actualExpression.location.line,
fnName: fnName) {
lastPredicateResult = try predicate.satisfies(uncachedExpression)
return lastPredicateResult!.toBoolean(expectation: style)
}
switch result {
case .completed: return lastPredicateResult!
case .timedOut:
let message = lastPredicateResult?.message ?? .fail("timed out before returning a value")
return PredicateResult(status: .fail, message: message)
case let .errorThrown(error):
return PredicateResult(status: .fail, message: .fail("unexpected error thrown: <\(error)>"))
case let .raisedException(exception):
return PredicateResult(status: .fail, message: .fail("unexpected exception raised: \(exception)"))
case .blockedRunLoop:
let message = lastPredicateResult?.message.appended(message: " (timed out, but main run loop was unresponsive).") ??
.fail("main run loop was unresponsive")
return PredicateResult(status: .fail, message: message)
case .incomplete:
internalError("Reached .incomplete state for \(fnName)(...).")
}
}
private enum AsyncMatchStyle {
case eventually, never
}

private func toNeverPredicate<T>(predicate: Predicate<T>, timeout: DispatchTimeInterval, poll: DispatchTimeInterval, fnName: String) -> Predicate<T> {
// swiftlint:disable:next function_parameter_count
private func async<T>(
style: ExpectationStyle,
matchStyle: AsyncMatchStyle,
predicate: Predicate<T>,
timeout: DispatchTimeInterval,
poll: DispatchTimeInterval,
fnName: String
) -> Predicate<T> {
return Predicate { actualExpression in
let uncachedExpression = actualExpression.withoutCaching()
let fnName = "expect(...).\(fnName)(...)"
Expand All @@ -60,22 +39,32 @@ private func toNeverPredicate<T>(predicate: Predicate<T>, timeout: DispatchTimeI
line: actualExpression.location.line,
fnName: fnName) {
lastPredicateResult = try predicate.satisfies(uncachedExpression)
return lastPredicateResult!.toBoolean(expectation: .toMatch)
return lastPredicateResult!.toBoolean(expectation: style)
}
switch result {
case .completed:
return PredicateResult(
status: .fail,
message: lastPredicateResult?.message ?? .fail("matched the predicate when it shouldn't have")
)
switch matchStyle {
case .eventually:
return lastPredicateResult!
case .never:
return PredicateResult(
status: .fail,
message: lastPredicateResult?.message ?? .fail("matched the predicate when it shouldn't have")
)
}
case .timedOut:
return PredicateResult(status: .doesNotMatch, message: .expectedTo("never match the predicate"))
switch matchStyle {
case .eventually:
let message = lastPredicateResult?.message ?? .fail("timed out before returning a value")
return PredicateResult(status: .fail, message: message)
case .never:
return PredicateResult(status: .doesNotMatch, message: .expectedTo("never match the predicate"))
}
case let .errorThrown(error):
return PredicateResult(status: .fail, message: .fail("unexpected error thrown: <\(error)>"))
case let .raisedException(exception):
return PredicateResult(status: .fail, message: .fail("unexpected exception raised: \(exception)"))
case .blockedRunLoop:
// swiftlint:disable:next line_length
let message = lastPredicateResult?.message.appended(message: " (timed out, but main run loop was unresponsive).") ??
.fail("main run loop was unresponsive")
return PredicateResult(status: .fail, message: message)
Expand Down Expand Up @@ -105,7 +94,14 @@ extension Expectation {
let (pass, msg) = execute(
expression,
.toMatch,
async(style: .toMatch, predicate: predicate, timeout: timeout, poll: pollInterval, fnName: "toEventually"),
async(
style: .toMatch,
matchStyle: .eventually,
predicate: predicate,
timeout: timeout,
poll: pollInterval,
fnName: "toEventually"
),
to: "to eventually",
description: description,
captureExceptions: false
Expand All @@ -127,6 +123,7 @@ extension Expectation {
.toNotMatch,
async(
style: .toNotMatch,
matchStyle: .eventually,
predicate: predicate,
timeout: timeout,
poll: pollInterval,
Expand Down Expand Up @@ -163,7 +160,14 @@ extension Expectation {
let (pass, msg) = execute(
expression,
.toNotMatch,
toNeverPredicate(predicate: predicate, timeout: until, poll: pollInterval, fnName: "toNever"),
async(
style: .toMatch,
matchStyle: .never,
predicate: predicate,
timeout: until,
poll: pollInterval,
fnName: "toNever"
),
to: "to never",
description: description,
captureExceptions: false
Expand Down

0 comments on commit 013c0fc

Please sign in to comment.