Merge pull request #6776 from ArtSabintsev/feature/XCTAssertNoThrow

Implementation for XCTAssertNoThrow
This commit is contained in:
Robert Widmann
2017-01-13 01:39:26 -07:00
committed by GitHub
2 changed files with 123 additions and 49 deletions

View File

@@ -1030,6 +1030,26 @@ public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _
}
}
public func XCTAssertNoThrow<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
let assertionType = _XCTAssertionType.noThrow
let result = _XCTRunThrowableBlock { _ = try expression() }
switch result {
case .success:
return
case .failedWithError(let error):
_XCTRegisterFailure(true, "XCTAssertNoThrow failed: threw error \"\(error)\"", message, file, line)
case .failedWithException(_, _, let reason):
_XCTRegisterFailure(true, _XCTFailureDescription(assertionType, 1, reason as NSString), message, file, line)
case .failedWithUnknownException:
_XCTRegisterFailure(true, _XCTFailureDescription(assertionType, 2), message, file, line)
}
}
#if XCTEST_ENABLE_EXCEPTION_ASSERTIONS
// --- Currently-Unsupported Assertions ---
@@ -1051,12 +1071,6 @@ public func XCTAssertThrowsSpecificNamed(_ expression: @autoclosure () -> Any?,
// FIXME: Unsupported
}
public func XCTAssertNoThrow(_ expression: @autoclosure () -> Any?, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
let assertionType = _XCTAssertionType.assertion_NoThrow
// FIXME: Unsupported
}
public func XCTAssertNoThrowSpecific(_ expression: @autoclosure () -> Any?, _ exception: Any, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
let assertionType = _XCTAssertionType.assertion_NoThrowSpecific

View File

@@ -252,52 +252,112 @@ XCTestTestSuite.test("XCTAssertThrowsError") {
}
XCTestTestSuite.test("XCTAssertNoThrow") {
class ErrorTestCase: XCTestCase {
var doThrow = true
var errorCode = 42
dynamic func throwSomething() throws {
if doThrow {
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
}
}
dynamic func test_throws() {
XCTAssertNoThrow(try throwSomething())
}
}
// Success
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
testCase.doThrow = false
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(0, testRun.totalFailureCount)
expectTrue(testRun.hasSucceeded)
}
// Failure
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(1, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
// Throws wrong thing
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
testCase.errorCode = 23
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(1, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
}
XCTestTestSuite.test("XCTAsserts with throwing expressions") {
class ErrorTestCase: XCTestCase {
var doThrow = true
var errorCode = 42
dynamic func throwSomething() throws -> String {
if doThrow {
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
}
return "Hello"
}
dynamic func test_withThrowing() {
XCTAssertEqual(try throwSomething(), "Hello")
}
class ErrorTestCase: XCTestCase {
var doThrow = true
var errorCode = 42
dynamic func throwSomething() throws -> String {
if doThrow {
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
}
return "Hello"
}
// Try success case
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
testCase.doThrow = false
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(0, testRun.totalFailureCount)
expectTrue(testRun.hasSucceeded)
dynamic func test_withThrowing() {
XCTAssertEqual(try throwSomething(), "Hello")
}
// Now try when the expression throws
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(1, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
}
// Try success case
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
testCase.doThrow = false
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(0, testRun.unexpectedExceptionCount)
expectEqual(0, testRun.totalFailureCount)
expectTrue(testRun.hasSucceeded)
}
// Now try when the expression throws
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
execute(testCase.run)
let testRun = testCase.testRun!
expectEqual(1, testRun.testCaseCount)
expectEqual(1, testRun.executionCount)
expectEqual(0, testRun.failureCount)
expectEqual(1, testRun.unexpectedExceptionCount)
expectEqual(1, testRun.totalFailureCount)
expectFalse(testRun.hasSucceeded)
}
}
XCTestTestSuite.test("Test methods that wind up throwing") {