Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {}
}
Loading