@@ -252,6 +252,75 @@ XCTestTestSuite.test("XCTAssertThrowsError") {
252
252
253
253
}
254
254
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
+ }
264
+ }
265
+
266
+ dynamic func test_throws( ) {
267
+
268
+ XCTAssertNoThrow ( try throwSomething ( ) ) {
269
+ error in
270
+ let nserror = error as NSError
271
+ XCTAssertEqual ( nserror. domain, " MyDomain " )
272
+ XCTAssertEqual ( nserror. code, 42 )
273
+ }
274
+ }
275
+ }
276
+
277
+ // Success
278
+ do {
279
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_throws) )
280
+ testCase. doThrow = false
281
+ execute ( testCase. run)
282
+ let testRun = testCase. testRun!
283
+
284
+ expectEqual ( 1 , testRun. testCaseCount)
285
+ expectEqual ( 1 , testRun. executionCount)
286
+ expectEqual ( 0 , testRun. failureCount)
287
+ expectEqual ( 0 , testRun. unexpectedExceptionCount)
288
+ expectEqual ( 0 , testRun. totalFailureCount)
289
+ expectTrue ( testRun. hasSucceeded)
290
+ }
291
+
292
+ // Failure
293
+ do {
294
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_throws) )
295
+ execute ( testCase. run)
296
+ let testRun = testCase. testRun!
297
+
298
+ expectEqual ( 1 , testRun. testCaseCount)
299
+ expectEqual ( 1 , testRun. executionCount)
300
+ expectEqual ( 1 , testRun. failureCount)
301
+ expectEqual ( 0 , testRun. unexpectedExceptionCount)
302
+ expectEqual ( 1 , testRun. totalFailureCount)
303
+ expectFalse ( testRun. hasSucceeded)
304
+ }
305
+
306
+ // Throws wrong thing
307
+ do {
308
+ let testCase = ErrorTestCase ( selector: #selector( ErrorTestCase . test_throws) )
309
+ testCase. errorCode = 23
310
+ execute ( testCase. run)
311
+ let testRun = testCase. testRun!
312
+
313
+ expectEqual ( 1 , testRun. testCaseCount)
314
+ expectEqual ( 1 , testRun. executionCount)
315
+ expectEqual ( 1 , testRun. failureCount)
316
+ expectEqual ( 0 , testRun. unexpectedExceptionCount)
317
+ expectEqual ( 1 , testRun. totalFailureCount)
318
+ expectFalse ( testRun. hasSucceeded)
319
+ }
320
+
321
+ }
322
+
323
+
255
324
XCTestTestSuite . test ( " XCTAsserts with throwing expressions " ) {
256
325
class ErrorTestCase : XCTestCase {
257
326
var doThrow = true
0 commit comments