Skip to content

Instantly share code, notes, and snippets.

@AlexChekel1337
Created February 24, 2022 20:34
Show Gist options
  • Save AlexChekel1337/7f3800da640a97e7096bff5f91ec5777 to your computer and use it in GitHub Desktop.
Save AlexChekel1337/7f3800da640a97e7096bff5f91ec5777 to your computer and use it in GitHub Desktop.
Asserts that given result is a success, and if so, returns a success value
func XCTAssertSuccess<T, U>(
_ result: @autoclosure () throws -> Result<T, U>,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) -> T {
do {
let value = try result()
switch value {
case .success(let successValue):
return successValue
case .failure:
XCTFail(message(), file: file, line: line)
// Requires `continueAfterFailure` to be set to `false` on XCTestCase
// Otherwise we will hit the fatal error and will not be able to continue
// executing other tests
fatalError("XCTAssertSuccess failed")
}
} catch {
XCTFail(error.localizedDescription)
// Requires `continueAfterFailure` to be set to `false` on XCTestCase
// Otherwise we will hit the fatal error and will not be able to continue
// executing other tests
fatalError("XCTAssertSuccess failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment