-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding StartWith matcher for sequence prefixes
- Loading branch information
Showing
3 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Foundation | ||
|
||
/// A Nimble matcher that succeeds when the exepected sequence is a prefix of the actual sequence. | ||
/// | ||
/// This is a matcher abstraction for https://developer.apple.com/documentation/swift/sequence/2854218-starts | ||
public func beginWith<Seq1: Sequence, Seq2: Sequence>(prefix expectedPrefix: Seq2?) | ||
-> Predicate<Seq1> where Seq1.Element: Equatable, Seq1.Element == Seq2.Element { | ||
return Predicate.define("beginWith <\(stringify(expectedPrefix))>") { (actualExpression, msg) in | ||
let actualPrefix = try actualExpression.evaluate() | ||
switch (expectedPrefix, actualPrefix) { | ||
case (nil, _?): | ||
return PredicateResult(status: .fail, message: msg.appendedBeNilHint()) | ||
case (nil, nil), (_, nil): | ||
return PredicateResult(status: .fail, message: msg) | ||
case (let expected?, let actual?): | ||
let matches = actual.starts(with: expected) | ||
return PredicateResult(bool: matches, message: msg) | ||
} | ||
} | ||
} | ||
|
||
/// A Nimble matcher that succeeds when the expected sequence is the prefix of the actual sequence, using the given predicate as the equivalence test. | ||
/// | ||
/// This is a matcher abstraction for https://developer.apple.com/documentation/swift/sequence/2996828-starts | ||
public func beginWith<Seq1: Sequence, Seq2: Sequence>( | ||
prefix expectedPrefix: Seq2?, | ||
by areEquivalent: @escaping (Seq1.Element, Seq2.Element) -> Bool | ||
) -> Predicate<Seq1> { | ||
return Predicate.define("beginWith <\(stringify(expectedPrefix))>") { (actualExpression, msg) in | ||
let actualPrefix = try actualExpression.evaluate() | ||
switch (expectedPrefix, actualPrefix) { | ||
case (nil, _?): | ||
return PredicateResult(status: .fail, message: msg.appendedBeNilHint()) | ||
case (nil, nil), (_, nil): | ||
return PredicateResult(status: .fail, message: msg) | ||
case (let expected?, let actual?): | ||
let matches = actual.starts(with: expected, by: areEquivalent) | ||
return PredicateResult(bool: matches, message: msg) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Foundation | ||
import Nimble | ||
import XCTest | ||
|
||
final class BeginWithPrefixTest: XCTestCase { | ||
|
||
func testBeginWithSequencePrefix() { | ||
failsWithErrorMessageForNil("expected to beginWith <nil>, got <nil>") { | ||
expect(nil as [Int]?).to(beginWith(prefix: nil as [Int]?)) | ||
} | ||
|
||
failsWithErrorMessageForNil("expected to beginWith <[1, 2]>, got <nil>") { | ||
expect(nil as [Int]?).to(beginWith(prefix: [1, 2])) | ||
} | ||
|
||
failsWithErrorMessageForNil("expected to beginWith <nil>, got <[1, 2]>") { | ||
expect([1, 2]).to(beginWith(prefix: nil as [Int]?)) | ||
} | ||
|
||
let sequence = [1, 2, 3] | ||
expect(sequence).toNot(beginWith(prefix: [1, 2, 3, 4])) | ||
expect(sequence).toNot(beginWith(prefix: [2, 3])) | ||
|
||
expect(sequence).to(beginWith(prefix: [1, 2, 3])) | ||
expect(sequence).to(beginWith(prefix: [1, 2])) | ||
expect(sequence).to(beginWith(prefix: [])) | ||
|
||
expect([]).toNot(beginWith(prefix: [1])) | ||
expect([]).to(beginWith(prefix: [] as [Int])) | ||
} | ||
|
||
func testBeginWithSequencePrefixUsingPredicateClosure() { | ||
failsWithErrorMessageForNil("expected to beginWith <nil>, got <nil>") { | ||
expect(nil as [Int]?).to(beginWith(prefix: nil as [Int]?, by: { $0 == $1 })) | ||
} | ||
|
||
failsWithErrorMessageForNil("expected to beginWith <[1, 2]>, got <nil>") { | ||
expect(nil as [Int]?).to(beginWith(prefix: [1, 2], by: { $0 == $1 })) | ||
} | ||
|
||
failsWithErrorMessageForNil("expected to beginWith <nil>, got <[1, 2]>") { | ||
expect([1, 2]).to(beginWith(prefix: nil as [Int]?, by: { $0 == $1 })) | ||
} | ||
|
||
let sequence = [1, 2, 3] | ||
expect(sequence).toNot(beginWith(prefix: [1, 2, 3, 4], by: { $0 == $1 })) | ||
expect(sequence).toNot(beginWith(prefix: [2, 3], by: { $0 == $1 })) | ||
|
||
expect(sequence).to(beginWith(prefix: [1, 2, 3], by: { $0 == $1 })) | ||
expect(sequence).to(beginWith(prefix: [1, 2], by: { $0 == $1 })) | ||
expect(sequence).to(beginWith(prefix: [], by: { $0 == $1 })) | ||
|
||
expect([]).toNot(beginWith(prefix: [1], by: { $0 == $1 })) | ||
expect([]).to(beginWith(prefix: [] as [Int], by: { $0 == $1 })) | ||
} | ||
|
||
func testBeginWithSequencePrefixWithDifferentSequenceTypes() { | ||
expect(1...3).to(beginWith(prefix: [1, 2, 3])) | ||
expect(1...3).toNot(beginWith(prefix: [1, 2, 3, 4, 5])) | ||
|
||
expect(1...3).to(beginWith(prefix: [1, 2, 3], by: { $0 == $1 })) | ||
expect(1...3).toNot(beginWith(prefix: [1, 2, 3, 4, 5], by: { $0 == $1 })) | ||
} | ||
} |