Skip to content

Commit 684f13b

Browse files
committed
[BREAKING] Make beCloseTo matcher generic to fix a build error when used with allPass matcher on Swift 5.3
This suppresses a build error on Swift 5.3 like: `Global function 'allPass' requires the types 'Date' and 'NMBDoubleConvertible' be equivalent`.
1 parent 1dc2439 commit 684f13b

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

Sources/Nimble/Matchers/BeCloseTo.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@ internal func isCloseTo(_ actualValue: NMBDoubleConvertible?,
1919
/// point values which can have imprecise results when doing arithmetic on them.
2020
///
2121
/// @see equal
22-
public func beCloseTo(_ expectedValue: Double, within delta: Double = DefaultDelta) -> Predicate<Double> {
22+
public func beCloseTo<Value: NMBDoubleConvertible>(_ expectedValue: Value, within delta: Double = DefaultDelta) -> Predicate<Value> {
2323
return Predicate.define { actualExpression in
2424
return isCloseTo(try actualExpression.evaluate(), expectedValue: expectedValue, delta: delta)
2525
}
2626
}
2727

28-
/// A Nimble matcher that succeeds when a value is close to another. This is used for floating
29-
/// point values which can have imprecise results when doing arithmetic on them.
30-
///
31-
/// @see equal
32-
public func beCloseTo(_ expectedValue: NMBDoubleConvertible, within delta: Double = DefaultDelta) -> Predicate<NMBDoubleConvertible> {
28+
private func beCloseTo(_ expectedValue: NMBDoubleConvertible, within delta: Double = DefaultDelta) -> Predicate<NMBDoubleConvertible> {
3329
return Predicate.define { actualExpression in
3430
return isCloseTo(try actualExpression.evaluate(), expectedValue: expectedValue, delta: delta)
3531
}

Tests/NimbleTests/Matchers/BeCloseToTest.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ final class BeCloseToTest: XCTestCase {
5353
expect(NSDate(dateTimeString: "2015-08-26 11:43:00")).to(beCloseTo(NSDate(dateTimeString: "2015-08-26 11:43:05"), within: 10))
5454

5555
failsWithErrorMessage("expected to not be close to <2015-08-26 11:43:00.0050> (within 0.006), got <2015-08-26 11:43:00.0000>") {
56-
let expectedDate = NSDate(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
56+
// Cast to NSDate is needed for Linux (swift-corelibs-foundation) compatibility.
57+
let expectedDate = NSDate(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005) as NSDate
5758
expect(NSDate(dateTimeString: "2015-08-26 11:43:00")).toNot(beCloseTo(expectedDate, within: 0.006))
5859
}
5960
}
@@ -139,4 +140,10 @@ final class BeCloseToTest: XCTestCase {
139140
expect([0.1, 1.2]).to(beCloseTo([0.3, 1.3], within: 0.1))
140141
}
141142
}
143+
144+
// https://github.com/Quick/Nimble/issues/831
145+
func testCombinationWithAllPass() {
146+
let values: [NSNumber] = [0]
147+
expect(values).to(allPass(beCloseTo(0)))
148+
}
142149
}

0 commit comments

Comments
 (0)