Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

beIdenticalTo / ===: disallow comparing non-objects #955

Merged
merged 1 commit into from
Feb 25, 2022
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
16 changes: 8 additions & 8 deletions Sources/Nimble/Matchers/BeIdenticalTo.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// A Nimble matcher that succeeds when the actual value is the same instance
/// as the expected instance.
public func beIdenticalTo(_ expected: Any?) -> Predicate<Any> {
public func beIdenticalTo(_ expected: AnyObject?) -> Predicate<AnyObject> {
return Predicate.define { actualExpression in
let actual = try actualExpression.evaluate() as AnyObject?
let actual = try actualExpression.evaluate()

let bool = actual === (expected as AnyObject?) && actual !== nil
let bool = actual === expected && actual !== nil
return PredicateResult(
bool: bool,
message: .expectedCustomValueTo(
Expand All @@ -15,12 +15,12 @@ public func beIdenticalTo(_ expected: Any?) -> Predicate<Any> {
}
}

extension Expectation where T == Any {
public static func === (lhs: Expectation, rhs: Any?) {
extension Expectation where T == AnyObject {
public static func === (lhs: Expectation, rhs: AnyObject?) {
lhs.to(beIdenticalTo(rhs))
}

public static func !== (lhs: Expectation, rhs: Any?) {
public static func !== (lhs: Expectation, rhs: AnyObject?) {
lhs.toNot(beIdenticalTo(rhs))
}
}
Expand All @@ -29,7 +29,7 @@ extension Expectation where T == Any {
/// as the expected instance.
///
/// Alias for "beIdenticalTo".
public func be(_ expected: Any?) -> Predicate<Any> {
public func be(_ expected: AnyObject?) -> Predicate<AnyObject> {
return beIdenticalTo(expected)
}

Expand All @@ -39,7 +39,7 @@ import class Foundation.NSObject
extension NMBPredicate {
@objc public class func beIdenticalToMatcher(_ expected: NSObject?) -> NMBPredicate {
return NMBPredicate { actualExpression in
let aExpr = actualExpression.cast { $0 as Any? }
let aExpr = actualExpression.cast { $0 as AnyObject? }
return try beIdenticalTo(expected).satisfies(aExpr).toObjectiveC()
}
}
Expand Down
1 change: 0 additions & 1 deletion Tests/NimbleTests/Matchers/BeIdenticalToTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ final class BeIdenticalToTest: XCTestCase {
let value = NSDate()
expect(value).to(be(value))
expect(1 as NSNumber).toNot(be("turtles" as NSString))
expect([1]).toNot(be([1]))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is illegal now. The NSArray equivalent is in the next line.

expect([1 as NSNumber] as NSArray).toNot(be([1 as NSNumber] as NSArray))

let value1 = NSArray()
Expand Down