Skip to content

Remove the Confirmation.ExpectedCount marker protocol. #689

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
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 2 additions & 32 deletions Sources/Testing/Issues/Confirmation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public func confirmation<R>(
@_spi(Experimental)
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: some Confirmation.ExpectedCount,
expectedCount: some RangeExpression<Int> & Sendable,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation = #_sourceLocation,
_ body: (Confirmation) async throws -> sending R
Expand Down Expand Up @@ -200,22 +200,7 @@ public func confirmation<R>(
fatalError("Unsupported")
}

@_spi(Experimental)
extension Confirmation {
/// A protocol that describes a range expression that can be used with
/// ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-9rt6m``.
///
/// This protocol represents any expression that describes a range of
/// confirmation counts. For example, the expression `1 ..< 10` automatically
/// conforms to it.
///
/// You do not generally need to add conformances to this type yourself. It is
/// used by the testing library to abstract away the different range types
/// provided by the Swift standard library.
public protocol ExpectedCount: Sendable, RangeExpression<Int> {}
}

extension Confirmation.ExpectedCount {
extension RangeExpression where Bound == Int, Self: Sendable {
/// Get an instance of ``Issue/Kind-swift.enum`` corresponding to this value.
///
/// - Parameters:
Expand All @@ -233,18 +218,3 @@ extension Confirmation.ExpectedCount {
}
}
}

@_spi(Experimental)
extension ClosedRange<Int>: Confirmation.ExpectedCount {}

@_spi(Experimental)
extension PartialRangeFrom<Int>: Confirmation.ExpectedCount {}

@_spi(Experimental)
extension PartialRangeThrough<Int>: Confirmation.ExpectedCount {}

@_spi(Experimental)
extension PartialRangeUpTo<Int>: Confirmation.ExpectedCount {}

@_spi(Experimental)
extension Range<Int>: Confirmation.ExpectedCount {}
12 changes: 9 additions & 3 deletions Sources/Testing/Issues/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct Issue: Sendable {
/// the confirmation passed to these functions' `body` closures is confirmed
/// too few or too many times.
@_spi(Experimental)
indirect case confirmationOutOfRange(actual: Int, expected: any Confirmation.ExpectedCount)
indirect case confirmationOutOfRange(actual: Int, expected: any RangeExpression & Sendable)

/// An issue due to an `Error` being thrown by a test function and caught by
/// the testing library.
Expand Down Expand Up @@ -227,8 +227,14 @@ extension Issue {
///
/// - Parameter issue: The original issue that gets snapshotted.
public init(snapshotting issue: borrowing Issue) {
self.kind = Issue.Kind.Snapshot(snapshotting: issue.kind)
self.comments = issue.comments
if case .confirmationOutOfRange = issue.kind {
// Work around poor stringification of this issue kind in Xcode 16.
self.kind = .unconditional
self.comments = CollectionOfOne("\(issue.kind)") + issue.comments
} else {
self.kind = Issue.Kind.Snapshot(snapshotting: issue.kind)
self.comments = issue.comments
}
self.sourceContext = issue.sourceContext
self.isKnown = issue.isKnown
}
Expand Down
16 changes: 14 additions & 2 deletions Tests/TestingTests/ConfirmationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,28 @@ struct UnsuccessfulConfirmationTests {
}

@Test(.hidden, arguments: [
1 ... 2 as any Confirmation.ExpectedCount,
1 ... 2 as any ExpectedCount,
1 ..< 2,
1 ..< 3,
..<2,
...2,
999...,
])
func confirmedOutOfRange(_ range: any Confirmation.ExpectedCount) async {
func confirmedOutOfRange(_ range: any ExpectedCount) async {
await confirmation(expectedCount: range) { (thingHappened) async in
thingHappened(count: 3)
}
}
}

// MARK: -

/// Needed since we don't have generic test functions, so we need a concrete
/// argument type for `confirmedOutOfRange(_:)`, but we can't write
/// `any RangeExpression<Int> & Sendable`. ([96960993](rdar://96960993))
protocol ExpectedCount: RangeExpression, Sendable where Bound == Int {}
extension ClosedRange<Int>: ExpectedCount {}
extension PartialRangeFrom<Int>: ExpectedCount {}
extension PartialRangeThrough<Int>: ExpectedCount {}
extension PartialRangeUpTo<Int>: ExpectedCount {}
extension Range<Int>: ExpectedCount {}