From 39db3c9a651590afcf529cd3bb26b80ce67e3527 Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Thu, 12 Jul 2018 03:44:03 +0900 Subject: [PATCH 1/2] Refactor `contain` matcher using `Predicate.simple` Replacing `Predicate.fromDeprecatedClosure`. --- Sources/Nimble/Matchers/Contain.swift | 40 +++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Sources/Nimble/Matchers/Contain.swift b/Sources/Nimble/Matchers/Contain.swift index 3517dc178..d92a80cfc 100644 --- a/Sources/Nimble/Matchers/Contain.swift +++ b/Sources/Nimble/Matchers/Contain.swift @@ -8,15 +8,15 @@ public func contain(_ items: T...) -> Predicate public func contain(_ items: [T]) -> Predicate where S.Iterator.Element == T { - return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in - failureMessage.postfixMessage = "contain <\(arrayAsString(items))>" + return Predicate.simple("contain <\(arrayAsString(items))>") { actualExpression in if let actual = try actualExpression.evaluate() { - return items.all { + let matches = items.all { return actual.contains($0) } + return PredicateStatus(bool: matches) } - return false - }.requireNonNil + return .fail + } } /// A Nimble matcher that succeeds when the actual string contains the expected substring. @@ -25,16 +25,16 @@ public func contain(_ substrings: String...) -> Predicate { } public func contain(_ substrings: [String]) -> Predicate { - return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in - failureMessage.postfixMessage = "contain <\(arrayAsString(substrings))>" + return Predicate.simple("contain <\(arrayAsString(substrings))>") { actualExpression in if let actual = try actualExpression.evaluate() { - return substrings.all { + let matches = substrings.all { let range = actual.range(of: $0) return range != nil && !range!.isEmpty } + return PredicateStatus(bool: matches) } - return false - }.requireNonNil + return .fail + } } /// A Nimble matcher that succeeds when the actual string contains the expected substring. @@ -43,13 +43,13 @@ public func contain(_ substrings: NSString...) -> Predicate { } public func contain(_ substrings: [NSString]) -> Predicate { - return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in - failureMessage.postfixMessage = "contain <\(arrayAsString(substrings))>" + return Predicate.simple("contain <\(arrayAsString(substrings))>") { actualExpression in if let actual = try actualExpression.evaluate() { - return substrings.all { actual.range(of: $0.description).length != 0 } + let matches = substrings.all { actual.range(of: $0.description).length != 0 } + return PredicateStatus(bool: matches) } - return false - }.requireNonNil + return .fail + } } /// A Nimble matcher that succeeds when the actual collection contains the expected object. @@ -58,13 +58,13 @@ public func contain(_ items: Any?...) -> Predicate { } public func contain(_ items: [Any?]) -> Predicate { - return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in - failureMessage.postfixMessage = "contain <\(arrayAsString(items))>" - guard let actual = try actualExpression.evaluate() else { return false } - return items.all { item in + return Predicate.simple("contain <\(arrayAsString(items))>") { actualExpression in + guard let actual = try actualExpression.evaluate() else { return .fail } + let matches = items.all { item in return item != nil && actual.contains(item!) } - }.requireNonNil + return PredicateStatus(bool: matches) + } } #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) From 9bbe0982e3c76db2feb73f161a008e5cda249424 Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Wed, 18 Jul 2018 13:38:29 +0900 Subject: [PATCH 2/2] Avoid forced unwrapping --- Sources/Nimble/Matchers/Contain.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Nimble/Matchers/Contain.swift b/Sources/Nimble/Matchers/Contain.swift index d92a80cfc..2c84c5c72 100644 --- a/Sources/Nimble/Matchers/Contain.swift +++ b/Sources/Nimble/Matchers/Contain.swift @@ -61,7 +61,7 @@ public func contain(_ items: [Any?]) -> Predicate { return Predicate.simple("contain <\(arrayAsString(items))>") { actualExpression in guard let actual = try actualExpression.evaluate() else { return .fail } let matches = items.all { item in - return item != nil && actual.contains(item!) + return item.map { actual.contains($0) } ?? false } return PredicateStatus(bool: matches) }