3
3
4
4
// REQUIRES: objc_interop
5
5
6
- // Currently it fails because a dylib cannot be found.
7
- // TODO: Re-enable this test when rdar://problem/24222804 is fixed
8
6
// REQUIRES: OS=macosx
9
7
10
- // watchOS 2.0 does not have a public XCTest module.
8
+ // watchOS 2.0 does not have an XCTest module.
11
9
// XFAIL: OS=watchos
12
10
13
11
import StdlibUnittest
@@ -36,7 +34,7 @@ XCTestTestSuite.test("exceptions") {
36
34
}
37
35
}
38
36
39
- let testCase = ExceptionTestCase ( selector: " test_raises " )
37
+ let testCase = ExceptionTestCase ( selector: #selector ( ExceptionTestCase . test_raises) )
40
38
testCase. runTest ( )
41
39
let testRun = testCase. testRun!
42
40
@@ -61,7 +59,7 @@ XCTestTestSuite.test("XCTAssertEqual/Array<T>") {
61
59
}
62
60
}
63
61
64
- let passingTestCase = AssertEqualArrayTestCase ( selector: " test_whenArraysAreEqual_passes " )
62
+ let passingTestCase = AssertEqualArrayTestCase ( selector: #selector ( AssertEqualArrayTestCase . test_whenArraysAreEqual_passes) )
65
63
passingTestCase. runTest ( )
66
64
let passingTestRun = passingTestCase. testRun!
67
65
expectEqual ( 1 , passingTestRun. testCaseCount)
@@ -71,7 +69,7 @@ XCTestTestSuite.test("XCTAssertEqual/Array<T>") {
71
69
expectEqual ( 0 , passingTestRun. totalFailureCount)
72
70
expectTrue ( passingTestRun. hasSucceeded)
73
71
74
- let failingTestCase = AssertEqualArrayTestCase ( selector: " test_whenArraysAreNotEqual_fails " )
72
+ let failingTestCase = AssertEqualArrayTestCase ( selector: #selector ( AssertEqualArrayTestCase . test_whenArraysAreNotEqual_fails) )
75
73
failingTestCase. runTest ( )
76
74
let failingTestRun = failingTestCase. testRun!
77
75
expectEqual ( 1 , failingTestRun. testCaseCount)
@@ -95,7 +93,7 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
95
93
}
96
94
}
97
95
98
- let passingTestCase = AssertEqualDictionaryTestCase ( selector: " test_whenDictionariesAreEqual_passes " )
96
+ let passingTestCase = AssertEqualDictionaryTestCase ( selector: #selector ( AssertEqualDictionaryTestCase . test_whenDictionariesAreEqual_passes) )
99
97
passingTestCase. runTest ( )
100
98
let passingTestRun = passingTestCase. testRun!
101
99
expectEqual ( 1 , passingTestRun. testCaseCount)
@@ -105,7 +103,7 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
105
103
expectEqual ( 0 , passingTestRun. totalFailureCount)
106
104
expectTrue ( passingTestRun. hasSucceeded)
107
105
108
- let failingTestCase = AssertEqualDictionaryTestCase ( selector: " test_whenDictionariesAreNotEqual_fails " )
106
+ let failingTestCase = AssertEqualDictionaryTestCase ( selector: #selector ( AssertEqualDictionaryTestCase . test_whenDictionariesAreNotEqual_fails) )
109
107
failingTestCase. runTest ( )
110
108
let failingTestRun = failingTestCase. testRun!
111
109
expectEqual ( 1 , failingTestRun. testCaseCount)
@@ -116,5 +114,171 @@ XCTestTestSuite.test("XCTAssertEqual/Dictionary<T, U>") {
116
114
expectFalse ( failingTestRun. hasSucceeded)
117
115
}
118
116
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
+
119
283
runAllTests ( )
120
284
0 commit comments