|
19 | 19 | /// - ``Trait/disabled(if:_:sourceLocation:)``
|
20 | 20 | /// - ``Trait/disabled(_:sourceLocation:_:)``
|
21 | 21 | public struct ConditionTrait: TestTrait, SuiteTrait {
|
| 22 | + /// An enumeration identifying the result of evaluating the condition. |
| 23 | + public enum Evaluation: Sendable { |
| 24 | + /// The condition succeeded. |
| 25 | + case success |
| 26 | + /// The condition failed, potentially returning a `Comment`. |
| 27 | + case failure(Comment?) |
| 28 | + } |
| 29 | + |
22 | 30 | /// An enumeration describing the kinds of conditions that can be represented
|
23 | 31 | /// by an instance of this type.
|
24 | 32 | enum Kind: Sendable {
|
@@ -79,19 +87,26 @@ public struct ConditionTrait: TestTrait, SuiteTrait {
|
79 | 87 |
|
80 | 88 | /// The source location where this trait was specified.
|
81 | 89 | public var sourceLocation: SourceLocation
|
82 |
| - |
83 |
| - public func prepare(for test: Test) async throws { |
84 |
| - let result: Bool |
85 |
| - var commentOverride: Comment? |
86 |
| - |
| 90 | + |
| 91 | + /// Returns the result of evaluating the condition. |
| 92 | + public func evaluate() async throws -> Evaluation { |
87 | 93 | switch kind {
|
88 | 94 | case let .conditional(condition):
|
89 |
| - (result, commentOverride) = try await condition() |
| 95 | + switch try await condition() { |
| 96 | + case (true, _): |
| 97 | + .success |
| 98 | + case (false, let comment): |
| 99 | + .failure(comment) |
| 100 | + } |
90 | 101 | case let .unconditional(unconditionalValue):
|
91 |
| - result = unconditionalValue |
| 102 | + unconditionalValue ? .success : .failure(nil) |
92 | 103 | }
|
| 104 | + } |
| 105 | + |
| 106 | + public func prepare(for test: Test) async throws { |
| 107 | + let result = try await evaluate() |
93 | 108 |
|
94 |
| - if !result { |
| 109 | + if case let .failure(commentOverride) = result { |
95 | 110 | // We don't need to consider including a backtrace here because it will
|
96 | 111 | // primarily contain frames in the testing library, not user code. If an
|
97 | 112 | // error was thrown by a condition evaluated above, the caller _should_
|
|
0 commit comments