Skip to content

Commit c638ef9

Browse files
authored
Fix regression where be and beIdenticalTo matchers stopped matching against AnyObject protocols (#1183)
* Fix regression where be and beIdenticalTo matchers stopped matching against AnyObject protocols * Fix build issue on older versions of swift
1 parent 6de3456 commit c638ef9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Sources/Nimble/Matchers/BeIdenticalTo.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
/// A Nimble matcher that succeeds when the actual value is the same instance
22
/// as the expected instance.
33
public func beIdenticalTo<T: AnyObject>(_ expected: T?) -> Matcher<T> {
4+
_beIdenticalTo(expected)
5+
}
6+
7+
/// A Nimble matcher that succeeds when the actual value is the same instance
8+
/// as the expected instance.
9+
public func beIdenticalTo(_ expected: AnyObject?) -> Matcher<AnyObject> {
10+
_beIdenticalTo(expected)
11+
}
12+
13+
private func _beIdenticalTo<T: AnyObject>(_ expected: T?) -> Matcher<T> {
414
return Matcher.define { actualExpression in
515
let actual = try actualExpression.evaluate()
616

@@ -36,7 +46,15 @@ public func !== (lhs: AsyncExpectation<AnyObject>, rhs: AnyObject?) async {
3646
///
3747
/// Alias for "beIdenticalTo".
3848
public func be<T: AnyObject>(_ expected: T?) -> Matcher<T> {
39-
return beIdenticalTo(expected)
49+
return _beIdenticalTo(expected)
50+
}
51+
52+
/// A Nimble matcher that succeeds when the actual value is the same instance
53+
/// as the expected instance.
54+
///
55+
/// Alias for "beIdenticalTo".
56+
public func be(_ expected: AnyObject?) -> Matcher<AnyObject> {
57+
return _beIdenticalTo(expected)
4058
}
4159

4260
#if canImport(Darwin)

Tests/NimbleTests/Matchers/BeIdenticalToObjectTest.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@ final class BeIdenticalToObjectTest: XCTestCase {
1212

1313
func testBeIdenticalToPositive() {
1414
expect(self.testObjectA).to(beIdenticalTo(testObjectA))
15+
}
16+
17+
func testbeIdenticalToAsSubmatcher() {
1518
// check that the typing works out when used as a submatcher.
19+
expect(self.testObjectA).to(map({ $0 }, be(testObjectA)))
1620
expect(self.testObjectA).to(map({ $0 }, beIdenticalTo(testObjectA)))
1721
}
1822

23+
func testBeIdenticalToAnyObjectProtocol() {
24+
let object = AnObjectImplementation()
25+
26+
expect(object as AnObjectProtocol).to(be(object))
27+
expect(object as AnObjectProtocol).to(beIdenticalTo(object))
28+
}
29+
1930
func testBeIdenticalToNegative() {
2031
expect(self.testObjectA).toNot(beIdenticalTo(testObjectB))
2132
}
@@ -54,5 +65,9 @@ final class BeIdenticalToObjectTest: XCTestCase {
5465
expect(self.testObjectA) === testObjectA
5566
expect(self.testObjectA) !== testObjectB
5667
}
68+
}
5769

70+
private protocol AnObjectProtocol: AnyObject {}
71+
private final class AnObjectImplementation: AnObjectProtocol {
72+
init() {}
5873
}

0 commit comments

Comments
 (0)