Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Convert Objective-C beCloseTo matcher to NMBPredicate #764

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 15 additions & 44 deletions Sources/Nimble/Matchers/BeCloseTo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,31 @@ public func beCloseTo(_ expectedValue: NMBDoubleConvertible, within delta: Doubl
}

#if canImport(Darwin)
public class NMBObjCBeCloseToMatcher: NSObject, NMBMatcher {
// swiftlint:disable identifier_name
var _expected: NSNumber
var _delta: CDouble
// swiftlint:enable identifier_name
init(expected: NSNumber, within: CDouble) {
_expected = expected
_delta = within
}
public class NMBObjCBeCloseToPredicate: NMBPredicate {
private let _expected: NSNumber

@objc public func matches(_ actualExpression: @escaping () -> NSObject?, failureMessage: FailureMessage, location: SourceLocation) -> Bool {
let actualBlock: () -> NMBDoubleConvertible? = ({
return actualExpression() as? NMBDoubleConvertible
})
let expr = Expression(expression: actualBlock, location: location)
let predicate = beCloseTo(self._expected, within: self._delta)

do {
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
}
}
fileprivate init(expected: NSNumber, within: CDouble) {
_expected = expected

@objc public func doesNotMatch(_ actualExpression: @escaping () -> NSObject?, failureMessage: FailureMessage, location: SourceLocation) -> Bool {
let actualBlock: () -> NMBDoubleConvertible? = ({
return actualExpression() as? NMBDoubleConvertible
})
let expr = Expression(expression: actualBlock, location: location)
let predicate = beCloseTo(self._expected, within: self._delta)

do {
let result = try predicate.satisfies(expr)
result.message.update(failureMessage: failureMessage)
return result.toBoolean(expectation: .toNotMatch)
} catch let error {
failureMessage.stringValue = "unexpected error thrown: <\(error)>"
return false
let predicate = beCloseTo(expected, within: within)
let predicateBlock: PredicateBlock = { actualExpression in
let expr = actualExpression.cast { $0 as? NMBDoubleConvertible }
return try predicate.satisfies(expr).toObjectiveC()
}
super.init(predicate: predicateBlock)
}

@objc public var within: (CDouble) -> NMBObjCBeCloseToMatcher {
@objc public var within: (CDouble) -> NMBObjCBeCloseToPredicate {
let expected = _expected
return { delta in
return NMBObjCBeCloseToMatcher(expected: self._expected, within: delta)
return NMBObjCBeCloseToPredicate(expected: expected, within: delta)
}
}
}

extension NMBObjCMatcher {
@objc public class func beCloseToMatcher(_ expected: NSNumber, within: CDouble) -> NMBObjCBeCloseToMatcher {
return NMBObjCBeCloseToMatcher(expected: expected, within: within)
extension NMBPredicate {
@objc public class func beCloseToMatcher(_ expected: NSNumber, within: CDouble) -> NMBObjCBeCloseToPredicate {
return NMBObjCBeCloseToPredicate(expected: expected, within: within)
}
}
#endif
Expand Down
10 changes: 5 additions & 5 deletions Sources/NimbleObjectiveC/DSL.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import <Foundation/Foundation.h>

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

Expand Down Expand Up @@ -127,14 +127,14 @@ NIMBLE_EXPORT NMBExpectation *NMB_expectAction(void(^actualBlock)(void), NSStrin

#define DEFINE_OVERLOAD(TYPE, EXPR) \
NIMBLE_EXPORT_INLINE NIMBLE_OVERLOADABLE \
NMBObjCBeCloseToMatcher *NMB_beCloseTo(TYPE expectedValue) { \
NMBObjCBeCloseToPredicate *NMB_beCloseTo(TYPE expectedValue) { \
return NMB_beCloseTo((NSNumber *)(EXPR)); \
} \
NIMBLE_SHORT_OVERLOADED(NMBObjCBeCloseToMatcher *beCloseTo(TYPE expectedValue), \
NIMBLE_SHORT_OVERLOADED(NMBObjCBeCloseToPredicate *beCloseTo(TYPE expectedValue), \
NMB_beCloseTo(expectedValue));

NIMBLE_EXPORT NIMBLE_OVERLOADABLE NMBObjCBeCloseToMatcher *NMB_beCloseTo(NSNumber *expectedValue);
NIMBLE_SHORT_OVERLOADED(NMBObjCBeCloseToMatcher *beCloseTo(NSNumber *expectedValue),
NIMBLE_EXPORT NIMBLE_OVERLOADABLE NMBObjCBeCloseToPredicate *NMB_beCloseTo(NSNumber *expectedValue);
NIMBLE_SHORT_OVERLOADED(NMBObjCBeCloseToPredicate *beCloseTo(NSNumber *expectedValue),
NMB_beCloseTo(expectedValue));

// it would be better to only overload float & double, but zero becomes ambigious
Expand Down
4 changes: 2 additions & 2 deletions Sources/NimbleObjectiveC/DSL.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ NIMBLE_EXPORT void NMB_failWithMessage(NSString *msg, NSString *file, NSUInteger
return [NMBPredicate beAKindOfMatcher:expectedClass];
}

NIMBLE_EXPORT NIMBLE_OVERLOADABLE NMBObjCBeCloseToMatcher *NMB_beCloseTo(NSNumber *expectedValue) {
return [NMBObjCMatcher beCloseToMatcher:expectedValue within:0.001];
NIMBLE_EXPORT NIMBLE_OVERLOADABLE NMBObjCBeCloseToPredicate *NMB_beCloseTo(NSNumber *expectedValue) {
return [NMBPredicate beCloseToMatcher:expectedValue within:0.001];
}

NIMBLE_EXPORT id<NMBMatcher> NMB_beginWith(id itemElementOrSubstring) {
Expand Down