-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathIssue.swift
493 lines (445 loc) · 18.8 KB
/
Issue.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
/// A type describing a failure or warning which occurred during a test.
public struct Issue: Sendable {
/// Kinds of issues which may be recorded.
public enum Kind: Sendable {
/// An issue which occurred unconditionally, for example by using
/// ``Issue/record(_:sourceLocation:)``.
case unconditional
/// An issue due to a failed expectation, such as those produced by
/// ``expect(_:_:sourceLocation:)``.
///
/// - Parameters:
/// - expectation: The expectation that failed.
indirect case expectationFailed(_ expectation: Expectation)
/// An issue due to a confirmation being confirmed the wrong number of
/// times.
///
/// - Parameters:
/// - actual: The number of times ``Confirmation/confirm(count:)`` was
/// actually called.
/// - expected: The expected number of times
/// ``Confirmation/confirm(count:)`` should have been called.
///
/// This issue can occur when calling
/// ``confirmation(_:expectedCount:sourceLocation:_:)`` when the
/// confirmation passed to these functions' `body` closures is confirmed too
/// few or too many times.
indirect case confirmationMiscounted(actual: Int, expected: Int)
/// An issue due to an `Error` being thrown by a test function and caught by
/// the testing library.
///
/// - Parameters:
/// - error: The error which was associated with this issue.
indirect case errorCaught(_ error: any Error)
/// An issue due to a test reaching its time limit and timing out.
///
/// - Parameters:
/// - timeLimitComponents: The time limit reached by the test.
///
/// @Comment {
/// - Bug: The associated value of this enumeration case should be an
/// instance of `Duration`, but the testing library's deployment target
/// predates the introduction of that type.
/// }
indirect case timeLimitExceeded(timeLimitComponents: (seconds: Int64, attoseconds: Int64))
/// A known issue was expected, but was not recorded.
case knownIssueNotRecorded
/// An issue occurred due to misuse of the testing library.
case apiMisused
/// An issue due to a failure in the underlying system, not due to a failure
/// within the tests being run.
case system
/// A protocol describing additional context provided by an external tool or
/// library that recorded an issue of kind
/// ``Issue/Kind/recordedByTool(_:)``.
///
/// Test authors do not generally need to use this protocol. Rather, a tool
/// or library based on the testing library can use it to propagate
/// additional information about an issue to other layers of the testing
/// library's infrastructure.
///
/// A tool or library may conform as many types as it needs to this
/// protocol. Instances of types conforming to this protocol must be
/// encodable as JSON so that they can be included in event streams produced
/// by the testing library.
public protocol ToolContext: Sendable, Encodable {
/// The human-readable name of the tool that recorded the issue.
var toolName: String { get }
}
/// An issue recorded by an external tool or library that uses the testing
/// library.
///
/// - Parameters:
/// - toolContext: Any tool-specific context about the issue including the
/// name of the tool that recorded it.
@_spi(Experimental)
indirect case recordedByTool(_ toolContext: any ToolContext)
}
/// The kind of issue this value represents.
public var kind: Kind
/// Any comments provided by the developer and associated with this issue.
///
/// If no comment was supplied when the issue occurred, the value of this
/// property is the empty array.
public var comments: [Comment]
/// A ``SourceContext`` indicating where and how this issue occurred.
public var sourceContext: SourceContext
/// Whether or not this issue is known to occur.
public var isKnown = false
/// Initialize an issue instance with the specified details.
///
/// - Parameters:
/// - kind: The kind of issue this value represents.
/// - comments: An array of comments describing the issue. This array may be
/// empty.
/// - sourceContext: A ``SourceContext`` indicating where and how this issue
/// occurred. This defaults to a default source context returned by
/// calling ``SourceContext/init(backtrace:sourceLocation:)`` with zero
/// arguments.
init(
kind: Kind,
comments: [Comment],
sourceContext: SourceContext = .init()
) {
self.kind = kind
self.comments = comments
self.sourceContext = sourceContext
}
/// The error which was associated with this issue, if any.
///
/// The value of this property is non-`nil` when ``kind-swift.property`` is
/// ``Kind-swift.enum/errorCaught(_:)``.
public var error: (any Error)? {
if case let .errorCaught(error) = kind {
return error
}
return nil
}
/// The location in source where this issue occurred, if available.
public var sourceLocation: SourceLocation? {
get {
sourceContext.sourceLocation
}
set {
sourceContext.sourceLocation = newValue
}
}
}
// MARK: - CustomStringConvertible, CustomDebugStringConvertible
extension Issue: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
if comments.isEmpty {
return String(describing: kind)
}
let joinedComments = comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
if case let .recordedByTool(toolContext) = kind {
return "\(joinedComments) (from '\(toolContext.toolName)')"
} else {
return "\(kind): \(joinedComments)"
}
}
public var debugDescription: String {
if comments.isEmpty {
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? "")"
}
let joinedComments: String = comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? ""): \(joinedComments)"
}
}
extension Issue.Kind: CustomStringConvertible {
public var description: String {
switch self {
case .unconditional:
"Unconditionally failed"
case let .expectationFailed(expectation):
if let mismatchedErrorDescription = expectation.mismatchedErrorDescription {
"Expectation failed: \(mismatchedErrorDescription)"
} else {
"Expectation failed: \(expectation.evaluatedExpression.expandedDescription())"
}
case let .confirmationMiscounted(actual: actual, expected: expected):
"Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(expected.counting("time"))"
case let .errorCaught(error):
"Caught error: \(error)"
case let .timeLimitExceeded(timeLimitComponents: timeLimitComponents):
"Time limit was exceeded: \(TimeValue(timeLimitComponents))"
case .knownIssueNotRecorded:
"Known issue was not recorded"
case .apiMisused:
"An API was misused"
case .system:
"A system failure occurred"
case let .recordedByTool(toolContext):
"'\(toolContext.toolName)' recorded an issue"
}
}
}
// MARK: - Snapshotting
extension Issue {
/// A serializable type describing a failure or warning which occurred during a test.
@_spi(ForToolsIntegrationOnly)
public struct Snapshot: Sendable, Codable {
/// The kind of issue this value represents.
public var kind: Kind.Snapshot
/// Any comments provided by the developer and associated with this issue.
///
/// If no comment was supplied when the issue occurred, the value of this
/// property is the empty array.
public var comments: [Comment]
/// A ``SourceContext`` indicating where and how this issue occurred.
public var sourceContext: SourceContext
/// Whether or not this issue is known to occur.
public var isKnown = false
/// Initialize an issue instance with the specified details.
///
/// - 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
self.sourceContext = issue.sourceContext
self.isKnown = issue.isKnown
}
/// The error which was associated with this issue, if any.
///
/// The value of this property is non-`nil` when ``kind-swift.property`` is
/// ``Kind-swift.enum/errorCaught(_:)``.
public var error: (any Error)? {
if case let .errorCaught(error) = kind {
return error
}
return nil
}
/// The location in source where this issue occurred, if available.
public var sourceLocation: SourceLocation? {
get {
sourceContext.sourceLocation
}
set {
sourceContext.sourceLocation = newValue
}
}
}
}
extension Issue.Kind {
/// Serializable kinds of issues which may be recorded.
@_spi(ForToolsIntegrationOnly)
public enum Snapshot: Sendable, Codable {
/// An issue which occurred unconditionally, for example by using
/// ``Issue/record(_:sourceLocation:)``.
case unconditional
/// An issue due to a failed expectation, such as those produced by
/// ``expect(_:_:sourceLocation:)``.
///
/// - Parameters:
/// - expectation: The expectation that failed.
indirect case expectationFailed(_ expectation: Expectation.Snapshot)
/// An issue due to a confirmation being confirmed the wrong number of
/// times.
///
/// - Parameters:
/// - actual: The number of times ``Confirmation/confirm(count:)`` was
/// actually called.
/// - expected: The expected number of times
/// ``Confirmation/confirm(count:)`` should have been called.
///
/// This issue can occur when calling
/// ``confirmation(_:expectedCount:sourceLocation:_:)`` when the
/// confirmation passed to these functions' `body` closures is confirmed too
/// few or too many times.
indirect case confirmationMiscounted(actual: Int, expected: Int)
/// An issue due to an `Error` being thrown by a test function and caught by
/// the testing library.
///
/// - Parameters:
/// - error: A snapshot of the underlying error which was associated with
/// this issue.
indirect case errorCaught(_ error: ErrorSnapshot)
/// An issue due to a test reaching its time limit and timing out.
///
/// - Parameters:
/// - timeLimitComponents: The time limit reached by the test.
///
/// @Comment {
/// - Bug: The associated value of this enumeration case should be an
/// instance of `Duration`, but the testing library's deployment target
/// predates the introduction of that type.
/// }
indirect case timeLimitExceeded(timeLimitComponents: (seconds: Int64, attoseconds: Int64))
/// A known issue was expected, but was not recorded.
case knownIssueNotRecorded
/// An issue occurred due to misuse of the testing library.
case apiMisused
/// An issue due to a failure in the underlying system, not due to a failure
/// within the tests being run.
case system
/// Snapshots an ``Issue.Kind``.
/// - Parameter kind: The original ``Issue.Kind`` to snapshot.
public init(snapshotting kind: Issue.Kind) {
self = switch kind {
case .unconditional:
.unconditional
case let .expectationFailed(expectation):
.expectationFailed(Expectation.Snapshot(snapshotting: expectation))
case let .confirmationMiscounted(actual: actual, expected: expected):
.confirmationMiscounted(actual: actual, expected: expected)
case let .errorCaught(error):
.errorCaught(ErrorSnapshot(snapshotting: error))
case let .timeLimitExceeded(timeLimitComponents: timeLimitComponents):
.timeLimitExceeded(timeLimitComponents: timeLimitComponents)
case .knownIssueNotRecorded:
.knownIssueNotRecorded
case .apiMisused:
.apiMisused
case .system:
.system
case .recordedByTool:
.unconditional // TBD
}
}
/// The keys used to encode ``Issue.Kind``.
private enum _CodingKeys: CodingKey {
case unconditional
case expectationFailed
case confirmationMiscounted
case errorCaught
case timeLimitExceeded
case knownIssueNotRecorded
case apiMisused
case system
/// The keys used to encode ``Issue.Kind.expectationFailed``.
enum _ExpectationFailedKeys: CodingKey {
case expectation
}
/// The keys used to encode ``Issue.Kind.confirmationMiscount``.
enum _ConfirmationMiscountedKeys: CodingKey {
case actual
case expected
}
/// The keys used to encode``Issue.Kind.errorCaught``.
enum _ErrorCaughtKeys: CodingKey {
case error
}
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: _CodingKeys.self)
if try container.decodeIfPresent(Bool.self, forKey: .unconditional) != nil {
self = .unconditional
} else if let expectationFailedContainer = try? container.nestedContainer(keyedBy: _CodingKeys._ExpectationFailedKeys.self,
forKey: .expectationFailed) {
self = .expectationFailed(try expectationFailedContainer.decode(Expectation.Snapshot.self, forKey: .expectation))
} else if let confirmationMiscountedContainer = try? container.nestedContainer(keyedBy: _CodingKeys._ConfirmationMiscountedKeys.self,
forKey: .confirmationMiscounted) {
self = .confirmationMiscounted(actual: try confirmationMiscountedContainer.decode(Int.self,
forKey: .actual),
expected: try confirmationMiscountedContainer.decode(Int.self,
forKey: .expected))
} else if let errorCaught = try? container.nestedContainer(keyedBy: _CodingKeys._ErrorCaughtKeys.self,
forKey: .errorCaught) {
self = .errorCaught(try errorCaught.decode(ErrorSnapshot.self, forKey: .error))
} else if let timeLimit = try container.decodeIfPresent(TimeValue.self, forKey: .timeLimitExceeded) {
self = .timeLimitExceeded(timeLimitComponents: timeLimit.components)
} else if try container.decodeIfPresent(Bool.self, forKey: .knownIssueNotRecorded) != nil {
self = .knownIssueNotRecorded
} else if try container.decodeIfPresent(Bool.self, forKey: .apiMisused) != nil {
self = .apiMisused
} else if try container.decodeIfPresent(Bool.self, forKey: .system) != nil {
self = .system
} else {
throw DecodingError.valueNotFound(
Self.self,
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Value found did not match any of the existing cases for Issue.Kind."
)
)
}
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: _CodingKeys.self)
switch self {
case .unconditional:
try container.encode(true, forKey: .unconditional)
case let .expectationFailed(expectation):
var errorCaughtContainer = container.nestedContainer(keyedBy: _CodingKeys._ExpectationFailedKeys.self,
forKey: .expectationFailed)
try errorCaughtContainer.encode(expectation, forKey: .expectation)
case let .confirmationMiscounted(actual, expected):
var confirmationMiscountedContainer = container.nestedContainer(keyedBy: _CodingKeys._ConfirmationMiscountedKeys.self,
forKey: .confirmationMiscounted)
try confirmationMiscountedContainer.encode(actual, forKey: .actual)
try confirmationMiscountedContainer.encode(expected, forKey: .expected)
case let .errorCaught(error):
var errorCaughtContainer = container.nestedContainer(keyedBy: _CodingKeys._ErrorCaughtKeys.self, forKey: .errorCaught)
try errorCaughtContainer.encode(error, forKey: .error)
case let .timeLimitExceeded(timeLimitComponents):
try container.encode(TimeValue(timeLimitComponents), forKey: .timeLimitExceeded)
case .knownIssueNotRecorded:
try container.encode(true, forKey: .knownIssueNotRecorded)
case .apiMisused:
try container.encode(true, forKey: .apiMisused)
case .system:
try container.encode(true, forKey: .system)
}
}
}
}
// MARK: - Snapshot CustomStringConvertible, CustomDebugStringConvertible
extension Issue.Snapshot: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
if comments.isEmpty {
return String(describing: kind)
}
let joinedComments = comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
return "\(kind): \(joinedComments)"
}
public var debugDescription: String {
if comments.isEmpty {
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? "")"
}
let joinedComments: String = comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? ""): \(joinedComments)"
}
}
extension Issue.Kind.Snapshot: CustomStringConvertible {
public var description: String {
switch self {
case .unconditional:
"Unconditionally failed"
case let .expectationFailed(expectation):
if let mismatchedErrorDescription = expectation.mismatchedErrorDescription {
"Expectation failed: \(mismatchedErrorDescription)"
} else {
"Expectation failed: \(expectation.evaluatedExpression.expandedDescription())"
}
case let .confirmationMiscounted(actual: actual, expected: expected):
"Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(expected.counting("time"))"
case let .errorCaught(error):
"Caught error: \(error)"
case let .timeLimitExceeded(timeLimitComponents: timeLimitComponents):
"Time limit was exceeded: \(TimeValue(timeLimitComponents))"
case .knownIssueNotRecorded:
"Known issue was not recorded"
case .apiMisused:
"An API was misused"
case .system:
"A system failure occurred"
}
}
}