diff --git a/Sources/Nimble/Adapters/NMBExpectation.swift b/Sources/Nimble/Adapters/NMBExpectation.swift index 36693051c..8d83e94e5 100644 --- a/Sources/Nimble/Adapters/NMBExpectation.swift +++ b/Sources/Nimble/Adapters/NMBExpectation.swift @@ -11,24 +11,27 @@ private func from(objcPredicate: NMBPredicate) -> Predicate { } } -internal struct ObjCMatcherWrapper: Matcher { - let matcher: NMBMatcher - - func matches(_ actualExpression: Expression, failureMessage: FailureMessage) -> Bool { - return matcher.matches( - // swiftlint:disable:next force_try - ({ try! actualExpression.evaluate() }), - failureMessage: failureMessage, - location: actualExpression.location) - } - - func doesNotMatch(_ actualExpression: Expression, failureMessage: FailureMessage) -> Bool { - return matcher.doesNotMatch( - // swiftlint:disable:next force_try - ({ try! actualExpression.evaluate() }), - failureMessage: failureMessage, - location: actualExpression.location) +private func from(matcher: NMBMatcher, style: ExpectationStyle) -> Predicate { + // Almost same as `Matcher.toClosure` + let closure: (Expression, FailureMessage) throws -> Bool = { expr, msg in + switch style { + case .toMatch: + return matcher.matches( + // swiftlint:disable:next force_try + ({ try! expr.evaluate() }), + failureMessage: msg, + location: expr.location + ) + case .toNotMatch: + return !matcher.doesNotMatch( + // swiftlint:disable:next force_try + ({ try! expr.evaluate() }), + failureMessage: msg, + location: expr.location + ) + } } + return Predicate._fromDeprecatedClosure(closure) } // Equivalent to Expectation, but for Nimble's Objective-C interface @@ -65,7 +68,7 @@ public class NMBExpectation: NSObject { if let pred = matcher as? NMBPredicate { self.expectValue.to(from(objcPredicate: pred)) } else { - self.expectValue.to(ObjCMatcherWrapper(matcher: matcher)) + self.expectValue.to(from(matcher: matcher, style: .toMatch)) } return self } @@ -76,7 +79,7 @@ public class NMBExpectation: NSObject { if let pred = matcher as? NMBPredicate { self.expectValue.to(from(objcPredicate: pred), description: description) } else { - self.expectValue.to(ObjCMatcherWrapper(matcher: matcher), description: description) + self.expectValue.to(from(matcher: matcher, style: .toMatch), description: description) } return self } @@ -87,7 +90,7 @@ public class NMBExpectation: NSObject { if let pred = matcher as? NMBPredicate { self.expectValue.toNot(from(objcPredicate: pred)) } else { - self.expectValue.toNot(ObjCMatcherWrapper(matcher: matcher)) + self.expectValue.toNot(from(matcher: matcher, style: .toNotMatch)) } return self } @@ -98,7 +101,7 @@ public class NMBExpectation: NSObject { if let pred = matcher as? NMBPredicate { self.expectValue.toNot(from(objcPredicate: pred), description: description) } else { - self.expectValue.toNot(ObjCMatcherWrapper(matcher: matcher), description: description) + self.expectValue.toNot(from(matcher: matcher, style: .toNotMatch), description: description) } return self } @@ -118,7 +121,7 @@ public class NMBExpectation: NSObject { ) } else { self.expectValue.toEventually( - ObjCMatcherWrapper(matcher: matcher), + from(matcher: matcher, style: .toMatch), timeout: self._timeout, description: nil ) @@ -136,7 +139,7 @@ public class NMBExpectation: NSObject { ) } else { self.expectValue.toEventually( - ObjCMatcherWrapper(matcher: matcher), + from(matcher: matcher, style: .toMatch), timeout: self._timeout, description: description ) @@ -154,7 +157,7 @@ public class NMBExpectation: NSObject { ) } else { self.expectValue.toEventuallyNot( - ObjCMatcherWrapper(matcher: matcher), + from(matcher: matcher, style: .toNotMatch), timeout: self._timeout, description: nil ) @@ -172,7 +175,7 @@ public class NMBExpectation: NSObject { ) } else { self.expectValue.toEventuallyNot( - ObjCMatcherWrapper(matcher: matcher), + from(matcher: matcher, style: .toNotMatch), timeout: self._timeout, description: description ) diff --git a/Sources/Nimble/Expectation.swift b/Sources/Nimble/Expectation.swift index 80cb514a6..955654ebb 100644 --- a/Sources/Nimble/Expectation.swift +++ b/Sources/Nimble/Expectation.swift @@ -1,6 +1,6 @@ import Foundation -// Deprecated +@available(*, deprecated) internal func expressionDoesNotMatch(_ expression: Expression, matcher: U, toNot: String, description: String?) -> (Bool, FailureMessage) where U: Matcher, U.ValueType == T { let msg = FailureMessage() @@ -69,7 +69,9 @@ public struct Expectation { ////////////////// OLD API ///////////////////// /// DEPRECATED: Tests the actual value using a matcher to match. - public func to(_ matcher: U, description: String? = nil) + @available(*, deprecated, message: "Use Predicate instead") + @discardableResult + public func to(_ matcher: U, description: String? = nil) -> Self where U: Matcher, U.ValueType == T { let (pass, msg) = execute( expression, @@ -80,22 +82,28 @@ public struct Expectation { captureExceptions: false ) verify(pass, msg) + return self } /// DEPRECATED: Tests the actual value using a matcher to not match. - public func toNot(_ matcher: U, description: String? = nil) + @available(*, deprecated, message: "Use Predicate instead") + @discardableResult + public func toNot(_ matcher: U, description: String? = nil) -> Self where U: Matcher, U.ValueType == T { // swiftlint:disable:next line_length let (pass, msg) = expressionDoesNotMatch(expression, matcher: matcher, toNot: "to not", description: description) verify(pass, msg) + return self } /// DEPRECATED: Tests the actual value using a matcher to not match. /// /// Alias to toNot(). - public func notTo(_ matcher: U, description: String? = nil) + @available(*, deprecated, message: "Use Predicate instead") + @discardableResult + public func notTo(_ matcher: U, description: String? = nil) -> Self where U: Matcher, U.ValueType == T { - toNot(matcher, description: description) + return toNot(matcher, description: description) } ////////////////// NEW API ///////////////////// diff --git a/Sources/Nimble/Matchers/AllPass.swift b/Sources/Nimble/Matchers/AllPass.swift index dbbccb9c5..329e87e3d 100644 --- a/Sources/Nimble/Matchers/AllPass.swift +++ b/Sources/Nimble/Matchers/AllPass.swift @@ -18,6 +18,7 @@ public func allPass return createPredicate(matcher) } +@available(*, deprecated, message: "Use Predicate instead") public func allPass(_ elementMatcher: M) -> Predicate where S: Sequence, M: Matcher, S.Iterator.Element == M.ValueType { return createPredicate(elementMatcher.predicate) diff --git a/Sources/Nimble/Matchers/Async.swift b/Sources/Nimble/Matchers/Async.swift index 3bd8a413e..dc5e5eddd 100644 --- a/Sources/Nimble/Matchers/Async.swift +++ b/Sources/Nimble/Matchers/Async.swift @@ -116,7 +116,7 @@ extension Expectation { } } -// Deprecated +@available(*, deprecated, message: "Use Predicate instead") extension Expectation { /// Tests the actual value using a matcher to match by checking continuously /// at each pollInterval until the timeout is reached. diff --git a/Sources/Nimble/Matchers/MatcherFunc.swift b/Sources/Nimble/Matchers/MatcherFunc.swift index abcafa990..cba4d33d5 100644 --- a/Sources/Nimble/Matchers/MatcherFunc.swift +++ b/Sources/Nimble/Matchers/MatcherFunc.swift @@ -9,7 +9,7 @@ /// Use the Matcher protocol instead of this type to accept custom matchers as /// input parameters. /// @see allPass for an example that uses accepts other matchers as input. -@available(*, deprecated, message: "Use to Predicate instead") +@available(*, deprecated, message: "Use Predicate instead") public struct MatcherFunc: Matcher { public let matcher: (Expression, FailureMessage) throws -> Bool @@ -28,6 +28,7 @@ public struct MatcherFunc: Matcher { /// Compatibility layer to new Matcher API. Converts an old-style matcher to a new one. /// Note: You should definitely spend the time to convert to the new api as soon as possible /// since this struct type is deprecated. + @available(*, deprecated, message: "Use Predicate directly instead") public var predicate: Predicate { return Predicate.fromDeprecatedMatcher(self) } @@ -44,7 +45,7 @@ public struct MatcherFunc: Matcher { /// Use the Matcher protocol instead of this type to accept custom matchers as /// input parameters. /// @see allPass for an example that uses accepts other matchers as input. -@available(*, deprecated, message: "Use to Predicate instead") +@available(*, deprecated, message: "Use Predicate instead") public struct NonNilMatcherFunc: Matcher { public let matcher: (Expression, FailureMessage) throws -> Bool @@ -79,6 +80,7 @@ public struct NonNilMatcherFunc: Matcher { /// Compatibility layer to new Matcher API. Converts an old-style matcher to a new one. /// Note: You should definitely spend the time to convert to the new api as soon as possible /// since this struct type is deprecated. + @available(*, deprecated, message: "Use Predicate directly instead") public var predicate: Predicate { return Predicate.fromDeprecatedMatcher(self) } diff --git a/Sources/Nimble/Matchers/MatcherProtocols.swift b/Sources/Nimble/Matchers/MatcherProtocols.swift index 21ffd7719..5cb4341cc 100644 --- a/Sources/Nimble/Matchers/MatcherProtocols.swift +++ b/Sources/Nimble/Matchers/MatcherProtocols.swift @@ -12,6 +12,7 @@ public protocol Matcher { func doesNotMatch(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool } +@available(*, deprecated) extension Matcher { var predicate: Predicate { return Predicate.fromDeprecatedMatcher(self) diff --git a/Sources/Nimble/Matchers/PostNotification.swift b/Sources/Nimble/Matchers/PostNotification.swift index 1a1d8130c..a2da7cc10 100644 --- a/Sources/Nimble/Matchers/PostNotification.swift +++ b/Sources/Nimble/Matchers/PostNotification.swift @@ -66,6 +66,7 @@ public func postNotifications( } } +@available(*, deprecated, message: "Use Predicate instead") public func postNotifications( _ notificationsMatcher: T, fromNotificationCenter center: NotificationCenter = .default) diff --git a/Sources/Nimble/Matchers/Predicate.swift b/Sources/Nimble/Matchers/Predicate.swift index cb32b6288..706c120f0 100644 --- a/Sources/Nimble/Matchers/Predicate.swift +++ b/Sources/Nimble/Matchers/Predicate.swift @@ -166,32 +166,41 @@ public enum PredicateStatus { } } -// Backwards compatibility until Old Matcher API removal -extension Predicate: Matcher { - /// Compatibility layer for old Matcher API, deprecated - public static func fromDeprecatedFullClosure(_ matcher: @escaping (Expression, FailureMessage, Bool) throws -> Bool) -> Predicate { +extension Predicate { + /// Compatibility layer for old Matcher API, deprecated. + /// Emulates the MatcherFunc API + internal static func _fromDeprecatedClosure(_ matcher: @escaping (Expression, FailureMessage) throws -> Bool) -> Predicate { + // swiftlint:disable:previous identifier_name return Predicate { actual in let failureMessage = FailureMessage() - let result = try matcher(actual, failureMessage, true) + let result = try matcher(actual, failureMessage) return PredicateResult( status: PredicateStatus(bool: result), message: failureMessage.toExpectationMessage() ) } } +} - /// Compatibility layer for old Matcher API, deprecated. - /// Emulates the MatcherFunc API - public static func fromDeprecatedClosure(_ matcher: @escaping (Expression, FailureMessage) throws -> Bool) -> Predicate { +// Backwards compatibility until Old Matcher API removal +@available(*, deprecated, message: "Use Predicate directly instead") +extension Predicate: Matcher { + /// Compatibility layer for old Matcher API, deprecated + public static func fromDeprecatedFullClosure(_ matcher: @escaping (Expression, FailureMessage, Bool) throws -> Bool) -> Predicate { return Predicate { actual in let failureMessage = FailureMessage() - let result = try matcher(actual, failureMessage) + let result = try matcher(actual, failureMessage, true) return PredicateResult( status: PredicateStatus(bool: result), message: failureMessage.toExpectationMessage() ) } + } + /// Compatibility layer for old Matcher API, deprecated. + /// Emulates the MatcherFunc API + public static func fromDeprecatedClosure(_ matcher: @escaping (Expression, FailureMessage) throws -> Bool) -> Predicate { + return _fromDeprecatedClosure(matcher) } /// Compatibility layer for old Matcher API, deprecated. diff --git a/Sources/Nimble/Matchers/SatisfyAllOf.swift b/Sources/Nimble/Matchers/SatisfyAllOf.swift index 37dd2fbf7..81cbb6495 100644 --- a/Sources/Nimble/Matchers/SatisfyAllOf.swift +++ b/Sources/Nimble/Matchers/SatisfyAllOf.swift @@ -8,6 +8,7 @@ public func satisfyAllOf(_ predicates: Predicate...) -> Predicate { /// A Nimble matcher that succeeds when the actual value matches with all of the matchers /// provided in the variable list of matchers. +@available(*, deprecated, message: "Use Predicate instead") public func satisfyAllOf(_ matchers: U...) -> Predicate where U: Matcher, U.ValueType == T { return satisfyAllOf(matchers.map { $0.predicate }) diff --git a/Sources/Nimble/Matchers/SatisfyAnyOf.swift b/Sources/Nimble/Matchers/SatisfyAnyOf.swift index 03b0a3a58..3feaa97f6 100644 --- a/Sources/Nimble/Matchers/SatisfyAnyOf.swift +++ b/Sources/Nimble/Matchers/SatisfyAnyOf.swift @@ -8,6 +8,7 @@ public func satisfyAnyOf(_ predicates: Predicate...) -> Predicate { /// A Nimble matcher that succeeds when the actual value matches with any of the matchers /// provided in the variable list of matchers. +@available(*, deprecated, message: "Use Predicate instead") public func satisfyAnyOf(_ matchers: U...) -> Predicate where U: Matcher, U.ValueType == T { return satisfyAnyOf(matchers.map { $0.predicate }) @@ -45,10 +46,12 @@ public func || (left: Predicate, right: Predicate) -> Predicate { return satisfyAnyOf(left, right) } +@available(*, deprecated, message: "Use Predicate instead") public func || (left: NonNilMatcherFunc, right: NonNilMatcherFunc) -> Predicate { return satisfyAnyOf(left, right) } +@available(*, deprecated, message: "Use Predicate instead") public func || (left: MatcherFunc, right: MatcherFunc) -> Predicate { return satisfyAnyOf(left, right) }