Skip to content

Commit abcce80

Browse files
committed
Simplify testCases property on Test.Snapshot
1 parent c4387af commit abcce80

File tree

1 file changed

+19
-70
lines changed

1 file changed

+19
-70
lines changed

Sources/Testing/Test.swift

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public struct Test: Sendable {
221221
self.sourceLocation = sourceLocation
222222
self.containingTypeInfo = containingTypeInfo
223223
self.xcTestCompatibleSelector = xcTestCompatibleSelector
224-
self.testCaseState = .unevaluated({ .init(try await testCases()) })
224+
self.testCaseState = .unevaluated { .init(try await testCases()) }
225225
self.parameters = parameters
226226
}
227227
}
@@ -250,7 +250,7 @@ extension Test {
250250
case name
251251
case displayName
252252
case sourceLocation
253-
case testCaseState
253+
case testCases
254254
case parameters
255255
case comments
256256
case tags
@@ -302,11 +302,13 @@ extension Test {
302302
}
303303
}
304304

305-
/// The evaluation state of this test's cases, if any.
305+
/// The set of test cases associated with this test, if any.
306306
///
307-
/// If this test represents a suite type, the value of this property is
308-
/// `nil`.
309-
public var testCaseState: TestCaseState?
307+
/// If the ``Test`` this instance was snapshotted from represented a
308+
/// parameterized test function but its test cases had not yet been
309+
/// evaluated when the snapshot was taken, or the evaluation attempt failed,
310+
/// the value of this property will be an empty array.
311+
public var testCases: [Test.Case.Snapshot]?
310312

311313
/// The test function parameters, if any.
312314
///
@@ -345,14 +347,23 @@ extension Test {
345347
name = test.name
346348
displayName = test.displayName
347349
sourceLocation = test.sourceLocation
348-
testCaseState = test.testCaseState.map(TestCaseState.init(snapshotting:))
349350
parameters = test.parameters
350351
comments = test.comments
351352
tags = test.tags
352353
associatedBugs = test.associatedBugs
353354
if #available(_clockAPI, *) {
354355
_timeLimit = test.timeLimit.map(TimeValue.init)
355356
}
357+
358+
testCases = switch test.testCaseState {
359+
case .unevaluated,
360+
.evaluated(.failure):
361+
[]
362+
case let .evaluated(.success(testCases)):
363+
testCases.rawValue.map(Test.Case.Snapshot.init(snapshotting:))
364+
case nil:
365+
nil
366+
}
356367
}
357368

358369
/// Whether or not this test is parameterized.
@@ -370,69 +381,7 @@ extension Test {
370381
///
371382
/// - ``Test/isSuite``
372383
public var isSuite: Bool {
373-
testCaseState == nil
374-
}
375-
}
376-
}
377-
378-
extension Test.Snapshot.TestCaseState: Codable {
379-
/// A simplified version of ``TestCaseState`` suitable for encoding and
380-
/// decoding.
381-
private enum _EncodedForm: Sendable, Codable {
382-
/// The test's cases have not yet been evaluated.
383-
case unevaluated
384-
385-
/// A representation of `Swift.Result` suitable for encoding and decoding.
386-
enum Result: Sendable, Codable {
387-
/// A successful evaluation of a test's cases.
388-
///
389-
/// - Parameters:
390-
/// - testCases: The returned set of test cases.
391-
case success(_ testCases: [Test.Case.Snapshot])
392-
393-
/// A failed evaluation of a test's cases.
394-
///
395-
/// - Parameters:
396-
/// - error: A snapshot of the error caught when evaluating the test's
397-
/// cases.
398-
case failure(_ error: ErrorSnapshot)
399-
}
400-
401-
/// The test's cases have been evaluated, and either returned a set of test
402-
/// or failed by throwing an error.
403-
///
404-
/// - Parameters:
405-
/// - result: The result of having evaluated the test's cases.
406-
case evaluated(_ result: Result)
407-
}
408-
409-
public init(from decoder: any Decoder) throws {
410-
self = switch try _EncodedForm(from: decoder) {
411-
case .unevaluated:
412-
.unevaluated
413-
case let .evaluated(result):
414-
switch result {
415-
case let .success(testCases):
416-
.evaluated(.success(testCases))
417-
case let .failure(error):
418-
.evaluated(.failure(error))
419-
}
384+
testCases == nil
420385
}
421386
}
422-
423-
public func encode(to encoder: any Encoder) throws {
424-
let encodedForm: _EncodedForm = switch self {
425-
case .unevaluated:
426-
.unevaluated
427-
case let .evaluated(result):
428-
switch result {
429-
case let .success(testCases):
430-
.evaluated(.success(testCases))
431-
case let .failure(error):
432-
.evaluated(.failure(error))
433-
}
434-
}
435-
436-
try encodedForm.encode(to: encoder)
437-
}
438387
}

0 commit comments

Comments
 (0)