[swift-evolution] Proposal: XCTest Support for Swift Error Handling

Philippe Hausler phausler at apple.com
Mon Jan 11 20:15:43 CST 2016


I think I am in full agreement with Dmitri on this: The XCTAssertThrowsError should be the only one that interacts with errors and that others should not try to hijack the try.

But super-big +1 on the XCTAssertThrowsError being in place to verify error cases; swift-corelibs-foundation is missing a lot of testing of error cases and it would be nice to have an effort to start verifying those (and their structures)

> On Jan 10, 2016, at 11:18 AM, Dmitri Gribenko via swift-evolution <swift-evolution at swift.org> wrote:
> 
> On Sat, Jan 9, 2016 at 6:58 PM, Chris Hanson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Allowing Test Assertions to Throw Errors
> 
> We can also allow the @autoclosure expression that is passed into an assertion to throw an error, and treat that error as an unexpected failure (since the code is being invoked in an assertion that isn't directly related to error handling). For example:
> 
> func testVendingMultipleItemsWithSufficientFunds() {
>     vendingMachine.deposit(10)
>     XCTAssertEqual(try vendingMachine.vend(row: 1, column: 1), "Candy Bar")
>     XCTAssertEqual(try vendingMachine.vend(row: 1, column: 2), "Chips")
> }
> 
> I have significant concerns about doing this.  Consider the following code:
> 
> var foo = Foo()
> do {
>     XCTAssertEqual(try foo.doSomething(), 42)
> } catch {
>     XCTAssertEqual(foo.success, false)
> }
> 
> Adding ‘throws’ to the autoclosures will:
> 
> (1) change meaning of existing tests (the example above used to proceed to the catch block in case of errors, and will start failing with this change),
> 
> (2) hijacks ‘try’ and defeats its purpose — being able to understand the control flow.  Developers know that if they see a ‘try’, it should match with either a do-catch, or be used in a throwing function.  Adding this API makes the code misleading.
> 
> Note that although (2) applies to the XCTAssertThrowsError() API, because the name of that API makes it clear it is doing something special with error handling, I’m not concerned about it.  But adding error handling to regular assertion functions has the potential to create misleading code.
> 
> Changing the way control flow works breaks one of the basic language features — substitutability:
> 
> let bar1 = try bar()
> foo(bar1)
> 
> should be equivalent to:
> 
> foo(try bar())
> 
> Dmitri
> 
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com <mailto:gribozavr at gmail.com>>*/
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160111/a2e2c727/attachment.html>


More information about the swift-evolution mailing list