Skip to content

Commit

Permalink
Fix regression where be and beIdenticalTo matchers stopped matching a…
Browse files Browse the repository at this point in the history
…gainst AnyObject protocols (#1183)

* Fix regression where be and beIdenticalTo matchers stopped matching against AnyObject protocols

* Fix build issue on older versions of swift
  • Loading branch information
younata authored Dec 16, 2024
1 parent 6de3456 commit c638ef9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Sources/Nimble/Matchers/BeIdenticalTo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/// A Nimble matcher that succeeds when the actual value is the same instance
/// as the expected instance.
public func beIdenticalTo<T: AnyObject>(_ expected: T?) -> Matcher<T> {
_beIdenticalTo(expected)
}

/// A Nimble matcher that succeeds when the actual value is the same instance
/// as the expected instance.
public func beIdenticalTo(_ expected: AnyObject?) -> Matcher<AnyObject> {
_beIdenticalTo(expected)
}

private func _beIdenticalTo<T: AnyObject>(_ expected: T?) -> Matcher<T> {
return Matcher.define { actualExpression in
let actual = try actualExpression.evaluate()

Expand Down Expand Up @@ -36,7 +46,15 @@ public func !== (lhs: AsyncExpectation<AnyObject>, rhs: AnyObject?) async {
///
/// Alias for "beIdenticalTo".
public func be<T: AnyObject>(_ expected: T?) -> Matcher<T> {
return beIdenticalTo(expected)
return _beIdenticalTo(expected)
}

/// A Nimble matcher that succeeds when the actual value is the same instance
/// as the expected instance.
///
/// Alias for "beIdenticalTo".
public func be(_ expected: AnyObject?) -> Matcher<AnyObject> {
return _beIdenticalTo(expected)
}

#if canImport(Darwin)
Expand Down
15 changes: 15 additions & 0 deletions Tests/NimbleTests/Matchers/BeIdenticalToObjectTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ final class BeIdenticalToObjectTest: XCTestCase {

func testBeIdenticalToPositive() {
expect(self.testObjectA).to(beIdenticalTo(testObjectA))
}

func testbeIdenticalToAsSubmatcher() {
// check that the typing works out when used as a submatcher.
expect(self.testObjectA).to(map({ $0 }, be(testObjectA)))
expect(self.testObjectA).to(map({ $0 }, beIdenticalTo(testObjectA)))
}

func testBeIdenticalToAnyObjectProtocol() {
let object = AnObjectImplementation()

expect(object as AnObjectProtocol).to(be(object))
expect(object as AnObjectProtocol).to(beIdenticalTo(object))
}

func testBeIdenticalToNegative() {
expect(self.testObjectA).toNot(beIdenticalTo(testObjectB))
}
Expand Down Expand Up @@ -54,5 +65,9 @@ final class BeIdenticalToObjectTest: XCTestCase {
expect(self.testObjectA) === testObjectA
expect(self.testObjectA) !== testObjectB
}
}

private protocol AnObjectProtocol: AnyObject {}
private final class AnObjectImplementation: AnObjectProtocol {
init() {}
}

0 comments on commit c638ef9

Please sign in to comment.