-
Notifications
You must be signed in to change notification settings - Fork 112
Add a (internal-only) CustomIssueRepresentable
protocol.
#945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024–2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
// | ||
|
||
/// A protocol that provides instances of conforming types with the ability to | ||
/// record themselves as test issues. | ||
/// | ||
/// When a type conforms to this protocol, values of that type can be passed to | ||
/// ``Issue/record(_:_:)``. The testing library then calls the | ||
/// ``customize(_:)`` function and passes it an instance of ``Issue`` that will | ||
/// be used to represent the value. The function can then reconfigure or replace | ||
/// the issue as needed. | ||
/// | ||
/// This protocol may become part of the testing library's public interface in | ||
/// the future. There's not really anything _requiring_ it to conform to `Error` | ||
/// but all our current use cases are error types. If we want to allow other | ||
/// types to be represented as issues, we will need to add an overload of | ||
/// `Issue.record()` that takes `some CustomIssueRepresentable`. | ||
protocol CustomIssueRepresentable: Error { | ||
/// Customize the issue that will represent this value. | ||
/// | ||
/// - Parameters: | ||
/// - issue: The issue to customize. The function consumes this value. | ||
/// | ||
/// - Returns: A customized copy of `issue`. | ||
func customize(_ issue: consuming Issue) -> Issue | ||
} | ||
|
||
// MARK: - Internal error types | ||
|
||
/// A type representing an error in the testing library or its underlying | ||
/// infrastructure. | ||
/// | ||
/// When an error of this type is thrown and caught by the testing library, it | ||
/// is recorded as an issue of kind ``Issue/Kind/system`` rather than | ||
/// ``Issue/Kind/errorCaught(_:)``. | ||
/// | ||
/// This type is not part of the public interface of the testing library. | ||
/// External callers should generally record issues by throwing their own errors | ||
/// or by calling ``Issue/record(_:sourceLocation:)``. | ||
struct SystemError: Error, CustomStringConvertible, CustomIssueRepresentable { | ||
var description: String | ||
|
||
func customize(_ issue: consuming Issue) -> Issue { | ||
issue.kind = .system | ||
issue.comments.append("\(self)") | ||
return issue | ||
} | ||
} | ||
|
||
/// A type representing misuse of testing library API. | ||
/// | ||
/// When an error of this type is thrown and caught by the testing library, it | ||
/// is recorded as an issue of kind ``Issue/Kind/apiMisused`` rather than | ||
/// ``Issue/Kind/errorCaught(_:)``. | ||
/// | ||
/// This type is not part of the public interface of the testing library. | ||
/// External callers should generally record issues by throwing their own errors | ||
/// or by calling ``Issue/record(_:sourceLocation:)``. | ||
struct APIMisuseError: Error, CustomStringConvertible, CustomIssueRepresentable { | ||
var description: String | ||
|
||
func customize(_ issue: consuming Issue) -> Issue { | ||
issue.kind = .apiMisused | ||
issue.comments.append("\(self)") | ||
return issue | ||
} | ||
} | ||
|
||
extension ExpectationFailedError: CustomIssueRepresentable { | ||
func customize(_ issue: consuming Issue) -> Issue { | ||
// Instances of this error type are thrown by `#require()` after an issue of | ||
// kind `.expectationFailed` has already been recorded. Code that rethrows | ||
// this error does not generate a new issue, but code that passes this error | ||
// to Issue.record() is misbehaving. | ||
issue.kind = .apiMisused | ||
issue.comments.append("Recorded an error of type \(Self.self) representing an expectation that failed and was already recorded: \(expectation)") | ||
return issue | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.