Skip to content

Commit

Permalink
Merge pull request #762 from Quick/convert-objc-raiseexception-to-nmb…
Browse files Browse the repository at this point in the history
…predicate

[BREAKING] Convert Objective-C raiseException matcher to NMBPredicate
  • Loading branch information
ikesyo authored May 17, 2020
2 parents ef43d3d + 9fb973a commit 0d27e8c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 59 deletions.
97 changes: 43 additions & 54 deletions Sources/Nimble/Matchers/RaisesException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any> = 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<NSObject> = 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
6 changes: 3 additions & 3 deletions Sources/NimbleObjectiveC/DSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@class NMBExpectation;
@class NMBObjCBeCloseToMatcher;
@class NMBObjCRaiseExceptionMatcher;
@class NMBObjCRaiseExceptionPredicate;
@protocol NMBMatcher;


Expand Down Expand Up @@ -332,8 +332,8 @@ NIMBLE_EXPORT id<NMBMatcher> NMB_endWith(id itemElementOrSubstring);
NIMBLE_SHORT(id<NMBMatcher> 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<NMBMatcher> NMB_match(id expectedValue);
Expand Down
4 changes: 2 additions & 2 deletions Sources/NimbleObjectiveC/DSL.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0d27e8c

Please sign in to comment.