|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024–2025 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +/// A protocol that provides instances of conforming types with the ability to |
| 12 | +/// record themselves as test issues. |
| 13 | +/// |
| 14 | +/// When a type conforms to this protocol, values of that type can be passed to |
| 15 | +/// ``Issue/record(_:_:)``. The testing library then calls the |
| 16 | +/// ``customize(_:)`` function and passes it an instance of ``Issue`` that will |
| 17 | +/// be used to represent the value. The function can then reconfigure or replace |
| 18 | +/// the issue as needed. |
| 19 | +/// |
| 20 | +/// This protocol may become part of the testing library's public interface in |
| 21 | +/// the future. There's not really anything _requiring_ it to conform to `Error` |
| 22 | +/// but all our current use cases are error types. If we want to allow other |
| 23 | +/// types to be represented as issues, we will need to add an overload of |
| 24 | +/// `Issue.record()` that takes `some CustomIssueRepresentable`. |
| 25 | +protocol CustomIssueRepresentable: Error { |
| 26 | + /// Customize the issue that will represent this value. |
| 27 | + /// |
| 28 | + /// - Parameters: |
| 29 | + /// - issue: The issue to customize. The function consumes this value. |
| 30 | + /// |
| 31 | + /// - Returns: A customized copy of `issue`. |
| 32 | + func customize(_ issue: consuming Issue) -> Issue |
| 33 | +} |
| 34 | + |
| 35 | +// MARK: - Internal error types |
| 36 | + |
| 37 | +/// A type representing an error in the testing library or its underlying |
| 38 | +/// infrastructure. |
| 39 | +/// |
| 40 | +/// When an error of this type is thrown and caught by the testing library, it |
| 41 | +/// is recorded as an issue of kind ``Issue/Kind/system`` rather than |
| 42 | +/// ``Issue/Kind/errorCaught(_:)``. |
| 43 | +/// |
| 44 | +/// This type is not part of the public interface of the testing library. |
| 45 | +/// External callers should generally record issues by throwing their own errors |
| 46 | +/// or by calling ``Issue/record(_:sourceLocation:)``. |
| 47 | +struct SystemError: Error, CustomStringConvertible, CustomIssueRepresentable { |
| 48 | + var description: String |
| 49 | + |
| 50 | + func customize(_ issue: consuming Issue) -> Issue { |
| 51 | + issue.kind = .system |
| 52 | + issue.comments.append("\(self)") |
| 53 | + return issue |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// A type representing misuse of testing library API. |
| 58 | +/// |
| 59 | +/// When an error of this type is thrown and caught by the testing library, it |
| 60 | +/// is recorded as an issue of kind ``Issue/Kind/apiMisused`` rather than |
| 61 | +/// ``Issue/Kind/errorCaught(_:)``. |
| 62 | +/// |
| 63 | +/// This type is not part of the public interface of the testing library. |
| 64 | +/// External callers should generally record issues by throwing their own errors |
| 65 | +/// or by calling ``Issue/record(_:sourceLocation:)``. |
| 66 | +struct APIMisuseError: Error, CustomStringConvertible, CustomIssueRepresentable { |
| 67 | + var description: String |
| 68 | + |
| 69 | + func customize(_ issue: consuming Issue) -> Issue { |
| 70 | + issue.kind = .apiMisused |
| 71 | + issue.comments.append("\(self)") |
| 72 | + return issue |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +extension ExpectationFailedError: CustomIssueRepresentable { |
| 77 | + func customize(_ issue: consuming Issue) -> Issue { |
| 78 | + // Instances of this error type are thrown by `#require()` after an issue of |
| 79 | + // kind `.expectationFailed` has already been recorded. Code that rethrows |
| 80 | + // this error does not generate a new issue, but code that passes this error |
| 81 | + // to Issue.record() is misbehaving. |
| 82 | + issue.kind = .apiMisused |
| 83 | + issue.comments.append("Recorded an error of type \(Self.self) representing an expectation that failed and was already recorded: \(expectation)") |
| 84 | + return issue |
| 85 | + } |
| 86 | +} |
0 commit comments