apple / swift-nio-extras

Useful code around SwiftNIO.
https://swiftpackageindex.com/apple/swift-nio-extras/main/documentation/nioextras
Apache License 2.0
204 stars 79 forks source link

get rid of `do { ... } catch { ... }` for expected errors #83

Closed weissi closed 4 years ago

weissi commented 4 years ago

The

do {
    try someOperation()
XCTFail("should throw") // easy to forget
} catch error as SomethingError {
    XCTAssertEqual(.something, error as? SomethingError)
} catch {
    XCTFail("wrong error")
}

pattern is not only very long, it's also very error prone. If you forget any of the XCTFails, you might not tests what it looks like

XCTAssertThrowsError(try someOperation) { error in
    XCTAssertEqual(.something, error as? SomethingError)
}

is much safer and shorter.

We should replace all uses of this pattern in Tests/**.

See also https://github.com/apple/swift-nio/pull/1430 which does the same for swift-nio.

weissi commented 4 years ago

see also:

shekhar-rajak commented 4 years ago

Hi,

I want to work on to fix this. I will open a PR.

weissi commented 4 years ago

Awesome, thanks so much!