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
Add the ability for MTKTestable test(_:) blocks to throw #37
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The README needs to be updated.
The consequence of this change is that suddenly everything that uses the Testable
protocol now needs to be marked with a try
, and then additionally wrapped in something that can handle it (either XCTAssertThrowsError
, a do-catch
block, or marking the testing function as throws
... or I guess using try?
/try!
). I definitely understand the convenience of this in cases where thrown errors should cause a test to fail, but what does this do for all the probably more common cases where the code in the test
block doesn't even have a path to throw an error?
Is there a path to more cleanly implement this so we only have to add the error handling paths when we do want to test for this?
For example, what does it look like if failing on thrown errors is the default path?
static func test(_ testBlock: (Self) throws -> Void) {
let testVC = instanceForTesting()
testVC.loadView()
testVC.viewDidLoad()
do {
try testBlock(testVC)
}
catch let error {
XCTFail("An error occurred: \(error)")
}
}
(noting that if we do take this path, the test
method needs to take in file/line arguments and pass them to that XCTFail
message.
With this pattern, we don't break existing code. We also remove the requirement for adding try
any time anyone wants to use this functionality.
In the case that we do want the testBlock
to throw something, we've eliminated the need to write a bunch of boiler plate, the original goal of this PR.
This pattern also has one extra added benefit over your PR: if there is more test code after the call to test
, it still gets to run. If the call to test
itself throws us out of the test method, none of the code after it will have an opportunity to execute.
This is the beauty of public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T] You don't have to ... as for the |
@schrismartin Use Cmd+Option+/ on the functions you're modifying to get it to regen the doc comment. It will expect comments on what error the function can throw. |
XCTest
allows test methods to denote a test failure by marking the test asthrow
ing. This makes the testing of throwing functions a tad simpler.This tends to be much more elegant than something like this:
In the same light, the
test(_:)
function could be much more ergonomic by allowing uncaught errors to fail a test via propagation.The
test(_:)
function was markedrethrows
to allow the user to forego this functionality while testing functions that don't throw. This prevents the cruft of having totry
non-throwing functionality within a test when unnecessary.This PR is source breaking – anything that is already using the
test(_:)
method will need to add thethrows
andrethrows
keywords. This should be a minimal change, all of which can be caught by the compiler due to aType does not conform to protocol 'MTKTestable'
compiler error.