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

[7.x] Refactor contain matcher using Predicate.simple #573

Merged
merged 2 commits into from
Jul 18, 2018
Merged
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
42 changes: 21 additions & 21 deletions Sources/Nimble/Matchers/Contain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public func contain<S: Sequence, T: Equatable>(_ items: T...) -> Predicate<S>

public func contain<S: Sequence, T: Equatable>(_ items: [T]) -> Predicate<S>
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary in this PR, but wondering if we can rename this to allSatisfy and make it a compatibility shim for Swift < 4.2? https://github.com/apple/swift-evolution/blob/master/proposals/0207-containsOnly.md

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally makes sense 👍

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.
Expand All @@ -25,16 +25,16 @@ public func contain(_ substrings: String...) -> Predicate<String> {
}

public func contain(_ substrings: [String]) -> Predicate<String> {
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.
Expand All @@ -43,13 +43,13 @@ public func contain(_ substrings: NSString...) -> Predicate<NSString> {
}

public func contain(_ substrings: [NSString]) -> Predicate<NSString> {
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.
Expand All @@ -58,13 +58,13 @@ public func contain(_ items: Any?...) -> Predicate<NMBContainer> {
}

public func contain(_ items: [Any?]) -> Predicate<NMBContainer> {
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 item != nil && actual.contains(item!)
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.map { actual.contains($0) } ?? false
}
}.requireNonNil
return PredicateStatus(bool: matches)
}
}

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
Expand Down