diff --git a/validation-test/stdlib/XCTest.swift b/validation-test/stdlib/XCTest.swift index 043b984a5bf5e..6a523696e6323 100644 --- a/validation-test/stdlib/XCTest.swift +++ b/validation-test/stdlib/XCTest.swift @@ -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 @@ -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! @@ -61,7 +59,7 @@ XCTestTestSuite.test("XCTAssertEqual/Array") { } } - 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) @@ -71,7 +69,7 @@ XCTestTestSuite.test("XCTAssertEqual/Array") { 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) @@ -95,7 +93,7 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary") { } } - 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) @@ -105,7 +103,7 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary") { 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) @@ -116,5 +114,171 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary") { 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: 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()