Skip to content

Commit 2afc524

Browse files
authored
Merge pull request #895 from Quick/allpass-passfunc-nonnull-arg
[BREAKING] `passFunc` of `allPass` matcher now takes `S.Element` over `S.Element?`
2 parents 31be5d7 + ce497ae commit 2afc524

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ In Swift, the collection must be an instance of a type conforming to
11121112
// Swift
11131113
11141114
// Providing a custom function:
1115-
expect([1, 2, 3, 4]).to(allPass { $0! < 5 })
1115+
expect([1, 2, 3, 4]).to(allPass { $0 < 5 })
11161116
11171117
// Composing the expectation with another matcher:
11181118
expect([1, 2, 3, 4]).to(allPass(beLessThan(5)))

Sources/Nimble/Matchers/AllPass.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
public func allPass<S: Sequence>(
2-
_ passFunc: @escaping (S.Element?) throws -> Bool
2+
_ passFunc: @escaping (S.Element) throws -> Bool
33
) -> Predicate<S> {
4-
let matcher = Predicate.simpleNilable("pass a condition") { actualExpression in
5-
return PredicateStatus(bool: try passFunc(try actualExpression.evaluate()))
4+
let matcher = Predicate<S.Element>.define("pass a condition") { actualExpression, message in
5+
guard let actual = try actualExpression.evaluate() else {
6+
return PredicateResult(status: .fail, message: message)
7+
}
8+
return PredicateResult(bool: try passFunc(actual), message: message)
69
}
710
return createPredicate(matcher)
811
}
912

1013
public func allPass<S: Sequence>(
1114
_ passName: String,
12-
_ passFunc: @escaping (S.Element?) throws -> Bool
15+
_ passFunc: @escaping (S.Element) throws -> Bool
1316
) -> Predicate<S> {
14-
let matcher = Predicate.simpleNilable(passName) { actualExpression in
15-
return PredicateStatus(bool: try passFunc(try actualExpression.evaluate()))
17+
let matcher = Predicate<S.Element>.define(passName) { actualExpression, message in
18+
guard let actual = try actualExpression.evaluate() else {
19+
return PredicateResult(status: .fail, message: message)
20+
}
21+
return PredicateResult(bool: try passFunc(actual), message: message)
1622
}
1723
return createPredicate(matcher)
1824
}
@@ -33,7 +39,9 @@ private func createPredicate<S: Sequence>(_ elementMatcher: Predicate<S.Element>
3339
var failure: ExpectationMessage = .expectedTo("all pass")
3440
for currentElement in actualValue {
3541
let exp = Expression(
36-
expression: {currentElement}, location: actualExpression.location)
42+
expression: { currentElement },
43+
location: actualExpression.location
44+
)
3745
let predicateResult = try elementMatcher.satisfies(exp)
3846
if predicateResult.status == .matches {
3947
failure = predicateResult.message.prepended(expectation: "all ")

Tests/NimbleTests/Matchers/AllPassTest.swift

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ extension Optional where Wrapped: Comparable {
4444

4545
final class AllPassTest: XCTestCase {
4646
func testAllPassArray() {
47-
expect([1, 2, 3, 4]).to(allPass({$0 < 5}))
48-
expect([1, 2, 3, 4]).toNot(allPass({$0 > 5}))
47+
expect([1, 2, 3, 4]).to(allPass { $0 < 5 })
48+
expect([1, 2, 3, 4]).toNot(allPass { $0 > 5 })
4949

5050
failsWithErrorMessage(
5151
"expected to all pass a condition, but failed first at element <3> in <[1, 2, 3, 4]>") {
52-
expect([1, 2, 3, 4]).to(allPass({$0 < 3}))
52+
expect([1, 2, 3, 4]).to(allPass { $0 < 3 })
5353
}
5454
failsWithErrorMessage("expected to not all pass a condition") {
55-
expect([1, 2, 3, 4]).toNot(allPass({$0 < 5}))
55+
expect([1, 2, 3, 4]).toNot(allPass { $0 < 5 })
5656
}
5757
failsWithErrorMessage(
5858
"expected to all be something, but failed first at element <3> in <[1, 2, 3, 4]>") {
@@ -78,31 +78,24 @@ final class AllPassTest: XCTestCase {
7878

7979
func testAllPassCollectionsWithOptionals() {
8080
expect([nil, nil, nil] as [Int?]).to(allPass(beNil()))
81-
82-
failsWithErrorMessage("expected to all pass a condition, but failed first at element <nil> in <[nil, nil, nil]>") {
83-
expect([nil, nil, nil] as [Int?]).to(allPass({$0 == nil}))
84-
}
85-
}
86-
87-
func testAllPassCollectionsWithOptionalsUnwrappingOneOptionalLayer() {
88-
expect([nil, nil, nil] as [Int?]).to(allPass({$0! == nil}))
89-
expect([nil, 1, nil] as [Int?]).toNot(allPass({$0! == nil}))
90-
expect([1, 1, 1] as [Int?]).to(allPass({$0! == 1}))
91-
expect([1, 1, nil] as [Int?]).toNot(allPass({$0! == 1}))
92-
expect([1, 2, 3] as [Int?]).to(allPass({$0! < 4}))
93-
expect([1, 2, 3] as [Int?]).toNot(allPass({$0! < 3}))
94-
expect([1, 2, nil] as [Int?]).to(allPass({$0! < 3}))
81+
expect([nil, nil, nil] as [Int?]).to(allPass { $0 == nil })
82+
expect([nil, 1, nil] as [Int?]).toNot(allPass { $0 == nil })
83+
expect([1, 1, 1] as [Int?]).to(allPass { $0 == 1 })
84+
expect([1, 1, nil] as [Int?]).toNot(allPass { $0 == 1 })
85+
expect([1, 2, 3] as [Int?]).to(allPass { $0 < 4 })
86+
expect([1, 2, 3] as [Int?]).toNot(allPass { $0 < 3 })
87+
expect([1, 2, nil] as [Int?]).to(allPass { $0 < 3 })
9588
}
9689

9790
func testAllPassSet() {
98-
expect(Set([1, 2, 3, 4])).to(allPass({$0 < 5}))
99-
expect(Set([1, 2, 3, 4])).toNot(allPass({$0 > 5}))
91+
expect(Set([1, 2, 3, 4])).to(allPass { $0 < 5 })
92+
expect(Set([1, 2, 3, 4])).toNot(allPass { $0 > 5 })
10093

10194
failsWithErrorMessage("expected to not all pass a condition") {
102-
expect(Set([1, 2, 3, 4])).toNot(allPass({$0 < 5}))
95+
expect(Set([1, 2, 3, 4])).toNot(allPass { $0 < 5 })
10396
}
10497
failsWithErrorMessage("expected to not all be something") {
105-
expect(Set([1, 2, 3, 4])).toNot(allPass("be something", {$0 < 5}))
98+
expect(Set([1, 2, 3, 4])).toNot(allPass("be something") { $0 < 5 })
10699
}
107100
}
108101

0 commit comments

Comments
 (0)