Skip to content

Commit e0e3a82

Browse files
committed
Add ConditionTrait.evaluate()
1 parent 52aa355 commit e0e3a82

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

Sources/Testing/Traits/ConditionTrait.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
/// - ``Trait/disabled(if:_:sourceLocation:)``
2020
/// - ``Trait/disabled(_:sourceLocation:_:)``
2121
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+
2230
/// An enumeration describing the kinds of conditions that can be represented
2331
/// by an instance of this type.
2432
enum Kind: Sendable {
@@ -79,19 +87,26 @@ public struct ConditionTrait: TestTrait, SuiteTrait {
7987

8088
/// The source location where this trait was specified.
8189
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 {
8793
switch kind {
8894
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+
}
90101
case let .unconditional(unconditionalValue):
91-
result = unconditionalValue
102+
unconditionalValue ? .success : .failure(nil)
92103
}
104+
}
105+
106+
public func prepare(for test: Test) async throws {
107+
let result = try await evaluate()
93108

94-
if !result {
109+
if case let .failure(commentOverride) = result {
95110
// We don't need to consider including a backtrace here because it will
96111
// primarily contain frames in the testing library, not user code. If an
97112
// error was thrown by a condition evaluated above, the caller _should_

0 commit comments

Comments
 (0)