|
| 1 | +# Public API to evaluate ConditionTrait |
| 2 | + |
| 3 | +* Proposal: [ST-0010](0010-evaluate-condition.md) |
| 4 | +* Authors: [David Catmull](https://github.com/Uncommon) |
| 5 | +* Review Manager: [Stuart Montgomery](https://github.com/stmontgomery) |
| 6 | +* Status: **Active review (April 11...April 22, 2025)** |
| 7 | +* Bug: [swiftlang/swift-testing#903](https://github.com/swiftlang/swift-testing/issues/903) |
| 8 | +* Implementation: [swiftlang/swift-testing#909](https://github.com/swiftlang/swift-testing/pull/909) |
| 9 | +* Review: ([pitch](https://forums.swift.org/t/pitch-introduce-conditiontrait-evaluate/77242)) |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +This adds an `evaluate()` method to `ConditionTrait` to evaluate the condition |
| 14 | +without requiring a `Test` instance. |
| 15 | + |
| 16 | +## Motivation |
| 17 | + |
| 18 | +Currently, the only way a `ConditionTrait` is evaluated is inside the |
| 19 | +`prepare(for:)` method. This makes it difficult for third-party libraries to |
| 20 | +utilize these traits because evaluating a condition would require creating a |
| 21 | +dummy `Test` to pass to that method. |
| 22 | + |
| 23 | +## Proposed solution |
| 24 | + |
| 25 | +The proposal is to add a `ConditionTrait.evaluate()` method which returns the |
| 26 | +result of the evaluation. The existing `prepare(for:)` method is updated to call |
| 27 | +`evaluate()` so that the logic is not duplicated. |
| 28 | + |
| 29 | +## Detailed design |
| 30 | + |
| 31 | +The `evaluate()` method is as follows, containing essentially the same logic |
| 32 | +as was in `prepare(for:)`: |
| 33 | + |
| 34 | +```swift |
| 35 | +extension ConditionTrait { |
| 36 | + /// Evaluate this instance's underlying condition. |
| 37 | + /// |
| 38 | + /// - Returns: The result of evaluating this instance's underlying condition. |
| 39 | + /// |
| 40 | + /// The evaluation is performed each time this function is called, and is not |
| 41 | + /// cached. |
| 42 | + public func evaluate() async throws -> Bool |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Source compatibility |
| 47 | + |
| 48 | +This change is purely additive. |
| 49 | + |
| 50 | +## Integration with supporting tools |
| 51 | + |
| 52 | +This change allows third-party libraries to apply condition traits at other |
| 53 | +levels than suites or whole test functions, for example if tests are broken up |
| 54 | +into smaller sections. |
| 55 | + |
| 56 | +## Future directions |
| 57 | + |
| 58 | +This change seems sufficient for third party libraries to make use of |
| 59 | +`ConditionTrait`. Changes for other traits can be tackled in separate proposals. |
| 60 | + |
| 61 | +## Alternatives considered |
| 62 | + |
| 63 | +Exposing `ConditionTrait.Kind` and `.kind` was also considered, but it seemed |
| 64 | +unnecessary to go that far, and it would encourage duplicating the logic that |
| 65 | +already exists in `prepare(for:)`. |
0 commit comments