Created
February 24, 2022 20:34
-
-
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
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
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