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
  • Loading branch information
younata committed Dec 13, 2024
1 parent 6de3456 commit 6639400
Show file tree
Hide file tree
Showing 2 changed files with 35 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
16 changes: 16 additions & 0 deletions Tests/NimbleTests/Matchers/BeIdenticalToObjectTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@ 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() {
protocol SomeObject: AnyObject {}
class ConcreteImpl: SomeObject {
init() {}
}

let object = ConcreteImpl()

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

func testBeIdenticalToNegative() {
expect(self.testObjectA).toNot(beIdenticalTo(testObjectB))
}
Expand Down

0 comments on commit 6639400

Please sign in to comment.