Skip to content

Commit

Permalink
Make result type of expectations optional for consistency with #780
Browse files Browse the repository at this point in the history
  • Loading branch information
grynspan committed Oct 22, 2024
1 parent 375f6af commit 1d3b1d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func callExitTest(
isRequired: Bool,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation
) async -> Result<ExitTestArtifacts, any Error> {
) async -> Result<ExitTestArtifacts?, any Error> {
guard let configuration = Configuration.current ?? Configuration.all.first else {
preconditionFailure("A test must be running on the current task to use #expect(exitsWith:).")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ public func __checkClosureCall(
isRequired: Bool,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation
) async -> Result<ExitTestArtifacts, any Error> {
) async -> Result<ExitTestArtifacts?, any Error> {
await callExitTest(
exitsWith: expectedExitCondition,
observing: observedValues,
Expand Down
19 changes: 16 additions & 3 deletions Sources/Testing/Support/Additions/ResultAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@ extension Result {
/// `#require()` macros. Do not call it directly.
@inlinable public func __expected() where Success == Void {}

/// Handle this instance as if it were returned from a call to `#require()`.
///
/// - Warning: This function is used to implement the `#expect()` and
/// `#require()` macros. Do not call it directly.
@inlinable public func __required() throws -> Success {
try get()
}
}

// MARK: - Optional success values

extension Result {
/// Handle this instance as if it were returned from a call to `#expect()`.
///
/// - Warning: This function is used to implement the `#expect()` and
/// `#require()` macros. Do not call it directly.
@inlinable public func __expected() -> Success? {
@inlinable public func __expected<T>() -> Success where Success == T? {
try? get()
}

/// Handle this instance as if it were returned from a call to `#require()`.
///
/// - Warning: This function is used to implement the `#expect()` and
/// `#require()` macros. Do not call it directly.
@inlinable public func __required() throws -> Success {
try get()
@inlinable public func __required<T>() throws -> T where Success == T? {
// TODO: handle edge case where the value is nil (see #780)
try get()!
}
}

0 comments on commit 1d3b1d4

Please sign in to comment.