Skip to content

Commit 1d28fa4

Browse files
authored
Add ConditionTrait.evaluate() (#909)
Add `ConditionTrait.evaluate()` so that a condition can be evaluated independent of a `Test` object. ### Motivation: Currently, the only way a `ConditionTrait` is evaluated is inside the `prepare(for:)` method. This makes it difficult and awkward for third-party libraries to utilize these traits because evaluating a condition would require creating a dummy `Test` to pass to that method. ### Modifications: Add `ConditionTrait.evaluate()`, and `ConditionTrait.Evaluation` enum for the return value. ### Result: Public API allows for evaluating a `ConditionTrait` in any context. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 7ccbd68 commit 1d28fa4

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Sources/Testing/Traits/ConditionTrait.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,27 @@ public struct ConditionTrait: TestTrait, SuiteTrait {
6262

6363
/// The source location where this trait is specified.
6464
public var sourceLocation: SourceLocation
65-
66-
public func prepare(for test: Test) async throws {
67-
let result = switch kind {
65+
66+
/// Evaluate this instance's underlying condition.
67+
///
68+
/// - Returns: The result of evaluating this instance's underlying condition.
69+
///
70+
/// The evaluation is performed each time this function is called, and is not
71+
/// cached.
72+
@_spi(Experimental)
73+
public func evaluate() async throws -> Bool {
74+
switch kind {
6875
case let .conditional(condition):
6976
try await condition()
7077
case let .unconditional(unconditionalValue):
7178
unconditionalValue
7279
}
80+
}
81+
82+
public func prepare(for test: Test) async throws {
83+
let isEnabled = try await evaluate()
7384

74-
if !result {
85+
if !isEnabled {
7586
// We don't need to consider including a backtrace here because it will
7687
// primarily contain frames in the testing library, not user code. If an
7788
// error was thrown by a condition evaluated above, the caller _should_

Tests/TestingTests/Traits/ConditionTraitTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
//
1010

11-
@testable import Testing
11+
@testable @_spi(Experimental) import Testing
1212

1313
@Suite("Condition Trait Tests", .tags(.traitRelated))
1414
struct ConditionTraitTests {
@@ -41,4 +41,22 @@ struct ConditionTraitTests {
4141
.disabled(if: false)
4242
)
4343
func disabledTraitIf() throws {}
44+
45+
@Test
46+
func evaluateCondition() async throws {
47+
let trueUnconditional = ConditionTrait(kind: .unconditional(true), comments: [], sourceLocation: #_sourceLocation)
48+
let falseUnconditional = ConditionTrait.disabled()
49+
let enabledTrue = ConditionTrait.enabled(if: true)
50+
let enabledFalse = ConditionTrait.enabled(if: false)
51+
var result: Bool
52+
53+
result = try await trueUnconditional.evaluate()
54+
#expect(result)
55+
result = try await falseUnconditional.evaluate()
56+
#expect(!result)
57+
result = try await enabledTrue.evaluate()
58+
#expect(result)
59+
result = try await enabledFalse.evaluate()
60+
#expect(!result)
61+
}
4462
}

0 commit comments

Comments
 (0)