Skip to content

Commit 40edfec

Browse files
authored
Refinements to IssueHandlingTrait SPI (#1121)
A few refinements to `IssueHandlingTrait`, which is still SPI. ### Motivation: Polish this SPI in anticipation of posting a pitch to promote it to public API soon. ### Modifications: - Expose a `handleIssue(_:)` instance method to allow more easily composing multiple issue handling traits and calling their underlying handler closure. This is conceptually similar to what was done for [ST-0010: Public API to evaluate ConditionTrait](https://github.com/swiftlang/swift-evolution/blob/main/proposals/testing/0010-evaluate-condition.md). - Refine the names of private decls. - Add `- Returns:` in DocC for places it's missing. ### 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 17c00b0 commit 40edfec

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

Sources/Testing/Traits/IssueHandlingTrait.swift

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,31 @@
2626
/// - ``Trait/filterIssues(_:)``
2727
@_spi(Experimental)
2828
public struct IssueHandlingTrait: TestTrait, SuiteTrait {
29-
/// A function which transforms an issue and returns an optional replacement.
29+
/// A function which handles an issue and returns an optional replacement.
3030
///
3131
/// - Parameters:
32-
/// - issue: The issue to transform.
32+
/// - issue: The issue to handle.
3333
///
3434
/// - Returns: An issue to replace `issue`, or else `nil` if the issue should
3535
/// not be recorded.
36-
fileprivate typealias Transformer = @Sendable (_ issue: Issue) -> Issue?
36+
fileprivate typealias Handler = @Sendable (_ issue: Issue) -> Issue?
3737

38-
/// This trait's transformer function.
39-
private var _transformer: Transformer
38+
/// This trait's handler function.
39+
private var _handler: Handler
4040

41-
fileprivate init(transformer: @escaping Transformer) {
42-
_transformer = transformer
41+
fileprivate init(handler: @escaping Handler) {
42+
_handler = handler
43+
}
44+
45+
/// Handle a specified issue.
46+
///
47+
/// - Parameters:
48+
/// - issue: The issue to handle.
49+
///
50+
/// - Returns: An issue to replace `issue`, or else `nil` if the issue should
51+
/// not be recorded.
52+
public func handleIssue(_ issue: Issue) -> Issue? {
53+
_handler(issue)
4354
}
4455

4556
public var isRecursive: Bool {
@@ -90,7 +101,7 @@ extension IssueHandlingTrait: TestScoping {
90101
// records new issues. This means only issue handling traits whose scope
91102
// is outside this one will be allowed to handle such issues.
92103
let newIssue = Configuration.withCurrent(oldConfiguration) {
93-
_transformer(issue)
104+
handleIssue(issue)
94105
}
95106

96107
if let newIssue {
@@ -113,6 +124,8 @@ extension Trait where Self == IssueHandlingTrait {
113124
/// this trait is applied to. It is passed a recorded issue, and returns
114125
/// an optional issue to replace the passed-in one.
115126
///
127+
/// - Returns: An instance of ``IssueHandlingTrait`` that transforms issues.
128+
///
116129
/// The `transformer` closure is called synchronously each time an issue is
117130
/// recorded by the test this trait is applied to. The closure is passed the
118131
/// recorded issue, and if it returns a non-`nil` value, that will be recorded
@@ -131,7 +144,7 @@ extension Trait where Self == IssueHandlingTrait {
131144
/// record new issues, although they will only be handled by issue handling
132145
/// traits which precede this trait or were inherited from a containing suite.
133146
public static func transformIssues(_ transformer: @escaping @Sendable (Issue) -> Issue?) -> Self {
134-
Self(transformer: transformer)
147+
Self(handler: transformer)
135148
}
136149

137150
/// Constructs a trait that filters issues recorded by a test.
@@ -142,6 +155,8 @@ extension Trait where Self == IssueHandlingTrait {
142155
/// should return `true` if the issue should be included, or `false` if it
143156
/// should be suppressed.
144157
///
158+
/// - Returns: An instance of ``IssueHandlingTrait`` that filters issues.
159+
///
145160
/// The `isIncluded` closure is called synchronously each time an issue is
146161
/// recorded by the test this trait is applied to. The closure is passed the
147162
/// recorded issue, and if it returns `true`, the issue will be preserved in

0 commit comments

Comments
 (0)