XCTestAssertNoThrow for 3.1 (#185)
* Added implementation of XCTAssetNoThrow to XCTAssert.swift and tests
diff --git a/Sources/XCTest/Public/XCTAssert.swift b/Sources/XCTest/Public/XCTAssert.swift
index 11aed2c..5c982f1 100644
--- a/Sources/XCTest/Public/XCTAssert.swift
+++ b/Sources/XCTest/Public/XCTAssert.swift
@@ -25,6 +25,7 @@
case `false`
case fail
case throwsError
+ case noThrow
var name: String? {
switch(self) {
@@ -41,6 +42,7 @@
case .`true`: return "XCTAssertTrue"
case .`false`: return "XCTAssertFalse"
case .throwsError: return "XCTAssertThrowsError"
+ case .noThrow: return "XCTAssertNoThrow"
case .fail: return nil
}
}
@@ -421,3 +423,14 @@
}
}
}
+
+public func XCTAssertNoThrow<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
+ _XCTEvaluateAssertion(.noThrow, message: message, file: file, line: line) {
+ do {
+ _ = try expression()
+ return .success
+ } catch let error {
+ return .expectedFailure("threw error \"\(error)\"")
+ }
+ }
+}
diff --git a/Tests/Functional/ErrorHandling/main.swift b/Tests/Functional/ErrorHandling/main.swift
index 208e949..45a9049 100644
--- a/Tests/Functional/ErrorHandling/main.swift
+++ b/Tests/Functional/ErrorHandling/main.swift
@@ -26,6 +26,10 @@
// Tests for throwing assertion expressions
("test_assertionExpressionCanThrow", test_assertionExpressionCanThrow),
+
+ // Tests for XCTAssertNoThrow
+ ("test_shouldNotThrowErrorDefiningSuccess", test_shouldNotThrowErrorDefiningSuccess),
+ ("test_shouldThrowErrorDefiningFailure", test_shouldThrowErrorDefiningFailure),
]
}()
@@ -103,13 +107,28 @@
func test_assertionExpressionCanThrow() {
XCTAssertEqual(try functionThatShouldReturnButThrows(), 1)
}
+
+
+// CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorDefiningSuccess' started at \d+:\d+:\d+\.\d+
+// CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorDefiningSuccess' passed \(\d+\.\d+ seconds\)
+ func test_shouldNotThrowErrorDefiningSuccess() {
+ XCTAssertNoThrow(try functionThatDoesNotThrowError())
+ }
+
+// CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorDefiningFailure' started at \d+:\d+:\d+\.\d+
+// CHECK: .*/ErrorHandling/main.swift:[[@LINE+3]]: error: ErrorHandling.test_shouldThrowErrorDefiningFailure : XCTAssertNoThrow failed: threw error "anError\("an error message"\)" -
+// CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorDefiningFailure' failed \(\d+\.\d+ seconds\)
+ func test_shouldThrowErrorDefiningFailure() {
+ XCTAssertNoThrow(try functionThatDoesThrowError())
+ }
}
+
// CHECK: Test Suite 'ErrorHandling' failed at \d+:\d+:\d+\.\d+
-// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
+// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
XCTMain([testCase(ErrorHandling.allTests)])
// CHECK: Test Suite '.*\.xctest' failed at \d+:\d+:\d+\.\d+
-// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
+// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'All tests' failed at \d+:\d+:\d+\.\d+
-// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
+// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds