@@ -37,21 +37,48 @@ public class AssertionRecorder: AssertionHandler {
3737 }
3838}
3939
40+ extension NMBExceptionCapture {
41+ internal func tryBlockThrows( _ unsafeBlock: ( ) throws -> Void ) throws {
42+ var catchedError : Error ?
43+ tryBlock {
44+ do {
45+ try unsafeBlock ( )
46+ } catch {
47+ catchedError = error
48+ }
49+ }
50+ if let error = catchedError {
51+ throw error
52+ }
53+ }
54+ }
55+
4056/// Allows you to temporarily replace the current Nimble assertion handler with
4157/// the one provided for the scope of the closure.
4258///
4359/// Once the closure finishes, then the original Nimble assertion handler is restored.
4460///
4561/// @see AssertionHandler
46- public func withAssertionHandler( _ tempAssertionHandler: AssertionHandler , closure: ( ) throws -> Void ) {
62+ public func withAssertionHandler( _ tempAssertionHandler: AssertionHandler ,
63+ file: FileString = #file,
64+ line: UInt = #line,
65+ closure: ( ) throws -> Void ) {
4766 let environment = NimbleEnvironment . activeInstance
4867 let oldRecorder = environment. assertionHandler
4968 let capturer = NMBExceptionCapture ( handler: nil , finally: ( {
5069 environment. assertionHandler = oldRecorder
5170 } ) )
5271 environment. assertionHandler = tempAssertionHandler
53- capturer. tryBlock {
54- try ! closure ( )
72+
73+ do {
74+ try capturer. tryBlockThrows {
75+ try closure ( )
76+ }
77+ } catch {
78+ let failureMessage = FailureMessage ( )
79+ failureMessage. stringValue = " unexpected error thrown: < \( error) > "
80+ let location = SourceLocation ( file: file, line: line)
81+ tempAssertionHandler. assert ( false , message: failureMessage, location: location)
5582 }
5683}
5784
0 commit comments