Skip to content

Commit 1e65cf1

Browse files
authored
Fix how we check if a range expression covers one value. (#815)
My previous fix, #806, had the wrong logic to determine if a confirmation's expected count as-a-`RangeExpression` had exactly one value, so we were printing sub-optimal messages on test failures. Specifically, this: ```swift await confirmation(expectedCount: 1) { // transformed to 1...1 // fail to confirm } ``` Would report "1...1 time(s)" instead of "1 time". ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent cc2c42e commit 1e65cf1

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Sources/Testing/Issues/Issue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ extension Issue.Kind: CustomStringConvertible {
204204
// bound." That's sufficient for us to determine if the range contains
205205
// a single value.
206206
let upperBound = expected.first { $0 > lowerBound }
207-
if let upperBound, upperBound > lowerBound && lowerBound == upperBound - 1 {
207+
if upperBound == nil {
208208
return "Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(lowerBound.counting("time"))"
209209
}
210210
}

Tests/TestingTests/ConfirmationTests.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,36 @@ struct ConfirmationTests {
5151
var configuration = Configuration()
5252
configuration.eventHandler = { event, _ in
5353
if case let .issueRecorded(issue) = event.kind {
54-
#expect(String(describing: issue) != "")
54+
#expect(String(describing: issue).contains("time(s)"))
5555
}
5656
}
5757

5858
await Test {
5959
await confirmation(expectedCount: 1...) { _ in }
60+
await confirmation(expectedCount: 1...2) { _ in }
61+
await confirmation(expectedCount: 1..<3) { _ in }
62+
}.run(configuration: configuration)
63+
}
64+
65+
@Test func confirmationFailureCanBeDescribedAsSingleValue() async {
66+
var configuration = Configuration()
67+
configuration.eventHandler = { event, _ in
68+
if case let .issueRecorded(issue) = event.kind {
69+
#expect(!String(describing: issue).contains("time(s)"))
70+
}
71+
}
72+
73+
await Test {
74+
await confirmation(expectedCount: 1...1) { _ in }
75+
await confirmation(expectedCount: 1..<2) { _ in }
76+
await confirmation(expectedCount: Int.max...Int.max) { _ in }
77+
#if !SWT_NO_EXIT_TESTS
78+
await withKnownIssue("Crashes in Swift standard library (rdar://139568287)") {
79+
await #expect(exitsWith: .success) {
80+
await confirmation(expectedCount: Int.max...) { _ in }
81+
}
82+
}
83+
#endif
6084
}.run(configuration: configuration)
6185
}
6286

0 commit comments

Comments
 (0)