Skip to content

Commit cef4e66

Browse files
authored
Merge pull request swiftlang#6776 from ArtSabintsev/feature/XCTAssertNoThrow
Implementation for XCTAssertNoThrow
2 parents 1a22a8d + 5008ed8 commit cef4e66

File tree

2 files changed

+123
-49
lines changed

2 files changed

+123
-49
lines changed

stdlib/public/SDK/XCTest/XCTest.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,26 @@ public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _
10301030
}
10311031
}
10321032

1033+
public func XCTAssertNoThrow<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
1034+
let assertionType = _XCTAssertionType.noThrow
1035+
1036+
let result = _XCTRunThrowableBlock { _ = try expression() }
1037+
1038+
switch result {
1039+
case .success:
1040+
return
1041+
1042+
case .failedWithError(let error):
1043+
_XCTRegisterFailure(true, "XCTAssertNoThrow failed: threw error \"\(error)\"", message, file, line)
1044+
1045+
case .failedWithException(_, _, let reason):
1046+
_XCTRegisterFailure(true, _XCTFailureDescription(assertionType, 1, reason as NSString), message, file, line)
1047+
1048+
case .failedWithUnknownException:
1049+
_XCTRegisterFailure(true, _XCTFailureDescription(assertionType, 2), message, file, line)
1050+
}
1051+
}
1052+
10331053
#if XCTEST_ENABLE_EXCEPTION_ASSERTIONS
10341054
// --- Currently-Unsupported Assertions ---
10351055

@@ -1051,12 +1071,6 @@ public func XCTAssertThrowsSpecificNamed(_ expression: @autoclosure () -> Any?,
10511071
// FIXME: Unsupported
10521072
}
10531073

1054-
public func XCTAssertNoThrow(_ expression: @autoclosure () -> Any?, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
1055-
let assertionType = _XCTAssertionType.assertion_NoThrow
1056-
1057-
// FIXME: Unsupported
1058-
}
1059-
10601074
public func XCTAssertNoThrowSpecific(_ expression: @autoclosure () -> Any?, _ exception: Any, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
10611075
let assertionType = _XCTAssertionType.assertion_NoThrowSpecific
10621076

validation-test/stdlib/XCTest.swift

Lines changed: 103 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -252,52 +252,112 @@ XCTestTestSuite.test("XCTAssertThrowsError") {
252252

253253
}
254254

255-
XCTestTestSuite.test("XCTAsserts with throwing expressions") {
256-
class ErrorTestCase: XCTestCase {
257-
var doThrow = true
258-
var errorCode = 42
259-
260-
dynamic func throwSomething() throws -> String {
261-
if doThrow {
262-
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
263-
}
264-
return "Hello"
265-
}
266-
267-
dynamic func test_withThrowing() {
268-
XCTAssertEqual(try throwSomething(), "Hello")
269-
}
255+
XCTestTestSuite.test("XCTAssertNoThrow") {
256+
class ErrorTestCase: XCTestCase {
257+
var doThrow = true
258+
var errorCode = 42
259+
260+
dynamic func throwSomething() throws {
261+
if doThrow {
262+
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
263+
}
270264
}
271-
272-
// Try success case
273-
do {
274-
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
275-
testCase.doThrow = false
276-
execute(testCase.run)
277-
let testRun = testCase.testRun!
278-
279-
expectEqual(1, testRun.testCaseCount)
280-
expectEqual(1, testRun.executionCount)
281-
expectEqual(0, testRun.failureCount)
282-
expectEqual(0, testRun.unexpectedExceptionCount)
283-
expectEqual(0, testRun.totalFailureCount)
284-
expectTrue(testRun.hasSucceeded)
265+
266+
dynamic func test_throws() {
267+
XCTAssertNoThrow(try throwSomething())
285268
}
286-
287-
// Now try when the expression throws
288-
do {
289-
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
290-
execute(testCase.run)
291-
let testRun = testCase.testRun!
292-
293-
expectEqual(1, testRun.testCaseCount)
294-
expectEqual(1, testRun.executionCount)
295-
expectEqual(0, testRun.failureCount)
296-
expectEqual(1, testRun.unexpectedExceptionCount)
297-
expectEqual(1, testRun.totalFailureCount)
298-
expectFalse(testRun.hasSucceeded)
269+
}
270+
271+
// Success
272+
do {
273+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
274+
testCase.doThrow = false
275+
execute(testCase.run)
276+
let testRun = testCase.testRun!
277+
278+
expectEqual(1, testRun.testCaseCount)
279+
expectEqual(1, testRun.executionCount)
280+
expectEqual(0, testRun.failureCount)
281+
expectEqual(0, testRun.unexpectedExceptionCount)
282+
expectEqual(0, testRun.totalFailureCount)
283+
expectTrue(testRun.hasSucceeded)
284+
}
285+
286+
// Failure
287+
do {
288+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
289+
execute(testCase.run)
290+
let testRun = testCase.testRun!
291+
292+
expectEqual(1, testRun.testCaseCount)
293+
expectEqual(1, testRun.executionCount)
294+
expectEqual(1, testRun.failureCount)
295+
expectEqual(0, testRun.unexpectedExceptionCount)
296+
expectEqual(1, testRun.totalFailureCount)
297+
expectFalse(testRun.hasSucceeded)
298+
}
299+
300+
// Throws wrong thing
301+
do {
302+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
303+
testCase.errorCode = 23
304+
execute(testCase.run)
305+
let testRun = testCase.testRun!
306+
307+
expectEqual(1, testRun.testCaseCount)
308+
expectEqual(1, testRun.executionCount)
309+
expectEqual(1, testRun.failureCount)
310+
expectEqual(0, testRun.unexpectedExceptionCount)
311+
expectEqual(1, testRun.totalFailureCount)
312+
expectFalse(testRun.hasSucceeded)
313+
}
314+
}
315+
316+
XCTestTestSuite.test("XCTAsserts with throwing expressions") {
317+
class ErrorTestCase: XCTestCase {
318+
var doThrow = true
319+
var errorCode = 42
320+
321+
dynamic func throwSomething() throws -> String {
322+
if doThrow {
323+
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
324+
}
325+
return "Hello"
299326
}
300-
327+
328+
dynamic func test_withThrowing() {
329+
XCTAssertEqual(try throwSomething(), "Hello")
330+
}
331+
}
332+
333+
// Try success case
334+
do {
335+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
336+
testCase.doThrow = false
337+
execute(testCase.run)
338+
let testRun = testCase.testRun!
339+
340+
expectEqual(1, testRun.testCaseCount)
341+
expectEqual(1, testRun.executionCount)
342+
expectEqual(0, testRun.failureCount)
343+
expectEqual(0, testRun.unexpectedExceptionCount)
344+
expectEqual(0, testRun.totalFailureCount)
345+
expectTrue(testRun.hasSucceeded)
346+
}
347+
348+
// Now try when the expression throws
349+
do {
350+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
351+
execute(testCase.run)
352+
let testRun = testCase.testRun!
353+
354+
expectEqual(1, testRun.testCaseCount)
355+
expectEqual(1, testRun.executionCount)
356+
expectEqual(0, testRun.failureCount)
357+
expectEqual(1, testRun.unexpectedExceptionCount)
358+
expectEqual(1, testRun.totalFailureCount)
359+
expectFalse(testRun.hasSucceeded)
360+
}
301361
}
302362

303363
XCTestTestSuite.test("Test methods that wind up throwing") {

0 commit comments

Comments
 (0)