Skip to content

Commit

Permalink
Refactor containElementSatisfying matcher using Predicate.define
Browse files Browse the repository at this point in the history
Replacing `Predicate.fromDeprecatedClosure`.
  • Loading branch information
ikesyo committed Jul 10, 2018
1 parent dc046d2 commit bc3476b
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Sources/Nimble/Matchers/ContainElementSatisfying.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ import Foundation

public func containElementSatisfying<S: Sequence, T>(_ predicate: @escaping ((T) -> Bool), _ predicateDescription: String = "") -> Predicate<S> where S.Iterator.Element == T {

return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in
failureMessage.actualValue = nil

return Predicate.define { actualExpression in
let message: ExpectationMessage
if predicateDescription == "" {
failureMessage.postfixMessage = "find object in collection that satisfies predicate"
message = .expectedTo("find object in collection that satisfies predicate")
} else {
failureMessage.postfixMessage = "find object in collection \(predicateDescription)"
message = .expectedTo("find object in collection \(predicateDescription)")
}

if let sequence = try actualExpression.evaluate() {
for object in sequence {
if predicate(object) {
return true
return PredicateResult(bool: true, message: message)
}
}

return false
return PredicateResult(bool: false, message: message)
}

return false
}.requireNonNil
return PredicateResult(status: .fail, message: message)
}
}

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

0 comments on commit bc3476b

Please sign in to comment.