Skip to content

Commit

Permalink
Merge pull request #894 from Quick/benil-for-nested-optional
Browse files Browse the repository at this point in the history
Support nested optionals in `beNil` matcher
  • Loading branch information
ikesyo authored May 24, 2021
2 parents 6890514 + 2fbaec2 commit 31be5d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 12 additions & 0 deletions Sources/Nimble/Matchers/BeNil.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
/// A protocol which represents whether the value is nil or not.
private protocol _OptionalProtocol {
var isNil: Bool { get }
}

extension Optional: _OptionalProtocol {
var isNil: Bool { self == nil }
}

/// A Nimble matcher that succeeds when the actual value is nil.
public func beNil<T>() -> Predicate<T> {
return Predicate.simpleNilable("be nil") { actualExpression in
let actualValue = try actualExpression.evaluate()
if let actual = actualValue, let nestedOptionl = actual as? _OptionalProtocol {
return PredicateStatus(bool: nestedOptionl.isNil)
}
return PredicateStatus(bool: actualValue == nil)
}
}
Expand Down
7 changes: 3 additions & 4 deletions Tests/NimbleTests/Matchers/AllPassTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ final class AllPassTest: XCTestCase {
}
}

func testAllPassCollectionsWithOptionalsDontWork() {
failsWithErrorMessage("expected to all be nil, but failed first at element <nil> in <[nil, nil, nil]>") {
expect([nil, nil, nil] as [Int?]).to(allPass(beNil()))
}
func testAllPassCollectionsWithOptionals() {
expect([nil, nil, nil] as [Int?]).to(allPass(beNil()))

failsWithErrorMessage("expected to all pass a condition, but failed first at element <nil> in <[nil, nil, nil]>") {
expect([nil, nil, nil] as [Int?]).to(allPass({$0 == nil}))
}
Expand Down

0 comments on commit 31be5d7

Please sign in to comment.