diff --git a/Sources/Nimble/Matchers/BeIdenticalTo.swift b/Sources/Nimble/Matchers/BeIdenticalTo.swift index 85e5b50c8..ef8878d9a 100644 --- a/Sources/Nimble/Matchers/BeIdenticalTo.swift +++ b/Sources/Nimble/Matchers/BeIdenticalTo.swift @@ -1,6 +1,16 @@ /// A Nimble matcher that succeeds when the actual value is the same instance /// as the expected instance. public func beIdenticalTo(_ expected: T?) -> Matcher { + _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 { + _beIdenticalTo(expected) +} + +private func _beIdenticalTo(_ expected: T?) -> Matcher { return Matcher.define { actualExpression in let actual = try actualExpression.evaluate() @@ -36,7 +46,15 @@ public func !== (lhs: AsyncExpectation, rhs: AnyObject?) async { /// /// Alias for "beIdenticalTo". public func be(_ expected: T?) -> Matcher { - 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 { + return _beIdenticalTo(expected) } #if canImport(Darwin) diff --git a/Tests/NimbleTests/Matchers/BeIdenticalToObjectTest.swift b/Tests/NimbleTests/Matchers/BeIdenticalToObjectTest.swift index 0ba2316d0..28f3b0fcd 100644 --- a/Tests/NimbleTests/Matchers/BeIdenticalToObjectTest.swift +++ b/Tests/NimbleTests/Matchers/BeIdenticalToObjectTest.swift @@ -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)) }