From 9682bc871a0a53e1726587ffdb1d60e0b1a64872 Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Fri, 8 May 2020 22:28:32 +0900 Subject: [PATCH] [BREAKING] Make `raiseException` generic and usable with non-void closures --- Sources/Nimble/Matchers/RaisesException.swift | 6 +++--- Tests/NimbleTests/Matchers/RaisesExceptionTest.swift | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Sources/Nimble/Matchers/RaisesException.swift b/Sources/Nimble/Matchers/RaisesException.swift index 4dd930442..69a5687c1 100644 --- a/Sources/Nimble/Matchers/RaisesException.swift +++ b/Sources/Nimble/Matchers/RaisesException.swift @@ -12,11 +12,11 @@ import Foundation /// /// nil arguments indicates that the matcher should not attempt to match against /// that parameter. -public func raiseException( +public func raiseException( named: String? = nil, reason: String? = nil, userInfo: NSDictionary? = nil, - closure: ((NSException) -> Void)? = nil) -> Predicate { + closure: ((NSException) -> Void)? = nil) -> Predicate { return Predicate { actualExpression in var exception: NSException? let capture = NMBExceptionCapture(handler: ({ e in @@ -141,7 +141,7 @@ public class NMBObjCRaiseExceptionMatcher: NSObject, NMBMatcher { let expr = Expression(expression: block, location: location) do { - let predicate = raiseException( + let predicate: Predicate = raiseException( named: _name, reason: _reason, userInfo: _userInfo, diff --git a/Tests/NimbleTests/Matchers/RaisesExceptionTest.swift b/Tests/NimbleTests/Matchers/RaisesExceptionTest.swift index 43f1345dc..eb1135551 100644 --- a/Tests/NimbleTests/Matchers/RaisesExceptionTest.swift +++ b/Tests/NimbleTests/Matchers/RaisesExceptionTest.swift @@ -152,5 +152,17 @@ final class RaisesExceptionTest: XCTestCase { }) } } + + func testNonVoidClosure() { + expect { return 1 }.toNot(raiseException()) + expect { return 2 }.toNot(raiseException(named: "laugh")) + expect { return 3 }.toNot(raiseException(named: "laugh", reason: "Lulz")) + expect { return 4 }.toNot(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"])) + expect { return 5 }.toNot(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]) { _ in }) + } + + func testChainOnRaiseException() { + expect { () -> Int in return 5 }.toNot(raiseException()).to(equal(5)) + } } #endif