Skip to content

Implementation for XCTAssertNoThrow #6776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 13, 2017
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
26 changes: 20 additions & 6 deletions stdlib/public/SDK/XCTest/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

@CodaFi CodaFi Jan 13, 2017

Choose a reason for hiding this comment

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

It's not enough to just outdent. If you use Xcode you can set your indentation settings appropriately, highlight these code blocks, and hit ^+I (CNTRL+I) to have it do this for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, was using xcode. Will try that now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

The tests need to be updated as well

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 ---

Expand All @@ -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

Expand Down
146 changes: 103 additions & 43 deletions validation-test/stdlib/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,52 +252,112 @@ XCTestTestSuite.test("XCTAssertThrowsError") {

}

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")
}
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)
}
}

// 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_throws() {
XCTAssertNoThrow(try throwSomething())
}

// 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)
}

// 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")
}
}

// 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") {
Expand Down