Skip to content

Commit 74c4286

Browse files
Add CustomStringConvertible and CustomDebugStringConvertible to Issue.Snapshot and Issue.Kind.Snapshot (#359)
Make `Issue.Snapshot` and `Issue.Kind.Snapshot` conform to `CustomStringConvertible` and `CustomDebugStringConvertible` using the same implementation as their original types. Resolves rdar://126646993
1 parent e07ef28 commit 74c4286

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

Sources/Testing/Issues/Issue.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,54 @@ extension Issue.Kind {
405405
}
406406
}
407407
}
408+
409+
// MARK: - Snapshot CustomStringConvertible, CustomDebugStringConvertible
410+
411+
extension Issue.Snapshot: CustomStringConvertible, CustomDebugStringConvertible {
412+
public var description: String {
413+
if comments.isEmpty {
414+
return String(describing: kind)
415+
}
416+
let joinedComments = comments.lazy
417+
.map(\.rawValue)
418+
.joined(separator: "\n")
419+
return "\(kind): \(joinedComments)"
420+
}
421+
422+
public var debugDescription: String {
423+
if comments.isEmpty {
424+
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? "")"
425+
}
426+
let joinedComments: String = comments.lazy
427+
.map(\.rawValue)
428+
.joined(separator: "\n")
429+
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? ""): \(joinedComments)"
430+
}
431+
}
432+
433+
extension Issue.Kind.Snapshot: CustomStringConvertible {
434+
public var description: String {
435+
switch self {
436+
case .unconditional:
437+
"Unconditionally failed"
438+
case let .expectationFailed(expectation):
439+
if let mismatchedErrorDescription = expectation.mismatchedErrorDescription {
440+
"Expectation failed: \(mismatchedErrorDescription)"
441+
} else {
442+
"Expectation failed: \(expectation.evaluatedExpression.expandedDescription())"
443+
}
444+
case let .confirmationMiscounted(actual: actual, expected: expected):
445+
"Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(expected.counting("time"))"
446+
case let .errorCaught(error):
447+
"Caught error: \(error)"
448+
case let .timeLimitExceeded(timeLimitComponents: timeLimitComponents):
449+
"Time limit was exceeded: \(TimeValue(timeLimitComponents))"
450+
case .knownIssueNotRecorded:
451+
"Known issue was not recorded"
452+
case .apiMisused:
453+
"An API was misused"
454+
case .system:
455+
"A system failure occurred"
456+
}
457+
}
458+
}

Tests/TestingTests/IssueTests.swift

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,22 +1390,26 @@ final class IssueTests: XCTestCase {
13901390
@Suite("Issue Codable Conformance Tests")
13911391
struct IssueCodingTests {
13921392

1393+
private static let issueKinds: [Issue.Kind] = [
1394+
Issue.Kind.apiMisused,
1395+
Issue.Kind.confirmationMiscounted(actual: 13, expected: 42),
1396+
Issue.Kind.errorCaught(NSError(domain: "Domain", code: 13, userInfo: ["UserInfoKey": "UserInfoValue"])),
1397+
Issue.Kind.expectationFailed(Expectation(evaluatedExpression: .__fromSyntaxNode("abc"), isPassing: true, isRequired: true, sourceLocation: SourceLocation())),
1398+
Issue.Kind.knownIssueNotRecorded,
1399+
Issue.Kind.system,
1400+
Issue.Kind.timeLimitExceeded(timeLimitComponents: (13, 42)),
1401+
Issue.Kind.unconditional,
1402+
]
1403+
13931404
@Test("Codable",
1394-
arguments: [
1395-
Issue.Kind.apiMisused,
1396-
Issue.Kind.confirmationMiscounted(actual: 13, expected: 42),
1397-
Issue.Kind.errorCaught(NSError(domain: "Domain", code: 13, userInfo: ["UserInfoKey": "UserInfoValue"])),
1398-
Issue.Kind.expectationFailed(Expectation(evaluatedExpression: .__fromSyntaxNode("abc"), isPassing: true, isRequired: true, sourceLocation: SourceLocation())),
1399-
Issue.Kind.knownIssueNotRecorded,
1400-
Issue.Kind.system,
1401-
Issue.Kind.timeLimitExceeded(timeLimitComponents: (13, 42)),
1402-
Issue.Kind.unconditional,
1403-
]
1405+
arguments: issueKinds
14041406
)
14051407
func testCodable(issueKind: Issue.Kind) async throws {
1406-
let issue = Issue(kind: issueKind,
1407-
comments: ["Comment"],
1408-
sourceContext: SourceContext(backtrace: Backtrace.current(), sourceLocation: SourceLocation()))
1408+
let issue = Issue(
1409+
kind: issueKind,
1410+
comments: ["Comment"],
1411+
sourceContext: SourceContext(backtrace: Backtrace.current(), sourceLocation: SourceLocation())
1412+
)
14091413
let issueSnapshot = Issue.Snapshot(snapshotting: issue)
14101414
let decoded = try JSON.encodeAndDecode(issueSnapshot)
14111415

@@ -1479,4 +1483,19 @@ struct IssueCodingTests {
14791483
#expect(issueSnapshot.sourceLocation == updatedSourceLocation)
14801484
#expect(issueSnapshot.sourceContext.sourceLocation == updatedSourceLocation)
14811485
}
1486+
1487+
@Test("Custom descriptions are the same",
1488+
arguments: issueKinds
1489+
)
1490+
func customDescription(issueKind: Issue.Kind) async throws {
1491+
let issue = Issue(
1492+
kind: issueKind,
1493+
comments: ["Comment"],
1494+
sourceContext: SourceContext(backtrace: Backtrace.current(), sourceLocation: SourceLocation())
1495+
)
1496+
let issueSnapshot = Issue.Snapshot(snapshotting: issue)
1497+
1498+
#expect(String(describing: issueSnapshot) == String(describing: issue))
1499+
#expect(String(reflecting: issueSnapshot) == String(reflecting: issue))
1500+
}
14821501
}

0 commit comments

Comments
 (0)