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
32 changes: 32 additions & 0 deletions Sources/XCTest/Public/XCTAssert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
private enum _XCTAssertion {
case equal
case equalWithAccuracy
case identical
case notIdentical
case greaterThan
case greaterThanOrEqual
case lessThan
Expand All @@ -32,6 +34,8 @@ private enum _XCTAssertion {
switch(self) {
case .equal: return "XCTAssertEqual"
case .equalWithAccuracy: return "XCTAssertEqual"
case .identical: return "XCTAssertIdentical"
case .notIdentical: return "XCTAssertNotIdentical"
case .greaterThan: return "XCTAssertGreaterThan"
case .greaterThanOrEqual: return "XCTAssertGreaterThanOrEqual"
case .lessThan: return "XCTAssertLessThan"
Expand Down Expand Up @@ -206,6 +210,34 @@ public func XCTAssertEqualWithAccuracy<T: FloatingPoint>(_ expression1: @autoclo
XCTAssertEqual(try expression1(), try expression2(), accuracy: accuracy, message(), file: file, line: line)
}

private func describe(_ object: AnyObject?) -> String {
return object == nil ? String(describing: object) : String(describing: object!)
}

/// Asserts that two values are identical.
public func XCTAssertIdentical(_ expression1: @autoclosure () throws -> AnyObject?, _ expression2: @autoclosure () throws -> AnyObject?, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
_XCTEvaluateAssertion(.identical, message: message(), file: file, line: line) {
let (value1, value2) = (try expression1(), try expression2())
if value1 === value2 {
return .success
} else {
return .expectedFailure("(\"\(describe(value1))\") is not identical to (\"\(describe(value2))\")")
}
}
}

/// Asserts that two values aren't identical.
public func XCTAssertNotIdentical(_ expression1: @autoclosure () throws -> AnyObject?, _ expression2: @autoclosure () throws -> AnyObject?, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
_XCTEvaluateAssertion(.notIdentical, message: message(), file: file, line: line) {
let (value1, value2) = (try expression1(), try expression2())
if value1 !== value2 {
return .success
} else {
return .expectedFailure("(\"\(describe(value1))\") is identical to (\"\(describe(value2))\")")
}
}
}

public func XCTAssertFalse(_ expression: @autoclosure () throws -> Bool, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
_XCTEvaluateAssertion(.`false`, message: message(), file: file, line: line) {
let value = try expression()
Expand Down
29 changes: 26 additions & 3 deletions Tests/Functional/FailureMessagesTestCase/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class FailureMessagesTestCase: XCTestCase {
("testAssertFalse", testAssertFalse),
("testAssertGreaterThan", testAssertGreaterThan),
("testAssertGreaterThanOrEqual", testAssertGreaterThanOrEqual),
("testAssertIdentical", testAssertIdentical),
("testAssertNotIdentical", testAssertNotIdentical),
("testAssertLessThan", testAssertLessThan),
("testAssertLessThanOrEqual", testAssertLessThanOrEqual),
("testAssertNil", testAssertNil),
Expand Down Expand Up @@ -121,6 +123,27 @@ class FailureMessagesTestCase: XCTestCase {
XCTAssertGreaterThanOrEqual(-1, 0, "message", file: "test.swift")
}

// CHECK: Test Case 'FailureMessagesTestCase.testAssertIdentical' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: test.swift:[[@LINE+7]]: error: FailureMessagesTestCase.testAssertIdentical : XCTAssertIdentical failed: \("a"\) is not identical to \("b"\) - message
// CHECK: Test Case 'FailureMessagesTestCase.testAssertIdentical' failed \(\d+\.\d+ seconds\)
func testAssertIdentical() {
let object = XCTestExpectation()
XCTAssertIdentical(object, object)
XCTAssertIdentical(nil, nil)
XCTAssertIdentical(true as AnyObject, true as AnyObject)
XCTAssertIdentical("a" as AnyObject, "b" as AnyObject, "message", file: "test.swift")
}

// CHECK: Test Case 'FailureMessagesTestCase.testAssertNotIdentical' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: test.swift:[[@LINE+6]]: error: FailureMessagesTestCase.testAssertNotIdentical : XCTAssertNotIdentical failed: \("nil"\) is identical to \("nil"\) - message
// CHECK: Test Case 'FailureMessagesTestCase.testAssertNotIdentical' failed \(\d+\.\d+ seconds\)
func testAssertNotIdentical() {
XCTAssertNotIdentical("a" as AnyObject, "b" as AnyObject)
XCTAssertNotIdentical(true as AnyObject, false as AnyObject)
XCTAssertNotIdentical(XCTestExpectation(), XCTestExpectation())
XCTAssertNotIdentical(nil, nil, "message", file: "test.swift")
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for this change! I think we can land it soon

My only request is that you add a few more assertions to these two tests, in particular success cases which validate that both XCTAssertIdentical and XCTAssertNotIdentical succeed when passed identical or not identical values, respectively. Currently, the tests only validate failure scenarios, but we should be sure to have tests covering when they pass as well. Personally, I'm fine just adding those inside the existing two test method bodies, to keep things simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh that's a good point. I added a few succeeding assertions to the test.

}

// CHECK: Test Case 'FailureMessagesTestCase.testAssertLessThan' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: test.swift:[[@LINE+3]]: error: FailureMessagesTestCase.testAssertLessThan : XCTAssertLessThan failed: \("0"\) is not less than \("0"\) - message
// CHECK: Test Case 'FailureMessagesTestCase.testAssertLessThan' failed \(\d+\.\d+ seconds\)
Expand Down Expand Up @@ -213,11 +236,11 @@ class FailureMessagesTestCase: XCTestCase {
}
}
// CHECK: Test Suite 'FailureMessagesTestCase' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 24 tests, with 24 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 26 tests, with 26 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

XCTMain([testCase(FailureMessagesTestCase.allTests)])

// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 24 tests, with 24 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 26 tests, with 26 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 24 tests, with 24 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 26 tests, with 26 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds