-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using async/await in expect (#1004)
This changes Expectation into a protocol, and converts the old Expectation struct into SyncExpectation and AsyncExpectation. Expression is still synchronous, and toEventually is dropped for AsyncExpectations. Additionally, renamed the older Async methods to Polling to try to reduce confusion as to what toEventually actually does. Updated documentation, both to explain what toEventually actually does, and to elaborate that Nimble supports async/await.
- Loading branch information
Showing
21 changed files
with
503 additions
and
228 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
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,45 @@ | ||
private func convertAsyncExpression<T>(_ asyncExpression: () async throws -> T) async -> (() throws -> T) { | ||
let result: Result<T, Error> | ||
do { | ||
result = .success(try await asyncExpression()) | ||
} catch { | ||
result = .failure(error) | ||
} | ||
return { try result.get() } | ||
} | ||
|
||
/// Make an ``AsyncExpectation`` on a given actual value. The value given is lazily evaluated. | ||
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () async throws -> T?) async -> AsyncExpectation<T> { | ||
return AsyncExpectation( | ||
expression: Expression( | ||
expression: await convertAsyncExpression(expression), | ||
location: SourceLocation(file: file, line: line), | ||
isClosure: true)) | ||
} | ||
|
||
/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked. | ||
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T)) async -> AsyncExpectation<T> { | ||
return AsyncExpectation( | ||
expression: Expression( | ||
expression: await convertAsyncExpression(expression()), | ||
location: SourceLocation(file: file, line: line), | ||
isClosure: true)) | ||
} | ||
|
||
/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked. | ||
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async -> AsyncExpectation<T> { | ||
return AsyncExpectation( | ||
expression: Expression( | ||
expression: await convertAsyncExpression(expression()), | ||
location: SourceLocation(file: file, line: line), | ||
isClosure: true)) | ||
} | ||
|
||
/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked. | ||
public func expect(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> Void)) async -> AsyncExpectation<Void> { | ||
return AsyncExpectation( | ||
expression: Expression( | ||
expression: await convertAsyncExpression(expression()), | ||
location: SourceLocation(file: file, line: line), | ||
isClosure: true)) | ||
} |
Oops, something went wrong.