From 16ecfdfd3023fd8836614e43a4d91b4469d21a30 Mon Sep 17 00:00:00 2001 From: Ben Chatelain Date: Sun, 3 May 2020 15:52:28 -0600 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A5=20Remove=20deprecated=20Matche?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Nimble.xcodeproj/project.pbxproj | 8 - Sources/Nimble/Adapters/NMBExpectation.swift | 160 +++++------------- Sources/Nimble/Expectation.swift | 52 ------ Sources/Nimble/Matchers/AllPass.swift | 5 - Sources/Nimble/Matchers/Async.swift | 72 -------- Sources/Nimble/Matchers/MatcherFunc.swift | 85 ---------- .../Nimble/Matchers/MatcherProtocols.swift | 24 --- .../Nimble/Matchers/PostNotification.swift | 32 ---- Sources/Nimble/Matchers/Predicate.swift | 49 ------ Sources/Nimble/Matchers/SatisfyAllOf.swift | 6 - Sources/Nimble/Matchers/SatisfyAnyOf.swift | 15 -- 11 files changed, 46 insertions(+), 462 deletions(-) delete mode 100644 Sources/Nimble/Matchers/MatcherFunc.swift diff --git a/Nimble.xcodeproj/project.pbxproj b/Nimble.xcodeproj/project.pbxproj index 100a7cc9c..e4fc51d18 100644 --- a/Nimble.xcodeproj/project.pbxproj +++ b/Nimble.xcodeproj/project.pbxproj @@ -54,11 +54,8 @@ 1F1871E11CA89EF600A34BF2 /* NMBExceptionCapture.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F1871BE1CA89EDB00A34BF2 /* NMBExceptionCapture.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F1871E21CA89EF600A34BF2 /* NMBStringify.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F1871C01CA89EDB00A34BF2 /* NMBStringify.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F1871E41CA89FB600A34BF2 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1871E31CA89FB600A34BF2 /* Async.swift */; }; - 1F1871E61CA89FCD00A34BF2 /* MatcherFunc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1871E51CA89FCD00A34BF2 /* MatcherFunc.swift */; }; 1F1871E71CA8A18400A34BF2 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1871E31CA89FB600A34BF2 /* Async.swift */; }; 1F1871E81CA8A18400A34BF2 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1871E31CA89FB600A34BF2 /* Async.swift */; }; - 1F1871E91CA8A18700A34BF2 /* MatcherFunc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1871E51CA89FCD00A34BF2 /* MatcherFunc.swift */; }; - 1F1871EB1CA8A18800A34BF2 /* MatcherFunc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1871E51CA89FCD00A34BF2 /* MatcherFunc.swift */; }; 1F1A742F1940169200FFFC47 /* Nimble.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F1A742E1940169200FFFC47 /* Nimble.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F1A74351940169200FFFC47 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F1A74291940169200FFFC47 /* Nimble.framework */; }; 1F1B5AD41963E13900CA8BF9 /* BeAKindOfTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1B5AD31963E13900CA8BF9 /* BeAKindOfTest.swift */; }; @@ -501,7 +498,6 @@ 1F1871C31CA89EDB00A34BF2 /* NMBObjCMatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NMBObjCMatcher.swift; sourceTree = ""; }; 1F1871CD1CA89EE000A34BF2 /* ExceptionCapture.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExceptionCapture.swift; sourceTree = ""; }; 1F1871E31CA89FB600A34BF2 /* Async.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Async.swift; sourceTree = ""; }; - 1F1871E51CA89FCD00A34BF2 /* MatcherFunc.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MatcherFunc.swift; sourceTree = ""; }; 1F1A74291940169200FFFC47 /* Nimble.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 1F1A742D1940169200FFFC47 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 1F1A742E1940169200FFFC47 /* Nimble.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Nimble.h; sourceTree = ""; }; @@ -864,7 +860,6 @@ B20058C020E92C7500C1264D /* ElementsEqual.swift */, 472FD1341B9E085700C7B8DA /* HaveCount.swift */, DDB4D5EC19FE43C200E9D9FE /* Match.swift */, - 1F1871E51CA89FCD00A34BF2 /* MatcherFunc.swift */, 1FD8CD1D1968AB07008ED995 /* MatcherProtocols.swift */, AE7ADE441C80BF8000B94CD3 /* MatchError.swift */, 1FCF91521C61C8A400B15DCB /* PostNotification.swift */, @@ -1355,7 +1350,6 @@ 1FD8CD441968AB07008ED995 /* BeginWith.swift in Sources */, 1FD8CD4A1968AB07008ED995 /* BeIdenticalTo.swift in Sources */, 1FE661581E6574E30035F243 /* ExpectationMessage.swift in Sources */, - 1F1871E61CA89FCD00A34BF2 /* MatcherFunc.swift in Sources */, 1FD8CD421968AB07008ED995 /* BeEmpty.swift in Sources */, 1F1871E41CA89FB600A34BF2 /* Async.swift in Sources */, 1F1871CA1CA89EDB00A34BF2 /* NMBStringify.m in Sources */, @@ -1493,7 +1487,6 @@ 1F5DF16D1BDCA0F500C3A531 /* AdapterProtocols.swift in Sources */, 1F5DF17B1BDCA0F500C3A531 /* BeginWith.swift in Sources */, 1F5DF17E1BDCA0F500C3A531 /* BeIdenticalTo.swift in Sources */, - 1F1871E91CA8A18700A34BF2 /* MatcherFunc.swift in Sources */, 1F5DF17A1BDCA0F500C3A531 /* BeEmpty.swift in Sources */, 1F5DF18C1BDCA0F500C3A531 /* Await.swift in Sources */, 1F1871D81CA89EEF00A34BF2 /* NMBStringify.m in Sources */, @@ -1632,7 +1625,6 @@ 1FD8CD491968AB07008ED995 /* BeGreaterThanOrEqualTo.swift in Sources */, 1FE661571E6574E30035F243 /* ExpectationMessage.swift in Sources */, 1FD8CD451968AB07008ED995 /* BeginWith.swift in Sources */, - 1F1871EB1CA8A18800A34BF2 /* MatcherFunc.swift in Sources */, 1FD8CD4B1968AB07008ED995 /* BeIdenticalTo.swift in Sources */, 1FD8CD431968AB07008ED995 /* BeEmpty.swift in Sources */, 1F1871D41CA89EEE00A34BF2 /* NMBStringify.m in Sources */, diff --git a/Sources/Nimble/Adapters/NMBExpectation.swift b/Sources/Nimble/Adapters/NMBExpectation.swift index 15d8ce35d..299426081 100644 --- a/Sources/Nimble/Adapters/NMBExpectation.swift +++ b/Sources/Nimble/Adapters/NMBExpectation.swift @@ -11,26 +11,6 @@ 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) - } -} - // Equivalent to Expectation, but for Nimble's Objective-C interface public class NMBExpectation: NSObject { // swiftlint:disable identifier_name @@ -60,127 +40,79 @@ public class NMBExpectation: NSObject { } } - @objc public var to: (NMBMatcher) -> Void { - return { matcher in - if let pred = matcher as? NMBPredicate { - self.expectValue.to(from(objcPredicate: pred)) - } else { - self.expectValue.to(ObjCMatcherWrapper(matcher: matcher)) - } + @objc public var to: (NMBPredicate) -> Void { + return { pred in + self.expectValue.to(from(objcPredicate: pred)) } } - @objc public var toWithDescription: (NMBMatcher, String) -> Void { - return { matcher, description in - if let pred = matcher as? NMBPredicate { - self.expectValue.to(from(objcPredicate: pred), description: description) - } else { - self.expectValue.to(ObjCMatcherWrapper(matcher: matcher), description: description) - } + @objc public var toWithDescription: (NMBPredicate, String) -> Void { + return { pred, description in + self.expectValue.to(from(objcPredicate: pred), description: description) } } - @objc public var toNot: (NMBMatcher) -> Void { - return { matcher in - if let pred = matcher as? NMBPredicate { - self.expectValue.toNot(from(objcPredicate: pred)) - } else { - self.expectValue.toNot(ObjCMatcherWrapper(matcher: matcher)) - } + @objc public var toNot: (NMBPredicate) -> Void { + return { pred in + self.expectValue.toNot(from(objcPredicate: pred)) } } - @objc public var toNotWithDescription: (NMBMatcher, String) -> Void { - return { matcher, description in - if let pred = matcher as? NMBPredicate { - self.expectValue.toNot(from(objcPredicate: pred), description: description) - } else { - self.expectValue.toNot(ObjCMatcherWrapper(matcher: matcher), description: description) - } + @objc public var toNotWithDescription: (NMBPredicate, String) -> Void { + return { pred, description in + self.expectValue.toNot(from(objcPredicate: pred), description: description) } } - @objc public var notTo: (NMBMatcher) -> Void { return toNot } - - @objc public var notToWithDescription: (NMBMatcher, String) -> Void { return toNotWithDescription } - - @objc public var toEventually: (NMBMatcher) -> Void { - return { matcher in - if let pred = matcher as? NMBPredicate { - self.expectValue.toEventually( - from(objcPredicate: pred), - timeout: self._timeout, - description: nil - ) - } else { - self.expectValue.toEventually( - ObjCMatcherWrapper(matcher: matcher), - timeout: self._timeout, - description: nil - ) - } + @objc public var notTo: (NMBPredicate) -> Void { return toNot } + + @objc public var notToWithDescription: (NMBPredicate, String) -> Void { return toNotWithDescription } + + @objc public var toEventually: (NMBPredicate) -> Void { + return { pred in + self.expectValue.toEventually( + from(objcPredicate: pred), + timeout: self._timeout, + description: nil + ) } } - @objc public var toEventuallyWithDescription: (NMBMatcher, String) -> Void { - return { matcher, description in - if let pred = matcher as? NMBPredicate { - self.expectValue.toEventually( - from(objcPredicate: pred), - timeout: self._timeout, - description: description - ) - } else { - self.expectValue.toEventually( - ObjCMatcherWrapper(matcher: matcher), - timeout: self._timeout, - description: description - ) - } + @objc public var toEventuallyWithDescription: (NMBPredicate, String) -> Void { + return { pred, description in + self.expectValue.toEventually( + from(objcPredicate: pred), + timeout: self._timeout, + description: description + ) } } - @objc public var toEventuallyNot: (NMBMatcher) -> Void { - return { matcher in - if let pred = matcher as? NMBPredicate { - self.expectValue.toEventuallyNot( - from(objcPredicate: pred), - timeout: self._timeout, - description: nil - ) - } else { - self.expectValue.toEventuallyNot( - ObjCMatcherWrapper(matcher: matcher), - timeout: self._timeout, - description: nil - ) - } + @objc public var toEventuallyNot: (NMBPredicate) -> Void { + return { pred in + self.expectValue.toEventuallyNot( + from(objcPredicate: pred), + timeout: self._timeout, + description: nil + ) } } - @objc public var toEventuallyNotWithDescription: (NMBMatcher, String) -> Void { - return { matcher, description in - if let pred = matcher as? NMBPredicate { - self.expectValue.toEventuallyNot( - from(objcPredicate: pred), - timeout: self._timeout, - description: description - ) - } else { - self.expectValue.toEventuallyNot( - ObjCMatcherWrapper(matcher: matcher), - timeout: self._timeout, - description: description - ) - } + @objc public var toEventuallyNotWithDescription: (NMBPredicate, String) -> Void { + return { pred, description in + self.expectValue.toEventuallyNot( + from(objcPredicate: pred), + timeout: self._timeout, + description: description + ) } } - @objc public var toNotEventually: (NMBMatcher) -> Void { + @objc public var toNotEventually: (NMBPredicate) -> Void { return toEventuallyNot } - @objc public var toNotEventuallyWithDescription: (NMBMatcher, String) -> Void { + @objc public var toNotEventuallyWithDescription: (NMBPredicate, String) -> Void { return toEventuallyNotWithDescription } diff --git a/Sources/Nimble/Expectation.swift b/Sources/Nimble/Expectation.swift index 41625a123..c45d92fcc 100644 --- a/Sources/Nimble/Expectation.swift +++ b/Sources/Nimble/Expectation.swift @@ -1,23 +1,5 @@ import Foundation -// Deprecated -internal func expressionDoesNotMatch(_ expression: Expression, matcher: U, toNot: String, description: String?) -> (Bool, FailureMessage) - where U: Matcher, U.ValueType == T { - let msg = FailureMessage() - msg.userDescription = description - msg.to = toNot - do { - let pass = try matcher.doesNotMatch(expression, failureMessage: msg) - if msg.actualValue == "" { - msg.actualValue = "<\(stringify(try expression.evaluate()))>" - } - return (pass, msg) - } catch let error { - msg.stringValue = "unexpected error thrown: <\(error)>" - return (false, msg) - } -} - internal func execute(_ expression: Expression, _ style: ExpectationStyle, _ predicate: Predicate, to: String, description: String?, captureExceptions: Bool = true) -> (Bool, FailureMessage) { func run() -> (Bool, FailureMessage) { let msg = FailureMessage() @@ -66,40 +48,6 @@ public struct Expectation { handler.assert(pass, message: message, location: expression.location) } - ////////////////// OLD API ///////////////////// - - /// DEPRECATED: Tests the actual value using a matcher to match. - public func to(_ matcher: U, description: String? = nil) - where U: Matcher, U.ValueType == T { - let (pass, msg) = execute( - expression, - .toMatch, - matcher.predicate, - to: "to", - description: description, - captureExceptions: false - ) - verify(pass, msg) - } - - /// DEPRECATED: Tests the actual value using a matcher to not match. - public func toNot(_ matcher: U, description: String? = nil) - 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) - } - - /// DEPRECATED: Tests the actual value using a matcher to not match. - /// - /// Alias to toNot(). - public func notTo(_ matcher: U, description: String? = nil) - where U: Matcher, U.ValueType == T { - toNot(matcher, description: description) - } - - ////////////////// NEW API ///////////////////// - /// Tests the actual value using a matcher to match. public func to(_ predicate: Predicate, description: String? = nil) { let (pass, msg) = execute(expression, .toMatch, predicate, to: "to", description: description) diff --git a/Sources/Nimble/Matchers/AllPass.swift b/Sources/Nimble/Matchers/AllPass.swift index dbbccb9c5..3af6ce8e3 100644 --- a/Sources/Nimble/Matchers/AllPass.swift +++ b/Sources/Nimble/Matchers/AllPass.swift @@ -18,11 +18,6 @@ public func allPass return createPredicate(matcher) } -public func allPass(_ elementMatcher: M) -> Predicate - where S: Sequence, M: Matcher, S.Iterator.Element == M.ValueType { - return createPredicate(elementMatcher.predicate) -} - public func allPass(_ elementPredicate: Predicate) -> Predicate where S: Sequence { return createPredicate(elementPredicate) diff --git a/Sources/Nimble/Matchers/Async.swift b/Sources/Nimble/Matchers/Async.swift index 3bd8a413e..02f2535a2 100644 --- a/Sources/Nimble/Matchers/Async.swift +++ b/Sources/Nimble/Matchers/Async.swift @@ -115,75 +115,3 @@ extension Expectation { return toEventuallyNot(predicate, timeout: timeout, pollInterval: pollInterval, description: description) } } - -// Deprecated -extension Expectation { - /// Tests the actual value using a matcher to match by checking continuously - /// at each pollInterval until the timeout is reached. - /// - /// @discussion - /// This function manages the main run loop (`NSRunLoop.mainRunLoop()`) while this function - /// is executing. Any attempts to touch the run loop may cause non-deterministic behavior. - public func toEventually(_ matcher: U, timeout: DispatchTimeInterval = AsyncDefaults.timeout, pollInterval: DispatchTimeInterval = AsyncDefaults.pollInterval, description: String? = nil) - where U: Matcher, U.ValueType == T { - if expression.isClosure { - let (pass, msg) = execute( - expression, - .toMatch, - async( - style: .toMatch, - predicate: matcher.predicate, - timeout: timeout, - poll: pollInterval, - fnName: "toEventually" - ), - to: "to eventually", - description: description, - captureExceptions: false - ) - verify(pass, msg) - } else { - verify(false, toEventuallyRequiresClosureError) - } - } - - /// Tests the actual value using a matcher to not match by checking - /// continuously at each pollInterval until the timeout is reached. - /// - /// @discussion - /// This function manages the main run loop (`NSRunLoop.mainRunLoop()`) while this function - /// is executing. Any attempts to touch the run loop may cause non-deterministic behavior. - public func toEventuallyNot(_ matcher: U, timeout: DispatchTimeInterval = AsyncDefaults.timeout, pollInterval: DispatchTimeInterval = AsyncDefaults.pollInterval, description: String? = nil) - where U: Matcher, U.ValueType == T { - if expression.isClosure { - let (pass, msg) = expressionDoesNotMatch( - expression, - matcher: async( - style: .toNotMatch, - predicate: matcher.predicate, - timeout: timeout, - poll: pollInterval, - fnName: "toEventuallyNot" - ), - toNot: "to eventually not", - description: description - ) - verify(pass, msg) - } else { - verify(false, toEventuallyRequiresClosureError) - } - } - - /// Tests the actual value using a matcher to not match by checking - /// continuously at each pollInterval until the timeout is reached. - /// - /// Alias of toEventuallyNot() - /// - /// @discussion - /// This function manages the main run loop (`NSRunLoop.mainRunLoop()`) while this function - /// is executing. Any attempts to touch the run loop may cause non-deterministic behavior. - public func toNotEventually(_ matcher: U, timeout: DispatchTimeInterval = AsyncDefaults.timeout, pollInterval: DispatchTimeInterval = AsyncDefaults.pollInterval, description: String? = nil) - where U: Matcher, U.ValueType == T { - return toEventuallyNot(matcher, timeout: timeout, pollInterval: pollInterval, description: description) - } -} diff --git a/Sources/Nimble/Matchers/MatcherFunc.swift b/Sources/Nimble/Matchers/MatcherFunc.swift deleted file mode 100644 index abcafa990..000000000 --- a/Sources/Nimble/Matchers/MatcherFunc.swift +++ /dev/null @@ -1,85 +0,0 @@ -/// DEPRECATED: A convenience API to build matchers that don't need special negation -/// behavior. The toNot() behavior is the negation of to(). -/// -/// @see NonNilMatcherFunc if you prefer to have this matcher fail when nil -/// values are received in an expectation. -/// -/// You may use this when implementing your own custom matchers. -/// -/// 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") -public struct MatcherFunc: Matcher { - public let matcher: (Expression, FailureMessage) throws -> Bool - - public init(_ matcher: @escaping (Expression, FailureMessage) throws -> Bool) { - self.matcher = matcher - } - - public func matches(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool { - return try matcher(actualExpression, failureMessage) - } - - public func doesNotMatch(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool { - return try !matcher(actualExpression, failureMessage) - } - - /// 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. - public var predicate: Predicate { - return Predicate.fromDeprecatedMatcher(self) - } -} - -/// DEPRECATED: A convenience API to build matchers that don't need special negation -/// behavior. The toNot() behavior is the negation of to(). -/// -/// Unlike MatcherFunc, this will always fail if an expectation contains nil. -/// This applies regardless of using to() or toNot(). -/// -/// You may use this when implementing your own custom matchers. -/// -/// 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") -public struct NonNilMatcherFunc: Matcher { - public let matcher: (Expression, FailureMessage) throws -> Bool - - public init(_ matcher: @escaping (Expression, FailureMessage) throws -> Bool) { - self.matcher = matcher - } - - public func matches(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool { - let pass = try matcher(actualExpression, failureMessage) - if try attachNilErrorIfNeeded(actualExpression, failureMessage: failureMessage) { - return false - } - return pass - } - - public func doesNotMatch(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool { - let pass = try !matcher(actualExpression, failureMessage) - if try attachNilErrorIfNeeded(actualExpression, failureMessage: failureMessage) { - return false - } - return pass - } - - internal func attachNilErrorIfNeeded(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool { - if try actualExpression.evaluate() == nil { - failureMessage.postfixActual = " (use beNil() to match nils)" - return true - } - return false - } - - /// 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. - public var predicate: Predicate { - return Predicate.fromDeprecatedMatcher(self) - } -} diff --git a/Sources/Nimble/Matchers/MatcherProtocols.swift b/Sources/Nimble/Matchers/MatcherProtocols.swift index 21ffd7719..ee0e1dcc7 100644 --- a/Sources/Nimble/Matchers/MatcherProtocols.swift +++ b/Sources/Nimble/Matchers/MatcherProtocols.swift @@ -4,30 +4,6 @@ import Foundation import CoreGraphics #endif -/// Implement this protocol to implement a custom matcher for Swift -@available(*, deprecated, message: "Use Predicate instead") -public protocol Matcher { - associatedtype ValueType - func matches(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool - func doesNotMatch(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool -} - -extension Matcher { - var predicate: Predicate { - return Predicate.fromDeprecatedMatcher(self) - } - - var toClosure: (Expression, FailureMessage, Bool) throws -> Bool { - return { expr, msg, expectedResult in - if expectedResult { - return try self.matches(expr, failureMessage: msg) - } else { - return try self.doesNotMatch(expr, failureMessage: msg) - } - } - } -} - #if canImport(Darwin) /// Objective-C interface to the Swift variant of Matcher. @objc public protocol NMBMatcher { diff --git a/Sources/Nimble/Matchers/PostNotification.swift b/Sources/Nimble/Matchers/PostNotification.swift index cb2d595da..c1bfa730a 100644 --- a/Sources/Nimble/Matchers/PostNotification.swift +++ b/Sources/Nimble/Matchers/PostNotification.swift @@ -65,35 +65,3 @@ public func postNotifications( return result } } - -public func postNotifications( - _ notificationsMatcher: T, - fromNotificationCenter center: NotificationCenter = .default) - -> Predicate - where T: Matcher, T.ValueType == [Notification] { - _ = mainThread // Force lazy-loading of this value - let collector = NotificationCollector(notificationCenter: center) - collector.startObserving() - var once: Bool = false - - return Predicate { actualExpression in - let collectorNotificationsExpression = Expression(memoizedExpression: { _ in - return collector.observedNotifications - }, location: actualExpression.location, withoutCaching: true) - - assert(pthread_equal(mainThread, pthread_self()) != 0, "Only expecting closure to be evaluated on main thread.") - if !once { - once = true - _ = try actualExpression.evaluate() - } - - let failureMessage = FailureMessage() - let match = try notificationsMatcher.matches(collectorNotificationsExpression, failureMessage: failureMessage) - if collector.observedNotifications.isEmpty { - failureMessage.actualValue = "no notifications" - } else { - failureMessage.actualValue = "<\(stringify(collector.observedNotifications))>" - } - return PredicateResult(bool: match, message: failureMessage.toExpectationMessage()) - } -} diff --git a/Sources/Nimble/Matchers/Predicate.swift b/Sources/Nimble/Matchers/Predicate.swift index 2604411af..14675645f 100644 --- a/Sources/Nimble/Matchers/Predicate.swift +++ b/Sources/Nimble/Matchers/Predicate.swift @@ -166,55 +166,6 @@ 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 { - return Predicate { actual in - let failureMessage = 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 Predicate { actual in - let failureMessage = FailureMessage() - let result = try matcher(actual, failureMessage) - return PredicateResult( - status: PredicateStatus(bool: result), - message: failureMessage.toExpectationMessage() - ) - } - - } - - /// Compatibility layer for old Matcher API, deprecated. - /// Same as calling .predicate on a MatcherFunc or NonNilMatcherFunc type. - public static func fromDeprecatedMatcher(_ matcher: M) -> Predicate where M: Matcher, M.ValueType == T { - return self.fromDeprecatedFullClosure(matcher.toClosure) - } - - /// Deprecated Matcher API, use satisfies(_:_) instead - public func matches(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool { - let result = try satisfies(actualExpression) - result.message.update(failureMessage: failureMessage) - return result.toBoolean(expectation: .toMatch) - } - - /// Deprecated Matcher API, use satisfies(_:_) instead - public func doesNotMatch(_ actualExpression: Expression, failureMessage: FailureMessage) throws -> Bool { - let result = try satisfies(actualExpression) - result.message.update(failureMessage: failureMessage) - return result.toBoolean(expectation: .toNotMatch) - } -} - extension Predicate { // Someday, make this public? Needs documentation internal func after(f: @escaping (Expression, PredicateResult) throws -> PredicateResult) -> Predicate { diff --git a/Sources/Nimble/Matchers/SatisfyAllOf.swift b/Sources/Nimble/Matchers/SatisfyAllOf.swift index 15de060b9..983a93b78 100644 --- a/Sources/Nimble/Matchers/SatisfyAllOf.swift +++ b/Sources/Nimble/Matchers/SatisfyAllOf.swift @@ -6,12 +6,6 @@ public func satisfyAllOf(_ predicates: Predicate...) -> Predicate { return satisfyAllOf(predicates) } -/// A Nimble matcher that succeeds when the actual value matches with all of the matchers -/// provided in the variable list of matchers. -public func satisfyAllOf(_ matchers: U...) -> Predicate - where U: Matcher, U.ValueType == T { - return satisfyAllOf(matchers.map { $0.predicate }) -} internal func satisfyAllOf(_ predicates: [Predicate]) -> Predicate { return Predicate.define { actualExpression in diff --git a/Sources/Nimble/Matchers/SatisfyAnyOf.swift b/Sources/Nimble/Matchers/SatisfyAnyOf.swift index e8b379f15..5658dd48c 100644 --- a/Sources/Nimble/Matchers/SatisfyAnyOf.swift +++ b/Sources/Nimble/Matchers/SatisfyAnyOf.swift @@ -6,13 +6,6 @@ public func satisfyAnyOf(_ predicates: Predicate...) -> Predicate { return satisfyAnyOf(predicates) } -/// A Nimble matcher that succeeds when the actual value matches with any of the matchers -/// provided in the variable list of matchers. -public func satisfyAnyOf(_ matchers: U...) -> Predicate - where U: Matcher, U.ValueType == T { - return satisfyAnyOf(matchers.map { $0.predicate }) -} - internal func satisfyAnyOf(_ predicates: [Predicate]) -> Predicate { return Predicate.define { actualExpression in var postfixMessages = [String]() @@ -45,14 +38,6 @@ public func || (left: Predicate, right: Predicate) -> Predicate { return satisfyAnyOf(left, right) } -public func || (left: NonNilMatcherFunc, right: NonNilMatcherFunc) -> Predicate { - return satisfyAnyOf(left, right) -} - -public func || (left: MatcherFunc, right: MatcherFunc) -> Predicate { - return satisfyAnyOf(left, right) -} - #if canImport(Darwin) extension NMBObjCMatcher { @objc public class func satisfyAnyOfMatcher(_ matchers: [NMBMatcher]) -> NMBPredicate { From bf5fd68b381b8eae78ac7d3c665645d876406d30 Mon Sep 17 00:00:00 2001 From: Ben Chatelain Date: Sun, 3 May 2020 15:59:00 -0600 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A5=20Remove=20Matcher=20from=20te?= =?UTF-8?q?sts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tests/NimbleTests/SynchronousTest.swift | 54 +++++++-------------- Tests/NimbleTests/UserDescriptionTest.swift | 6 +-- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/Tests/NimbleTests/SynchronousTest.swift b/Tests/NimbleTests/SynchronousTest.swift index bd05da37b..8e2082249 100644 --- a/Tests/NimbleTests/SynchronousTest.swift +++ b/Tests/NimbleTests/SynchronousTest.swift @@ -29,38 +29,35 @@ final class SynchronousTest: XCTestCase { } func testToMatchesIfMatcherReturnsTrue() { - expect(1).to(MatcherFunc { _, _ in true }) - expect {1}.to(MatcherFunc { _, _ in true }) - - expect(1).to(MatcherFunc { _, _ in true }.predicate) - expect {1}.to(MatcherFunc { _, _ in true }.predicate) - expect(1).to(Predicate.simple("match") { _ in .matches }) expect {1}.to(Predicate.simple("match") { _ in .matches }) } func testToProvidesActualValueExpression() { var value: Int? - expect(1).to(MatcherFunc { expr, _ in value = try expr.evaluate(); return true }) + expect(1).to(Predicate.simple("match") { expr in + value = try expr.evaluate() + return .matches + }) expect(value).to(equal(1)) } func testToProvidesAMemoizedActualValueExpression() { var callCount = 0 - expect { callCount += 1 }.to(MatcherFunc { expr, _ in + expect { callCount += 1 }.to(Predicate.simple("match") { expr in _ = try expr.evaluate() _ = try expr.evaluate() - return true + return .matches }) expect(callCount).to(equal(1)) } func testToProvidesAMemoizedActualValueExpressionIsEvaluatedAtMatcherControl() { var callCount = 0 - expect { callCount += 1 }.to(MatcherFunc { expr, _ in + expect { callCount += 1 }.to(Predicate.simple("match") { expr in expect(callCount).to(equal(0)) _ = try expr.evaluate() - return true + return .matches }) expect(callCount).to(equal(1)) } @@ -74,69 +71,52 @@ final class SynchronousTest: XCTestCase { // repeated tests from to() for toNot() func testToNotMatchesIfMatcherReturnsTrue() { - expect(1).toNot(MatcherFunc { _, _ in false }) - expect {1}.toNot(MatcherFunc { _, _ in false }) - - expect(1).toNot(MatcherFunc { _, _ in false }.predicate) - expect {1}.toNot(MatcherFunc { _, _ in false }.predicate) - expect(1).toNot(Predicate.simple("match") { _ in .doesNotMatch }) expect {1}.toNot(Predicate.simple("match") { _ in .doesNotMatch }) } func testToNotProvidesActualValueExpression() { var value: Int? - expect(1).toNot(MatcherFunc { expr, _ in value = try expr.evaluate(); return false }) + expect(1).toNot(Predicate.simple("not match") { expr in + value = try expr.evaluate() + return .doesNotMatch + }) expect(value).to(equal(1)) } func testToNotProvidesAMemoizedActualValueExpression() { var callCount = 0 - expect { callCount += 1 }.toNot(MatcherFunc { expr, _ in + expect { callCount += 1 }.toNot(Predicate.simple("not match") { expr in _ = try expr.evaluate() _ = try expr.evaluate() - return false + return .doesNotMatch }) expect(callCount).to(equal(1)) } func testToNotProvidesAMemoizedActualValueExpressionIsEvaluatedAtMatcherControl() { var callCount = 0 - expect { callCount += 1 }.toNot(MatcherFunc { expr, _ in + expect { callCount += 1 }.toNot(Predicate.simple("not match") { expr in expect(callCount).to(equal(0)) _ = try expr.evaluate() - return false + return .doesNotMatch }) expect(callCount).to(equal(1)) } func testToNegativeMatches() { failsWithErrorMessage("expected to match, got <1>") { - expect(1).to(MatcherFunc { _, _ in false }) - } - failsWithErrorMessage("expected to match, got <1>") { - expect(1).to(MatcherFunc { _, _ in false }.predicate) - } - failsWithErrorMessage("expected to match, got <1>") { - expect(1).to(Predicate.simple("match") { _ in .doesNotMatch }) + expect(1).to(Predicate.simple("not match") { _ in .doesNotMatch }) } } func testToNotNegativeMatches() { - failsWithErrorMessage("expected to not match, got <1>") { - expect(1).toNot(MatcherFunc { _, _ in true }) - } - failsWithErrorMessage("expected to not match, got <1>") { - expect(1).toNot(MatcherFunc { _, _ in true }.predicate) - } failsWithErrorMessage("expected to not match, got <1>") { expect(1).toNot(Predicate.simple("match") { _ in .matches }) } } func testNotToMatchesLikeToNot() { - expect(1).notTo(MatcherFunc { _, _ in false }) - expect(1).notTo(MatcherFunc { _, _ in false }.predicate) expect(1).notTo(Predicate.simple("match") { _ in .doesNotMatch }) } } diff --git a/Tests/NimbleTests/UserDescriptionTest.swift b/Tests/NimbleTests/UserDescriptionTest.swift index fad6d8ec5..eb90ddef5 100644 --- a/Tests/NimbleTests/UserDescriptionTest.swift +++ b/Tests/NimbleTests/UserDescriptionTest.swift @@ -9,7 +9,7 @@ final class UserDescriptionTest: XCTestCase { expected to match, got <1> """ ) { - expect(1).to(MatcherFunc { _, _ in false }, description: "These aren't equal!") + expect(1).to(Predicate.simple("match") { _ in .matches }, description: "These aren't equal!") } } @@ -20,7 +20,7 @@ final class UserDescriptionTest: XCTestCase { expected to not match, got <1> """ ) { - expect(1).notTo(MatcherFunc { _, _ in true }, description: "These aren't equal!") + expect(1).notTo(Predicate.simple("match") { _ in .matches }, description: "These aren't equal!") } } @@ -31,7 +31,7 @@ final class UserDescriptionTest: XCTestCase { expected to not match, got <1> """ ) { - expect(1).toNot(MatcherFunc { _, _ in true }, description: "These aren't equal!") + expect(1).toNot(Predicate.simple("match") { _ in .matches }, description: "These aren't equal!") } }