-
-
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 AsyncPredicate - Matchers with AsyncExpressions (#1056)
* Create AsyncPredicate, for allowing async functions in predicates. This will not ever replace the standard Predicates, but is meant to be a companion to it. * Allow satisfyAnyOf and satisfyAllOf to take in both Predicates and AsyncPredicates * Change the some AsyncablePredicate<T> in satisfyAnyOf/satisfyAllOf operators to any AsyncablePredicate<T> This is a workaround for a compiler bug in swift 5.7. * just require swift 5.8 for satisfyAny/AllOf with async predicates * xcode 14.3 requires macos 13 * Be more discerning when trying to find watchOS and macOS SDK versions * Use a more recent iPhone for testing * Add an async version of allPass. Update documentation to mention async predicates
- Loading branch information
Showing
22 changed files
with
1,346 additions
and
47 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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.