-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an async version of allPass. Update documentation to mention asyn…
…c predicates
- Loading branch information
Showing
4 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
public func allPass<S: Sequence>( | ||
_ passFunc: @escaping (S.Element) async throws -> Bool | ||
) -> AsyncPredicate<S> { | ||
let matcher = AsyncPredicate<S.Element>.define("pass a condition") { actualExpression, message in | ||
guard let actual = try await actualExpression.evaluate() else { | ||
return PredicateResult(status: .fail, message: message) | ||
} | ||
return PredicateResult(bool: try await passFunc(actual), message: message) | ||
} | ||
return createPredicate(matcher) | ||
} | ||
|
||
public func allPass<S: Sequence>( | ||
_ passName: String, | ||
_ passFunc: @escaping (S.Element) async throws -> Bool | ||
) -> AsyncPredicate<S> { | ||
let matcher = AsyncPredicate<S.Element>.define(passName) { actualExpression, message in | ||
guard let actual = try await actualExpression.evaluate() else { | ||
return PredicateResult(status: .fail, message: message) | ||
} | ||
return PredicateResult(bool: try await passFunc(actual), message: message) | ||
} | ||
return createPredicate(matcher) | ||
} | ||
|
||
public func allPass<S: Sequence>(_ elementPredicate: AsyncPredicate<S.Element>) -> AsyncPredicate<S> { | ||
return createPredicate(elementPredicate) | ||
} | ||
|
||
private func createPredicate<S: Sequence>(_ elementMatcher: AsyncPredicate<S.Element>) -> AsyncPredicate<S> { | ||
return AsyncPredicate { actualExpression in | ||
guard let actualValue = try await actualExpression.evaluate() else { | ||
return PredicateResult( | ||
status: .fail, | ||
message: .appends(.expectedTo("all pass"), " (use beNil() to match nils)") | ||
) | ||
} | ||
|
||
var failure: ExpectationMessage = .expectedTo("all pass") | ||
for currentElement in actualValue { | ||
let exp = AsyncExpression( | ||
expression: { currentElement }, | ||
location: actualExpression.location | ||
) | ||
let predicateResult = try await elementMatcher.satisfies(exp) | ||
if predicateResult.status == .matches { | ||
failure = predicateResult.message.prepended(expectation: "all ") | ||
} else { | ||
failure = predicateResult.message | ||
.replacedExpectation({ .expectedTo($0.expectedMessage) }) | ||
.wrappedExpectation( | ||
before: "all ", | ||
after: ", but failed first at element <\(stringify(currentElement))>" | ||
+ " in <\(stringify(actualValue))>" | ||
) | ||
return PredicateResult(status: .doesNotMatch, message: failure) | ||
} | ||
} | ||
failure = failure.replacedExpectation({ expectation in | ||
return .expectedTo(expectation.expectedMessage) | ||
}) | ||
return PredicateResult(status: .matches, message: failure) | ||
} | ||
} |
Oops, something went wrong.