Skip to content

Commit 31be5d7

Browse files
authored
Merge pull request #894 from Quick/benil-for-nested-optional
Support nested optionals in `beNil` matcher
2 parents 6890514 + 2fbaec2 commit 31be5d7

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Sources/Nimble/Matchers/BeNil.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
/// A protocol which represents whether the value is nil or not.
2+
private protocol _OptionalProtocol {
3+
var isNil: Bool { get }
4+
}
5+
6+
extension Optional: _OptionalProtocol {
7+
var isNil: Bool { self == nil }
8+
}
9+
110
/// A Nimble matcher that succeeds when the actual value is nil.
211
public func beNil<T>() -> Predicate<T> {
312
return Predicate.simpleNilable("be nil") { actualExpression in
413
let actualValue = try actualExpression.evaluate()
14+
if let actual = actualValue, let nestedOptionl = actual as? _OptionalProtocol {
15+
return PredicateStatus(bool: nestedOptionl.isNil)
16+
}
517
return PredicateStatus(bool: actualValue == nil)
618
}
719
}

Tests/NimbleTests/Matchers/AllPassTest.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ final class AllPassTest: XCTestCase {
7676
}
7777
}
7878

79-
func testAllPassCollectionsWithOptionalsDontWork() {
80-
failsWithErrorMessage("expected to all be nil, but failed first at element <nil> in <[nil, nil, nil]>") {
81-
expect([nil, nil, nil] as [Int?]).to(allPass(beNil()))
82-
}
79+
func testAllPassCollectionsWithOptionals() {
80+
expect([nil, nil, nil] as [Int?]).to(allPass(beNil()))
81+
8382
failsWithErrorMessage("expected to all pass a condition, but failed first at element <nil> in <[nil, nil, nil]>") {
8483
expect([nil, nil, nil] as [Int?]).to(allPass({$0 == nil}))
8584
}

0 commit comments

Comments
 (0)