Skip to content

Commit 50cc074

Browse files
ArtSabintsevbriancroom
authored andcommitted
XCTestAssertNoThrow (#184)
* Added implementation of XCTAssetNoThrow to XCTAssert.swift and tests
1 parent 1f20d9b commit 50cc074

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

Sources/XCTest/Public/XCTAssert.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ private enum _XCTAssertion {
2525
case `false`
2626
case fail
2727
case throwsError
28+
case noThrow
2829

2930
var name: String? {
3031
switch(self) {
@@ -41,6 +42,7 @@ private enum _XCTAssertion {
4142
case .`true`: return "XCTAssertTrue"
4243
case .`false`: return "XCTAssertFalse"
4344
case .throwsError: return "XCTAssertThrowsError"
45+
case .noThrow: return "XCTAssertNoThrow"
4446
case .fail: return nil
4547
}
4648
}
@@ -421,3 +423,14 @@ public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _
421423
}
422424
}
423425
}
426+
427+
public func XCTAssertNoThrow<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
428+
_XCTEvaluateAssertion(.noThrow, message: message, file: file, line: line) {
429+
do {
430+
_ = try expression()
431+
return .success
432+
} catch let error {
433+
return .expectedFailure("threw error \"\(error)\"")
434+
}
435+
}
436+
}

Tests/Functional/ErrorHandling/main.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class ErrorHandling: XCTestCase {
2626

2727
// Tests for throwing assertion expressions
2828
("test_assertionExpressionCanThrow", test_assertionExpressionCanThrow),
29+
30+
// Tests for XCTAssertNoThrow
31+
("test_shouldNotThrowErrorDefiningSuccess", test_shouldNotThrowErrorDefiningSuccess),
32+
("test_shouldThrowErrorDefiningFailure", test_shouldThrowErrorDefiningFailure),
2933
]
3034
}()
3135

@@ -103,13 +107,28 @@ class ErrorHandling: XCTestCase {
103107
func test_assertionExpressionCanThrow() {
104108
XCTAssertEqual(try functionThatShouldReturnButThrows(), 1)
105109
}
110+
111+
112+
// CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorDefiningSuccess' started at \d+:\d+:\d+\.\d+
113+
// CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorDefiningSuccess' passed \(\d+\.\d+ seconds\)
114+
func test_shouldNotThrowErrorDefiningSuccess() {
115+
XCTAssertNoThrow(try functionThatDoesNotThrowError())
116+
}
117+
118+
// CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorDefiningFailure' started at \d+:\d+:\d+\.\d+
119+
// CHECK: .*/ErrorHandling/main.swift:[[@LINE+3]]: error: ErrorHandling.test_shouldThrowErrorDefiningFailure : XCTAssertNoThrow failed: threw error "anError\("an error message"\)" -
120+
// CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorDefiningFailure' failed \(\d+\.\d+ seconds\)
121+
func test_shouldThrowErrorDefiningFailure() {
122+
XCTAssertNoThrow(try functionThatDoesThrowError())
123+
}
106124
}
125+
107126
// CHECK: Test Suite 'ErrorHandling' failed at \d+:\d+:\d+\.\d+
108-
// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
127+
// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
109128

110129
XCTMain([testCase(ErrorHandling.allTests)])
111130

112131
// CHECK: Test Suite '.*\.xctest' failed at \d+:\d+:\d+\.\d+
113-
// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
132+
// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
114133
// CHECK: Test Suite 'All tests' failed at \d+:\d+:\d+\.\d+
115-
// CHECK: \t Executed 6 tests, with 4 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
134+
// CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

0 commit comments

Comments
 (0)