@@ -252,52 +252,112 @@ XCTestTestSuite.test("XCTAssertThrowsError") {
252
252
253
253
}
254
254
255
- XCTestTestSuite . test ( " XCTAsserts with throwing expressions " ) {
256
- class ErrorTestCase : XCTestCase {
257
- var doThrow = true
258
- var errorCode = 42
259
-
260
- dynamic func throwSomething( ) throws -> String {
261
- if doThrow {
262
- throw NSError ( domain: " MyDomain " , code: errorCode, userInfo: nil )
263
- }
264
- return " Hello "
265
- }
266
-
267
- dynamic func test_withThrowing( ) {
268
- XCTAssertEqual ( try throwSomething ( ) , " Hello " )
269
- }
255
+ XCTestTestSuite . test ( " XCTAssertNoThrow " ) {
256
+ class ErrorTestCase : XCTestCase {
257
+ var doThrow = true
258
+ var errorCode = 42
259
+
260
+ dynamic func throwSomething( ) throws {
261
+ if doThrow {
262
+ throw NSError ( domain: " MyDomain " , code: errorCode, userInfo: nil )
263
+ }
270
264
}
271
-
272
- // Try success case
273
- do {
274
- let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_withThrowing) )
275
- testCase. doThrow = false
276
- execute ( testCase. run)
277
- let testRun = testCase. testRun!
278
-
279
- expectEqual ( 1 , testRun. testCaseCount)
280
- expectEqual ( 1 , testRun. executionCount)
281
- expectEqual ( 0 , testRun. failureCount)
282
- expectEqual ( 0 , testRun. unexpectedExceptionCount)
283
- expectEqual ( 0 , testRun. totalFailureCount)
284
- expectTrue ( testRun. hasSucceeded)
265
+
266
+ dynamic func test_throws( ) {
267
+ XCTAssertNoThrow ( try throwSomething ( ) )
285
268
}
286
-
287
- // Now try when the expression throws
288
- do {
289
- let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_withThrowing) )
290
- execute ( testCase. run)
291
- let testRun = testCase. testRun!
292
-
293
- expectEqual ( 1 , testRun. testCaseCount)
294
- expectEqual ( 1 , testRun. executionCount)
295
- expectEqual ( 0 , testRun. failureCount)
296
- expectEqual ( 1 , testRun. unexpectedExceptionCount)
297
- expectEqual ( 1 , testRun. totalFailureCount)
298
- expectFalse ( testRun. hasSucceeded)
269
+ }
270
+
271
+ // Success
272
+ do {
273
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_throws) )
274
+ testCase. doThrow = false
275
+ execute ( testCase. run)
276
+ let testRun = testCase. testRun!
277
+
278
+ expectEqual ( 1 , testRun. testCaseCount)
279
+ expectEqual ( 1 , testRun. executionCount)
280
+ expectEqual ( 0 , testRun. failureCount)
281
+ expectEqual ( 0 , testRun. unexpectedExceptionCount)
282
+ expectEqual ( 0 , testRun. totalFailureCount)
283
+ expectTrue ( testRun. hasSucceeded)
284
+ }
285
+
286
+ // Failure
287
+ do {
288
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_throws) )
289
+ execute ( testCase. run)
290
+ let testRun = testCase. testRun!
291
+
292
+ expectEqual ( 1 , testRun. testCaseCount)
293
+ expectEqual ( 1 , testRun. executionCount)
294
+ expectEqual ( 1 , testRun. failureCount)
295
+ expectEqual ( 0 , testRun. unexpectedExceptionCount)
296
+ expectEqual ( 1 , testRun. totalFailureCount)
297
+ expectFalse ( testRun. hasSucceeded)
298
+ }
299
+
300
+ // Throws wrong thing
301
+ do {
302
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_throws) )
303
+ testCase. errorCode = 23
304
+ execute ( testCase. run)
305
+ let testRun = testCase. testRun!
306
+
307
+ expectEqual ( 1 , testRun. testCaseCount)
308
+ expectEqual ( 1 , testRun. executionCount)
309
+ expectEqual ( 1 , testRun. failureCount)
310
+ expectEqual ( 0 , testRun. unexpectedExceptionCount)
311
+ expectEqual ( 1 , testRun. totalFailureCount)
312
+ expectFalse ( testRun. hasSucceeded)
313
+ }
314
+ }
315
+
316
+ XCTestTestSuite . test ( " XCTAsserts with throwing expressions " ) {
317
+ class ErrorTestCase : XCTestCase {
318
+ var doThrow = true
319
+ var errorCode = 42
320
+
321
+ dynamic func throwSomething( ) throws -> String {
322
+ if doThrow {
323
+ throw NSError ( domain: " MyDomain " , code: errorCode, userInfo: nil )
324
+ }
325
+ return " Hello "
299
326
}
300
-
327
+
328
+ dynamic func test_withThrowing( ) {
329
+ XCTAssertEqual ( try throwSomething ( ) , " Hello " )
330
+ }
331
+ }
332
+
333
+ // Try success case
334
+ do {
335
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_withThrowing) )
336
+ testCase. doThrow = false
337
+ execute ( testCase. run)
338
+ let testRun = testCase. testRun!
339
+
340
+ expectEqual ( 1 , testRun. testCaseCount)
341
+ expectEqual ( 1 , testRun. executionCount)
342
+ expectEqual ( 0 , testRun. failureCount)
343
+ expectEqual ( 0 , testRun. unexpectedExceptionCount)
344
+ expectEqual ( 0 , testRun. totalFailureCount)
345
+ expectTrue ( testRun. hasSucceeded)
346
+ }
347
+
348
+ // Now try when the expression throws
349
+ do {
350
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_withThrowing) )
351
+ execute ( testCase. run)
352
+ let testRun = testCase. testRun!
353
+
354
+ expectEqual ( 1 , testRun. testCaseCount)
355
+ expectEqual ( 1 , testRun. executionCount)
356
+ expectEqual ( 0 , testRun. failureCount)
357
+ expectEqual ( 1 , testRun. unexpectedExceptionCount)
358
+ expectEqual ( 1 , testRun. totalFailureCount)
359
+ expectFalse ( testRun. hasSucceeded)
360
+ }
301
361
}
302
362
303
363
XCTestTestSuite . test ( " Test methods that wind up throwing " ) {
0 commit comments