Created
September 11, 2021 14:21
-
-
Save BasThomas/ebf246686d23cf07b6ee2b00454d2d34 to your computer and use it in GitHub Desktop.
This file contains 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
import Foundation | |
import XCTest | |
struct WompWomp: Error { | |
} | |
func myThrowingFunction() throws -> Int { | |
throw WompWomp() | |
} | |
func myClosureFunction(_ closure: () -> Void) { | |
closure() | |
} | |
class ThrowNoThrowTests: XCTestCase { | |
func testExample() { | |
myClosureFunction { | |
XCTAssertEqual(myThrowingFunction(), 1) // Error: Call can throw but is not marked with 'try' | |
} | |
} | |
func testExample2() { | |
myClosureFunction { // Error: Invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> Void' | |
XCTAssertEqual(try myThrowingFunction(), 1) | |
} | |
} | |
func testExample3() { | |
myClosureFunction { | |
do { | |
try XCTAssertEqual(myThrowingFunction(), 1) // Test result: XCTAssertEqual failed: threw error "WompWomp()" | |
} catch { // Warning: 'catch' block is unreachable because no errors are thrown in 'do' block | |
print(error.localizedDescription) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe these examples also help: