Skip to content

<rdar://problem/24814424> Add unit tests for the error handling suppo… #1578

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 1 commit into from
Mar 8, 2016
Merged
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
180 changes: 172 additions & 8 deletions validation-test/stdlib/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

// REQUIRES: objc_interop

// Currently it fails because a dylib cannot be found.
// TODO: Re-enable this test when rdar://problem/24222804 is fixed
// REQUIRES: OS=macosx

// watchOS 2.0 does not have a public XCTest module.
// watchOS 2.0 does not have an XCTest module.
// XFAIL: OS=watchos

import StdlibUnittest
Expand Down Expand Up @@ -36,7 +34,7 @@ XCTestTestSuite.test("exceptions") {
}
}

let testCase = ExceptionTestCase(selector: "test_raises")
let testCase = ExceptionTestCase(selector: #selector(ExceptionTestCase.test_raises))
testCase.runTest()
let testRun = testCase.testRun!

Expand All @@ -61,7 +59,7 @@ XCTestTestSuite.test("XCTAssertEqual/Array<T>") {
}
}

let passingTestCase = AssertEqualArrayTestCase(selector: "test_whenArraysAreEqual_passes")
let passingTestCase = AssertEqualArrayTestCase(selector: #selector(AssertEqualArrayTestCase.test_whenArraysAreEqual_passes))
passingTestCase.runTest()
let passingTestRun = passingTestCase.testRun!
expectEqual(1, passingTestRun.testCaseCount)
Expand All @@ -71,7 +69,7 @@ XCTestTestSuite.test("XCTAssertEqual/Array<T>") {
expectEqual(0, passingTestRun.totalFailureCount)
expectTrue(passingTestRun.hasSucceeded)

let failingTestCase = AssertEqualArrayTestCase(selector: "test_whenArraysAreNotEqual_fails")
let failingTestCase = AssertEqualArrayTestCase(selector: #selector(AssertEqualArrayTestCase.test_whenArraysAreNotEqual_fails))
failingTestCase.runTest()
let failingTestRun = failingTestCase.testRun!
expectEqual(1, failingTestRun.testCaseCount)
Expand All @@ -95,7 +93,7 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
}
}

let passingTestCase = AssertEqualDictionaryTestCase(selector: "test_whenDictionariesAreEqual_passes")
let passingTestCase = AssertEqualDictionaryTestCase(selector: #selector(AssertEqualDictionaryTestCase.test_whenDictionariesAreEqual_passes))
passingTestCase.runTest()
let passingTestRun = passingTestCase.testRun!
expectEqual(1, passingTestRun.testCaseCount)
Expand All @@ -105,7 +103,7 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
expectEqual(0, passingTestRun.totalFailureCount)
expectTrue(passingTestRun.hasSucceeded)

let failingTestCase = AssertEqualDictionaryTestCase(selector: "test_whenDictionariesAreNotEqual_fails")
let failingTestCase = AssertEqualDictionaryTestCase(selector: #selector(AssertEqualDictionaryTestCase.test_whenDictionariesAreNotEqual_fails))
failingTestCase.runTest()
let failingTestRun = failingTestCase.testRun!
expectEqual(1, failingTestRun.testCaseCount)
Expand All @@ -116,5 +114,171 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
expectFalse(failingTestRun.hasSucceeded)
}

XCTestTestSuite.test("XCTAssertThrowsError") {
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() {
XCTAssertThrowsError(try throwSomething()) {
error in
let nserror = error as NSError
XCTAssertEqual(nserror.domain, "MyDomain")
XCTAssertEqual(nserror.code, 42)
}
}
}

// Try success case
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
testCase.runTest()
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 it does not throw
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
testCase.doThrow = false
testCase.runTest()
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)
}


// Now try when it throws the wrong thing
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
testCase.errorCode = 23
testCase.runTest()
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
testCase.runTest()
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))
testCase.runTest()
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)
}

}

/* Disabling these tests for now: <rdar://problem/25034414> Enable unit tests for test methods that throw once the open source CI is on 7.3

XCTestTestSuite.test("Test methods that wind up throwing") {
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_withThrowing() throws {
try throwSomething()
}
}

// Try success case
do {
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
testCase.doThrow = false
testCase.runTest()
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))
testCase.runTest()
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)
}

}
*/

runAllTests()