Skip to content

Fix a crash when describing a confirmation failure with a range. #806

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 1 commit into from
Nov 4, 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
16 changes: 10 additions & 6 deletions Sources/Testing/Issues/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,9 @@ extension Issue: CustomStringConvertible, CustomDebugStringConvertible {
/// In the future, when our minimum deployment target supports casting a value
/// to a constrained existential type ([SE-0353](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md#effect-on-abi-stability)),
/// we can remove this protocol and cast to `RangeExpression<Int>` instead.
private protocol _RangeExpressionOverIntValues: RangeExpression where Bound == Int {}
private protocol _RangeExpressionOverIntValues: RangeExpression & Sequence where Bound == Int, Element == Int {}
extension ClosedRange<Int>: _RangeExpressionOverIntValues {}
extension PartialRangeFrom<Int>: _RangeExpressionOverIntValues {}
extension PartialRangeThrough<Int>: _RangeExpressionOverIntValues {}
extension PartialRangeUpTo<Int>: _RangeExpressionOverIntValues {}
extension Range<Int>: _RangeExpressionOverIntValues {}

extension Issue.Kind: CustomStringConvertible {
Expand All @@ -200,9 +198,15 @@ extension Issue.Kind: CustomStringConvertible {
}
case let .confirmationMiscounted(actual: actual, expected: expected):
if let expected = expected as? any _RangeExpressionOverIntValues {
let expected = expected.relative(to: [])
if expected.upperBound > expected.lowerBound && expected.lowerBound == expected.upperBound - 1 {
return "Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(expected.lowerBound.counting("time"))"
let lowerBound = expected.first { _ in true }
if let lowerBound {
// Not actually an upper bound, just "any value greater than the lower
// bound." That's sufficient for us to determine if the range contains
// a single value.
let upperBound = expected.first { $0 > lowerBound }
if let upperBound, upperBound > lowerBound && lowerBound == upperBound - 1 {
return "Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(lowerBound.counting("time"))"
}
}
}
return "Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(String(describingForTest: expected)) time(s)"
Expand Down
13 changes: 13 additions & 0 deletions Tests/TestingTests/ConfirmationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ struct ConfirmationTests {
}
}

@Test func confirmationFailureCanBeDescribed() async {
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case let .issueRecorded(issue) = event.kind {
#expect(String(describing: issue) != "")
}
}

await Test {
await confirmation(expectedCount: 1...) { _ in }
}.run(configuration: configuration)
}

#if !SWT_NO_EXIT_TESTS
@Test("Confirmation requires positive count")
func positiveCount() async {
Expand Down