Skip to content

Commit

Permalink
Merge pull request Quick#573 from Quick/refactor-contain-matcher
Browse files Browse the repository at this point in the history
[7.x] Refactor `contain` matcher using `Predicate.simple`
  • Loading branch information
ikesyo authored Jul 18, 2018
2 parents 978636e + d9c1acf commit 97dd8bf
Showing 1 changed file with 21 additions and 21 deletions.
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 {
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

0 comments on commit 97dd8bf

Please sign in to comment.