diff --git a/Sources/Nimble/Matchers/RaisesException.swift b/Sources/Nimble/Matchers/RaisesException.swift index 69a5687c1..2bd657494 100644 --- a/Sources/Nimble/Matchers/RaisesException.swift +++ b/Sources/Nimble/Matchers/RaisesException.swift @@ -121,93 +121,82 @@ internal func exceptionMatchesNonNilFieldsOrClosure( return matches } -public class NMBObjCRaiseExceptionMatcher: NSObject, NMBMatcher { - // swiftlint:disable identifier_name - internal var _name: String? - internal var _reason: String? - internal var _userInfo: NSDictionary? - internal var _block: ((NSException) -> Void)? - // swiftlint:enable identifier_name - - internal init(name: String?, reason: String?, userInfo: NSDictionary?, block: ((NSException) -> Void)?) { +public class NMBObjCRaiseExceptionPredicate: NMBPredicate { + private let _name: String? + private let _reason: String? + private let _userInfo: NSDictionary? + private let _block: ((NSException) -> Void)? + + fileprivate init(name: String?, reason: String?, userInfo: NSDictionary?, block: ((NSException) -> Void)?) { _name = name _reason = reason _userInfo = userInfo _block = block - } - - @objc public func matches(_ actualBlock: @escaping () -> NSObject?, failureMessage: FailureMessage, location: SourceLocation) -> Bool { - let block: () -> Any? = ({ _ = actualBlock(); return nil }) - let expr = Expression(expression: block, location: location) - do { - let predicate: Predicate = raiseException( - named: _name, - reason: _reason, - userInfo: _userInfo, - closure: _block - ) - let result = try predicate.satisfies(expr) - result.message.update(failureMessage: failureMessage) - return result.toBoolean(expectation: .toMatch) - } catch let error { - failureMessage.stringValue = "unexpected error thrown: <\(error)>" - return false + let predicate: Predicate = raiseException( + named: name, + reason: reason, + userInfo: userInfo, + closure: block + ) + let predicateBlock: PredicateBlock = { actualExpression in + return try predicate.satisfies(actualExpression).toObjectiveC() } + super.init(predicate: predicateBlock) } - @objc public func doesNotMatch(_ actualBlock: @escaping () -> NSObject?, failureMessage: FailureMessage, location: SourceLocation) -> Bool { - return !matches(actualBlock, failureMessage: failureMessage, location: location) - } - - @objc public var named: (_ name: String) -> NMBObjCRaiseExceptionMatcher { + @objc public var named: (_ name: String) -> NMBObjCRaiseExceptionPredicate { + let (reason, userInfo, block) = (_reason, _userInfo, _block) return { name in - return NMBObjCRaiseExceptionMatcher( + return NMBObjCRaiseExceptionPredicate( name: name, - reason: self._reason, - userInfo: self._userInfo, - block: self._block + reason: reason, + userInfo: userInfo, + block: block ) } } - @objc public var reason: (_ reason: String?) -> NMBObjCRaiseExceptionMatcher { + @objc public var reason: (_ reason: String?) -> NMBObjCRaiseExceptionPredicate { + let (name, userInfo, block) = (_name, _userInfo, _block) return { reason in - return NMBObjCRaiseExceptionMatcher( - name: self._name, + return NMBObjCRaiseExceptionPredicate( + name: name, reason: reason, - userInfo: self._userInfo, - block: self._block + userInfo: userInfo, + block: block ) } } - @objc public var userInfo: (_ userInfo: NSDictionary?) -> NMBObjCRaiseExceptionMatcher { + @objc public var userInfo: (_ userInfo: NSDictionary?) -> NMBObjCRaiseExceptionPredicate { + let (name, reason, block) = (_name, _reason, _block) return { userInfo in - return NMBObjCRaiseExceptionMatcher( - name: self._name, - reason: self._reason, + return NMBObjCRaiseExceptionPredicate( + name: name, + reason: reason, userInfo: userInfo, - block: self._block + block: block ) } } - @objc public var satisfyingBlock: (_ block: ((NSException) -> Void)?) -> NMBObjCRaiseExceptionMatcher { + @objc public var satisfyingBlock: (_ block: ((NSException) -> Void)?) -> NMBObjCRaiseExceptionPredicate { + let (name, reason, userInfo) = (_name, _reason, _userInfo) return { block in - return NMBObjCRaiseExceptionMatcher( - name: self._name, - reason: self._reason, - userInfo: self._userInfo, + return NMBObjCRaiseExceptionPredicate( + name: name, + reason: reason, + userInfo: userInfo, block: block ) } } } -extension NMBObjCMatcher { - @objc public class func raiseExceptionMatcher() -> NMBObjCRaiseExceptionMatcher { - return NMBObjCRaiseExceptionMatcher(name: nil, reason: nil, userInfo: nil, block: nil) +extension NMBPredicate { + @objc public class func raiseExceptionMatcher() -> NMBObjCRaiseExceptionPredicate { + return NMBObjCRaiseExceptionPredicate(name: nil, reason: nil, userInfo: nil, block: nil) } } #endif diff --git a/Sources/NimbleObjectiveC/DSL.h b/Sources/NimbleObjectiveC/DSL.h index 9170541bf..2adce457a 100644 --- a/Sources/NimbleObjectiveC/DSL.h +++ b/Sources/NimbleObjectiveC/DSL.h @@ -2,7 +2,7 @@ @class NMBExpectation; @class NMBObjCBeCloseToMatcher; -@class NMBObjCRaiseExceptionMatcher; +@class NMBObjCRaiseExceptionPredicate; @protocol NMBMatcher; @@ -332,8 +332,8 @@ NIMBLE_EXPORT id NMB_endWith(id itemElementOrSubstring); NIMBLE_SHORT(id endWith(id itemElementOrSubstring), NMB_endWith(itemElementOrSubstring)); -NIMBLE_EXPORT NMBObjCRaiseExceptionMatcher *NMB_raiseException(void); -NIMBLE_SHORT(NMBObjCRaiseExceptionMatcher *raiseException(void), +NIMBLE_EXPORT NMBObjCRaiseExceptionPredicate *NMB_raiseException(void); +NIMBLE_SHORT(NMBObjCRaiseExceptionPredicate *raiseException(void), NMB_raiseException()); NIMBLE_EXPORT id NMB_match(id expectedValue); diff --git a/Sources/NimbleObjectiveC/DSL.m b/Sources/NimbleObjectiveC/DSL.m index 3333989bc..7adaca50a 100644 --- a/Sources/NimbleObjectiveC/DSL.m +++ b/Sources/NimbleObjectiveC/DSL.m @@ -142,8 +142,8 @@ NIMBLE_EXPORT void NMB_failWithMessage(NSString *msg, NSString *file, NSUInteger return [NMBPredicate satisfyAllOfMatcher:matchers]; } -NIMBLE_EXPORT NMBObjCRaiseExceptionMatcher *NMB_raiseException() { - return [NMBObjCMatcher raiseExceptionMatcher]; +NIMBLE_EXPORT NMBObjCRaiseExceptionPredicate *NMB_raiseException() { + return [NMBPredicate raiseExceptionMatcher]; } NIMBLE_EXPORT NMBWaitUntilTimeoutBlock NMB_waitUntilTimeoutBuilder(NSString *file, NSUInteger line) {