Skip to content

Commit c237762

Browse files
author
Mike Ferris
committed
Merge pull request #1578 from mike-ferris-apple/master
<rdar://problem/24814424> Add unit tests for the error handling suppo…
2 parents ebccd4f + 7fb274a commit c237762

File tree

1 file changed

+172
-8
lines changed

1 file changed

+172
-8
lines changed

validation-test/stdlib/XCTest.swift

Lines changed: 172 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
// REQUIRES: objc_interop
55

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

10-
// watchOS 2.0 does not have a public XCTest module.
8+
// watchOS 2.0 does not have an XCTest module.
119
// XFAIL: OS=watchos
1210

1311
import StdlibUnittest
@@ -36,7 +34,7 @@ XCTestTestSuite.test("exceptions") {
3634
}
3735
}
3836

39-
let testCase = ExceptionTestCase(selector: "test_raises")
37+
let testCase = ExceptionTestCase(selector: #selector(ExceptionTestCase.test_raises))
4038
testCase.runTest()
4139
let testRun = testCase.testRun!
4240

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

64-
let passingTestCase = AssertEqualArrayTestCase(selector: "test_whenArraysAreEqual_passes")
62+
let passingTestCase = AssertEqualArrayTestCase(selector: #selector(AssertEqualArrayTestCase.test_whenArraysAreEqual_passes))
6563
passingTestCase.runTest()
6664
let passingTestRun = passingTestCase.testRun!
6765
expectEqual(1, passingTestRun.testCaseCount)
@@ -71,7 +69,7 @@ XCTestTestSuite.test("XCTAssertEqual/Array<T>") {
7169
expectEqual(0, passingTestRun.totalFailureCount)
7270
expectTrue(passingTestRun.hasSucceeded)
7371

74-
let failingTestCase = AssertEqualArrayTestCase(selector: "test_whenArraysAreNotEqual_fails")
72+
let failingTestCase = AssertEqualArrayTestCase(selector: #selector(AssertEqualArrayTestCase.test_whenArraysAreNotEqual_fails))
7573
failingTestCase.runTest()
7674
let failingTestRun = failingTestCase.testRun!
7775
expectEqual(1, failingTestRun.testCaseCount)
@@ -95,7 +93,7 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
9593
}
9694
}
9795

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

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

117+
XCTestTestSuite.test("XCTAssertThrowsError") {
118+
class ErrorTestCase: XCTestCase {
119+
var doThrow = true
120+
var errorCode = 42
121+
122+
dynamic func throwSomething() throws {
123+
if doThrow {
124+
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
125+
}
126+
}
127+
128+
dynamic func test_throws() {
129+
XCTAssertThrowsError(try throwSomething()) {
130+
error in
131+
let nserror = error as NSError
132+
XCTAssertEqual(nserror.domain, "MyDomain")
133+
XCTAssertEqual(nserror.code, 42)
134+
}
135+
}
136+
}
137+
138+
// Try success case
139+
do {
140+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
141+
testCase.runTest()
142+
let testRun = testCase.testRun!
143+
144+
expectEqual(1, testRun.testCaseCount)
145+
expectEqual(1, testRun.executionCount)
146+
expectEqual(0, testRun.failureCount)
147+
expectEqual(0, testRun.unexpectedExceptionCount)
148+
expectEqual(0, testRun.totalFailureCount)
149+
expectTrue(testRun.hasSucceeded)
150+
}
151+
152+
// Now try when it does not throw
153+
do {
154+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
155+
testCase.doThrow = false
156+
testCase.runTest()
157+
let testRun = testCase.testRun!
158+
159+
expectEqual(1, testRun.testCaseCount)
160+
expectEqual(1, testRun.executionCount)
161+
expectEqual(1, testRun.failureCount)
162+
expectEqual(0, testRun.unexpectedExceptionCount)
163+
expectEqual(1, testRun.totalFailureCount)
164+
expectFalse(testRun.hasSucceeded)
165+
}
166+
167+
168+
// Now try when it throws the wrong thing
169+
do {
170+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_throws))
171+
testCase.errorCode = 23
172+
testCase.runTest()
173+
let testRun = testCase.testRun!
174+
175+
expectEqual(1, testRun.testCaseCount)
176+
expectEqual(1, testRun.executionCount)
177+
expectEqual(1, testRun.failureCount)
178+
expectEqual(0, testRun.unexpectedExceptionCount)
179+
expectEqual(1, testRun.totalFailureCount)
180+
expectFalse(testRun.hasSucceeded)
181+
}
182+
183+
}
184+
185+
XCTestTestSuite.test("XCTAsserts with throwing expressions") {
186+
class ErrorTestCase: XCTestCase {
187+
var doThrow = true
188+
var errorCode = 42
189+
190+
dynamic func throwSomething() throws -> String {
191+
if doThrow {
192+
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
193+
}
194+
return "Hello"
195+
}
196+
197+
dynamic func test_withThrowing() {
198+
XCTAssertEqual(try throwSomething(), "Hello")
199+
}
200+
}
201+
202+
// Try success case
203+
do {
204+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
205+
testCase.doThrow = false
206+
testCase.runTest()
207+
let testRun = testCase.testRun!
208+
209+
expectEqual(1, testRun.testCaseCount)
210+
expectEqual(1, testRun.executionCount)
211+
expectEqual(0, testRun.failureCount)
212+
expectEqual(0, testRun.unexpectedExceptionCount)
213+
expectEqual(0, testRun.totalFailureCount)
214+
expectTrue(testRun.hasSucceeded)
215+
}
216+
217+
// Now try when the expression throws
218+
do {
219+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
220+
testCase.runTest()
221+
let testRun = testCase.testRun!
222+
223+
expectEqual(1, testRun.testCaseCount)
224+
expectEqual(1, testRun.executionCount)
225+
expectEqual(0, testRun.failureCount)
226+
expectEqual(1, testRun.unexpectedExceptionCount)
227+
expectEqual(1, testRun.totalFailureCount)
228+
expectFalse(testRun.hasSucceeded)
229+
}
230+
231+
}
232+
233+
/* 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
234+
235+
XCTestTestSuite.test("Test methods that wind up throwing") {
236+
class ErrorTestCase: XCTestCase {
237+
var doThrow = true
238+
var errorCode = 42
239+
240+
dynamic func throwSomething() throws {
241+
if doThrow {
242+
throw NSError(domain: "MyDomain", code: errorCode, userInfo: nil)
243+
}
244+
}
245+
246+
dynamic func test_withThrowing() throws {
247+
try throwSomething()
248+
}
249+
}
250+
251+
// Try success case
252+
do {
253+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
254+
testCase.doThrow = false
255+
testCase.runTest()
256+
let testRun = testCase.testRun!
257+
258+
expectEqual(1, testRun.testCaseCount)
259+
expectEqual(1, testRun.executionCount)
260+
expectEqual(0, testRun.failureCount)
261+
expectEqual(0, testRun.unexpectedExceptionCount)
262+
expectEqual(0, testRun.totalFailureCount)
263+
expectTrue(testRun.hasSucceeded)
264+
}
265+
266+
// Now try when the expression throws
267+
do {
268+
let testCase = ErrorTestCase(selector: #selector(ErrorTestCase.test_withThrowing))
269+
testCase.runTest()
270+
let testRun = testCase.testRun!
271+
272+
expectEqual(1, testRun.testCaseCount)
273+
expectEqual(1, testRun.executionCount)
274+
expectEqual(0, testRun.failureCount)
275+
expectEqual(1, testRun.unexpectedExceptionCount)
276+
expectEqual(1, testRun.totalFailureCount)
277+
expectFalse(testRun.hasSucceeded)
278+
}
279+
280+
}
281+
*/
282+
119283
runAllTests()
120284

0 commit comments

Comments
 (0)