Skip to content

Commit 1d3b1d4

Browse files
committed
Make result type of expectations optional for consistency with #780
1 parent 375f6af commit 1d3b1d4

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func callExitTest(
236236
isRequired: Bool,
237237
isolation: isolated (any Actor)? = #isolation,
238238
sourceLocation: SourceLocation
239-
) async -> Result<ExitTestArtifacts, any Error> {
239+
) async -> Result<ExitTestArtifacts?, any Error> {
240240
guard let configuration = Configuration.current ?? Configuration.all.first else {
241241
preconditionFailure("A test must be running on the current task to use #expect(exitsWith:).")
242242
}

Sources/Testing/Expectations/ExpectationChecking+Macro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ public func __checkClosureCall(
11491149
isRequired: Bool,
11501150
isolation: isolated (any Actor)? = #isolation,
11511151
sourceLocation: SourceLocation
1152-
) async -> Result<ExitTestArtifacts, any Error> {
1152+
) async -> Result<ExitTestArtifacts?, any Error> {
11531153
await callExitTest(
11541154
exitsWith: expectedExitCondition,
11551155
observing: observedValues,

Sources/Testing/Support/Additions/ResultAdditions.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,32 @@ extension Result {
1515
/// `#require()` macros. Do not call it directly.
1616
@inlinable public func __expected() where Success == Void {}
1717

18+
/// Handle this instance as if it were returned from a call to `#require()`.
19+
///
20+
/// - Warning: This function is used to implement the `#expect()` and
21+
/// `#require()` macros. Do not call it directly.
22+
@inlinable public func __required() throws -> Success {
23+
try get()
24+
}
25+
}
26+
27+
// MARK: - Optional success values
28+
29+
extension Result {
1830
/// Handle this instance as if it were returned from a call to `#expect()`.
1931
///
2032
/// - Warning: This function is used to implement the `#expect()` and
2133
/// `#require()` macros. Do not call it directly.
22-
@inlinable public func __expected() -> Success? {
34+
@inlinable public func __expected<T>() -> Success where Success == T? {
2335
try? get()
2436
}
2537

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

0 commit comments

Comments
 (0)