Skip to content

Commit

Permalink
feat: add isIdenticalTo() and isNotIdenticalTo() (#112)
Browse files Browse the repository at this point in the history
- The `isIdenticalTo()` performs a `===` on subjects that are
`AnyObject`.
- The `isNotIdenticalTo()` performs a `!==` on subjects that are
`AnyObject`.

Closes #109.
  • Loading branch information
cgrindel authored Sep 16, 2023
1 parent 1c20130 commit 9ca2a36
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/Truth/AnyObectAssertions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public extension Subject where T: AnyObject {
/// Asserts that the actual value is identical (===) to expected.
@discardableResult func isIdenticalTo(
_ expected: T,
file: StaticString = #file, line: UInt = #line
) -> Self {
return doAssert(
failWith: Failure(
file: file,
line: line,
Fact("expected", expected),
Fact("to be identical to", actual)
)
) {
actual === expected
}
}

/// Asserts that the actual value is not identical (!==) to expected.
@discardableResult func isNotIdenticalTo(
_ expected: T,
file: StaticString = #file, line: UInt = #line
) -> Self {
return doAssert(
failWith: Failure(
file: file,
line: line,
Fact("expected not to be identical to", expected)
)
) {
actual !== expected
}
}
}
1 change: 1 addition & 0 deletions Sources/Truth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ swift_library(
name = "Truth",
testonly = True, # keep
srcs = [
"AnyObectAssertions.swift",
"AssertThat.swift",
"BidirectionalCollectionAssertions.swift",
"BinaryIntegerAssertions.swift",
Expand Down
40 changes: 40 additions & 0 deletions Tests/TruthTests/AnyObectAssertionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@testable import Truth
import XCTest

class AnyObectAssertionsTests: XCTestCase {
class Foo {
var name: String

init(name: String) {
self.name = name
}
}

func test_isIdenticalTo_WithIdenticalObjs_Succeeds() throws {
let actual = Foo(name: "Jim")
let expected = actual
assertNoFailures { $0.that(actual).isIdenticalTo(expected) }
}

func test_isIdenticalTo_WithDifferentObjs_Fails() throws {
let actual = Foo(name: "Jim")
let expected = Foo(name: "Jim")
assertFailure([Fact("expected", expected), Fact("to be identical to", actual)]) {
$0.that(actual).isIdenticalTo(expected)
}
}

func test_isNotIdenticalTo_WithDifferentObjs_Succeeds() throws {
let actual = Foo(name: "Jim")
let expected = Foo(name: "Jim")
assertNoFailures { $0.that(actual).isNotIdenticalTo(expected) }
}

func test_isNotIdenticalTo_WithIdenticalObjs_Fails() throws {
let actual = Foo(name: "Jim")
let expected = actual
assertFailure([Fact("expected not to be identical to", expected)]) {
$0.that(actual).isNotIdenticalTo(expected)
}
}
}
1 change: 1 addition & 0 deletions Tests/TruthTests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ bzlformat_pkg(name = "bzlformat")
swift_test(
name = "TruthTests",
srcs = [
"AnyObectAssertionsTests.swift",
"AssertThatTests.swift",
"AssertionConsumerError.swift",
"BidirectionalCollectionAssertionsTests.swift",
Expand Down

0 comments on commit 9ca2a36

Please sign in to comment.