Skip to content

Commit 5eff5d5

Browse files
committed
Support operator overloads for beNil matcher
1 parent c69d8cb commit 5eff5d5

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Sources/Nimble/Matchers/BeNil.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ public func beNil<T>() -> Predicate<T> {
66
}
77
}
88

9+
extension Expectation {
10+
/// Represents `nil` value to be used with the operator overloads for `beNil`.
11+
public struct Nil: ExpressibleByNilLiteral {
12+
public init(nilLiteral: ()) {}
13+
}
14+
15+
public static func == (lhs: Expectation, rhs: Expectation.Nil) {
16+
lhs.to(beNil())
17+
}
18+
19+
public static func != (lhs: Expectation, rhs: Expectation.Nil) {
20+
lhs.toNot(beNil())
21+
}
22+
}
23+
924
#if canImport(Darwin)
1025
import Foundation
1126

Tests/NimbleTests/Matchers/BeNilTest.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,32 @@ final class BeNilTest: XCTestCase {
88

99
func testBeNil() {
1010
expect(nil as Int?).to(beNil())
11+
expect(nil as Int?) == nil
12+
1113
expect(1 as Int?).toNot(beNil())
14+
expect(1 as Int?) != nil
15+
1216
expect(self.producesNil()).to(beNil())
17+
expect(self.producesNil()) == nil
1318

14-
failsWithErrorMessage("expected to not be nil, got <nil>") {
15-
expect(nil as Int?).toNot(beNil())
19+
do {
20+
let message = "expected to not be nil, got <nil>"
21+
failsWithErrorMessage(message) {
22+
expect(nil as Int?).toNot(beNil())
23+
}
24+
failsWithErrorMessage(message) {
25+
expect(nil as Int?) != nil
26+
}
1627
}
1728

18-
failsWithErrorMessage("expected to be nil, got <1>") {
19-
expect(1 as Int?).to(beNil())
29+
do {
30+
let message = "expected to be nil, got <1>"
31+
failsWithErrorMessage(message) {
32+
expect(1 as Int?).to(beNil())
33+
}
34+
failsWithErrorMessage(message) {
35+
expect(1 as Int?) == nil
36+
}
2037
}
2138
}
2239
}

0 commit comments

Comments
 (0)