Skip to content

Commit

Permalink
Merge pull request #570 from Quick/refactor-endwith-matcher
Browse files Browse the repository at this point in the history
[7.x] Refactor `endWith` matcher using `Predicate.simple`
  • Loading branch information
ikesyo authored Jul 13, 2018
2 parents a96ac9b + 25b9121 commit 497a9ec
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions Sources/Nimble/Matchers/EndWith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import Foundation
/// is equal to the expected value.
public func endWith<S: Sequence, T: Equatable>(_ endingElement: T) -> Predicate<S>
where S.Iterator.Element == T {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in
failureMessage.postfixMessage = "end with <\(endingElement)>"

return Predicate.simple("end with <\(endingElement)>") { actualExpression in
if let actualValue = try actualExpression.evaluate() {
var actualGenerator = actualValue.makeIterator()
var lastItem: T?
Expand All @@ -16,42 +14,40 @@ public func endWith<S: Sequence, T: Equatable>(_ endingElement: T) -> Predicate<
item = actualGenerator.next()
} while(item != nil)

return lastItem == endingElement
return PredicateStatus(bool: lastItem == endingElement)
}
return false
}.requireNonNil
return .fail
}
}

/// A Nimble matcher that succeeds when the actual collection's last element
/// is equal to the expected object.
public func endWith(_ endingElement: Any) -> Predicate<NMBOrderedCollection> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in
failureMessage.postfixMessage = "end with <\(endingElement)>"
guard let collection = try actualExpression.evaluate() else { return false }
guard collection.count > 0 else { return false }
return Predicate.simple("end with <\(endingElement)>") { actualExpression in
guard let collection = try actualExpression.evaluate() else { return .fail }
guard collection.count > 0 else { return PredicateStatus(bool: false) }
#if os(Linux)
guard let collectionValue = collection.object(at: collection.count - 1) as? NSObject else {
return false
return .fail
}
#else
let collectionValue = collection.object(at: collection.count - 1) as AnyObject
#endif

return collectionValue.isEqual(endingElement)
}.requireNonNil
return PredicateStatus(bool: collectionValue.isEqual(endingElement))
}
}

/// A Nimble matcher that succeeds when the actual string contains the expected substring
/// where the expected substring's location is the actual string's length minus the
/// expected substring's length.
public func endWith(_ endingSubstring: String) -> Predicate<String> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in
failureMessage.postfixMessage = "end with <\(endingSubstring)>"
return Predicate.simple("end with <\(endingSubstring)>") { actualExpression in
if let collection = try actualExpression.evaluate() {
return collection.hasSuffix(endingSubstring)
return PredicateStatus(bool: collection.hasSuffix(endingSubstring))
}
return false
}.requireNonNil
return .fail
}
}

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

0 comments on commit 497a9ec

Please sign in to comment.