Skip to content

Implement an overload of confirmation() that takes an unbounded range. #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Sources/Testing/Issues/Confirmation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
//

/// A type that can be used to confirm that an event occurs zero or more times.
public struct Confirmation: Sendable {
public final class Confirmation: Sendable {
/// The number of times ``confirm(count:)`` has been called.
///
/// This property is fileprivate because it may be mutated asynchronously and
/// callers may be tempted to use it in ways that result in data races.
fileprivate var count = Locked(rawValue: 0)
fileprivate let count = Locked(rawValue: 0)

/// Confirm this confirmation.
///
Expand Down Expand Up @@ -179,6 +179,22 @@ public func confirmation<R>(
return try await body(confirmation)
}

/// An overload of ``confirmation(_:expectedCount:sourceLocation:_:)-9bfdc``
/// that handles the unbounded range operator (`...`).
///
/// This overload is necessary because `UnboundedRange` does not conform to
/// `RangeExpression`. It effectively always succeeds because any number of
/// confirmations matches, so it is marked unavailable and is not implemented.
@available(*, unavailable, message: "Unbounded range '...' has no effect when used with a confirmation.")
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: UnboundedRange,
sourceLocation: SourceLocation = #_sourceLocation,
_ body: (Confirmation) async throws -> R
) async rethrows -> R {
fatalError("Unsupported")
}

@_spi(Experimental)
extension Confirmation {
/// A protocol that describes a range expression that can be used with
Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Support/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ enum Environment {
return nil
case let errorCode:
let error = Win32Error(rawValue: errorCode)
fatalError("unexpected error when getting environment variable '\(name)': \(error) (\(errorCode))")
fatalError("Unexpected error when getting environment variable '\(name)': \(error) (\(errorCode))")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by formatting tweak.

}
} else if count > buffer.count {
// Try again with the larger count.
Expand Down
2 changes: 1 addition & 1 deletion Sources/TestingMacros/Support/AvailabilityGuards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private func _createAvailabilityTraitExpr(
return ".__unavailable(message: \(message), sourceLocation: \(sourceLocationExpr))"

default:
fatalError("Unsupported keyword \(whenKeyword) passed to \(#function)")
fatalError("Unsupported keyword \(whenKeyword) passed to \(#function). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by amendment to fatal error message.

}
}

Expand Down