Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #557 from LucianoPAlmeida/master
Sequence elementsEqual matcher
- Loading branch information
Showing
4 changed files
with
66 additions
and
0 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,16 @@ | ||
/// A Nimble matcher that succeeds when the actual sequence contain the same elements in the same order to the exepected sequence. | ||
public func elementsEqual<S: Sequence>(_ expectedValue: S?) -> Predicate<S> where S.Element: Equatable { | ||
// A matcher abstraction for https://developer.apple.com/documentation/swift/sequence/2949668-elementsequal | ||
return Predicate.define("elementsEqual <\(stringify(expectedValue))>") { (actualExpression, msg) in | ||
let actualValue = try actualExpression.evaluate() | ||
switch (expectedValue, actualValue) { | ||
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 = expected.elementsEqual(actual) | ||
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,27 @@ | ||
import Foundation | ||
import XCTest | ||
import Nimble | ||
|
||
final class ElementsEqualTest: XCTestCase, XCTestCaseProvider { | ||
|
||
func testSequenceElementsEquality() { | ||
failsWithErrorMessageForNil("expected to elementsEqual <nil>, got <nil>") { | ||
expect(nil as [Int]?).to(elementsEqual(nil as [Int]?)) | ||
} | ||
let sequence = [1, 2] | ||
failsWithErrorMessageForNil("expected to elementsEqual <[1, 2]>, got <nil>") { | ||
expect(nil as [Int]?).to(elementsEqual(sequence)) | ||
} | ||
|
||
failsWithErrorMessageForNil("expected to elementsEqual <nil>, got <[1, 2]>") { | ||
expect(sequence).to(elementsEqual(nil as [Int]?)) | ||
} | ||
|
||
let sequence1 = [1, 2, 3] | ||
let sequence2 = [1, 2, 3, 4, 5] | ||
expect(sequence1).toNot(elementsEqual(sequence2)) | ||
expect(sequence1).toNot(elementsEqual([3, 2, 1])) | ||
expect(sequence1).to(elementsEqual([1, 2, 3])) | ||
|
||
} | ||
} |
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