@@ -30,6 +30,20 @@ class ErrorHandling: XCTestCase {
3030 // Tests for XCTAssertNoThrow
3131 ( " test_shouldNotThrowErrorDefiningSuccess " , test_shouldNotThrowErrorDefiningSuccess) ,
3232 ( " test_shouldThrowErrorDefiningFailure " , test_shouldThrowErrorDefiningFailure) ,
33+
34+ // Tests for XCTUnwrap
35+ ( " test_shouldNotThrowErrorOnUnwrapSuccess " , test_shouldNotThrowErrorOnUnwrapSuccess) ,
36+ ( " test_shouldThrowErrorOnUnwrapFailure " , test_shouldThrowErrorOnUnwrapFailure) ,
37+ ( " test_shouldThrowErrorOnEvaluationFailure " , test_shouldThrowErrorOnEvaluationFailure) ,
38+ ( " test_implicitlyUnwrappedOptional_notNil " , test_implicitlyUnwrappedOptional_notNil) ,
39+ ( " test_implicitlyUnwrappedOptional_nil " , test_implicitlyUnwrappedOptional_nil) ,
40+ ( " test_unwrapAnyOptional_notNil " , test_unwrapAnyOptional_notNil) ,
41+ ( " test_unwrapAnyOptional_nil " , test_unwrapAnyOptional_nil) ,
42+ ( " test_shouldReportFailureOnUnwrapFailure_catchUnwrapFailure " , test_shouldReportFailureOnUnwrapFailure_catchUnwrapFailure) ,
43+ ( " test_shouldReportFailureOnUnwrapFailure_catchExpressionFailure " , test_shouldReportFailureOnUnwrapFailure_catchExpressionFailure) ,
44+ ( " test_shouldReportCorrectTypeOnUnwrapFailure " , test_shouldReportCorrectTypeOnUnwrapFailure) ,
45+ ( " test_shouldReportCustomFileLineLocation " , test_shouldReportCustomFileLineLocation) ,
46+ ( " test_shouldReportFailureNotOnMainThread " , test_shouldReportFailureNotOnMainThread) ,
3347 ]
3448 } ( )
3549
@@ -38,6 +52,7 @@ class ErrorHandling: XCTestCase {
3852
3953 enum SomeError : Swift . Error {
4054 case anError( String )
55+ case shouldNotBeReached
4156 }
4257
4358 func functionThatDoesThrowError( ) throws {
@@ -63,6 +78,8 @@ class ErrorHandling: XCTestCase {
6378 switch thrownError {
6479 case . anError( let message) :
6580 XCTAssertEqual ( message, " an error message " )
81+ default :
82+ XCTFail ( " Unexpected error: \( thrownError) " )
6683 }
6784 }
6885 }
@@ -80,6 +97,8 @@ class ErrorHandling: XCTestCase {
8097 switch thrownError {
8198 case . anError( let message) :
8299 XCTAssertEqual ( message, " " )
100+ default :
101+ XCTFail ( " Unexpected error: \( thrownError) " )
83102 }
84103 }
85104 }
@@ -121,14 +140,149 @@ class ErrorHandling: XCTestCase {
121140 func test_shouldThrowErrorDefiningFailure( ) {
122141 XCTAssertNoThrow ( try functionThatDoesThrowError ( ) )
123142 }
143+
144+ func functionShouldReturnOptionalButThrows( ) throws -> String ? {
145+ throw SomeError . anError ( " an error message " )
146+ }
147+
148+ // CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorOnUnwrapSuccess' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
149+ // CHECK: Test Case 'ErrorHandling.test_shouldNotThrowErrorOnUnwrapSuccess' passed \(\d+\.\d+ seconds\)
150+ func test_shouldNotThrowErrorOnUnwrapSuccess( ) throws {
151+ let optional : String ? = " is not nil "
152+
153+ let unwrapped = try XCTUnwrap ( optional)
154+ XCTAssertEqual ( unwrapped, optional)
155+ }
156+
157+ // CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorOnUnwrapFailure' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
158+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+4]]: error: ErrorHandling.test_shouldThrowErrorOnUnwrapFailure : XCTUnwrap failed: expected non-nil value of type "String" -
159+ // CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorOnUnwrapFailure' failed \(\d+\.\d+ seconds\)
160+ func test_shouldThrowErrorOnUnwrapFailure( ) throws {
161+ let optional : String ? = nil
162+ _ = try XCTUnwrap ( optional)
163+
164+ // Should not be reached:
165+ throw SomeError . shouldNotBeReached
166+ }
167+
168+ // CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorOnEvaluationFailure' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
169+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+4]]: error: ErrorHandling.test_shouldThrowErrorOnEvaluationFailure : XCTUnwrap threw error "anError\("an error message"\)" - Failure error message
170+ // CHECK: \<EXPR\>:0: error: ErrorHandling.test_shouldThrowErrorOnEvaluationFailure : threw error "anError\("an error message"\)"
171+ // CHECK: Test Case 'ErrorHandling.test_shouldThrowErrorOnEvaluationFailure' failed \(\d+\.\d+ seconds\)
172+ func test_shouldThrowErrorOnEvaluationFailure( ) throws {
173+ _ = try XCTUnwrap ( functionShouldReturnOptionalButThrows ( ) , " Failure error message " )
174+
175+ // Should not be reached:
176+ throw SomeError . shouldNotBeReached
177+ }
178+
179+ // CHECK: Test Case 'ErrorHandling.test_implicitlyUnwrappedOptional_notNil' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
180+ // CHECK: Test Case 'ErrorHandling.test_implicitlyUnwrappedOptional_notNil' passed \(\d+\.\d+ seconds\)
181+ func test_implicitlyUnwrappedOptional_notNil( ) throws {
182+ let implicitlyUnwrappedOptional : String ! = " is not nil "
183+
184+ let unwrapped = try XCTUnwrap ( implicitlyUnwrappedOptional)
185+ XCTAssertEqual ( unwrapped, implicitlyUnwrappedOptional)
186+ }
187+
188+ // CHECK: Test Case 'ErrorHandling.test_implicitlyUnwrappedOptional_nil' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
189+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+4]]: error: ErrorHandling.test_implicitlyUnwrappedOptional_nil : XCTUnwrap failed: expected non-nil value of type "String" - Failure error message
190+ // CHECK: Test Case 'ErrorHandling.test_implicitlyUnwrappedOptional_nil' failed \(\d+\.\d+ seconds\)
191+ func test_implicitlyUnwrappedOptional_nil( ) throws {
192+ let implicitlyUnwrappedOptional : String ! = nil
193+ _ = try XCTUnwrap ( implicitlyUnwrappedOptional, " Failure error message " )
194+
195+ // Should not be reached:
196+ throw SomeError . shouldNotBeReached
197+ }
198+
199+ // CHECK: Test Case 'ErrorHandling.test_unwrapAnyOptional_notNil' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
200+ // CHECK: Test Case 'ErrorHandling.test_unwrapAnyOptional_notNil' passed \(\d+\.\d+ seconds\)
201+ func test_unwrapAnyOptional_notNil( ) throws {
202+ let anyOptional : Any ? = " is not nil "
203+
204+ let unwrapped = try XCTUnwrap ( anyOptional)
205+ XCTAssertEqual ( unwrapped as! String , anyOptional as! String )
206+ }
207+
208+ // CHECK: Test Case 'ErrorHandling.test_unwrapAnyOptional_nil' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
209+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+4]]: error: ErrorHandling.test_unwrapAnyOptional_nil : XCTUnwrap failed: expected non-nil value of type "Any" - Failure error message
210+ // CHECK: Test Case 'ErrorHandling.test_unwrapAnyOptional_nil' failed \(\d+\.\d+ seconds\)
211+ func test_unwrapAnyOptional_nil( ) throws {
212+ let anyOptional : Any ? = nil
213+ _ = try XCTUnwrap ( anyOptional, " Failure error message " )
214+
215+ // Should not be reached:
216+ throw SomeError . shouldNotBeReached
217+ }
218+
219+ // CHECK: Test Case 'ErrorHandling.test_shouldReportFailureOnUnwrapFailure_catchUnwrapFailure' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
220+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+5]]: error: ErrorHandling.test_shouldReportFailureOnUnwrapFailure_catchUnwrapFailure : XCTUnwrap failed: expected non-nil value of type "String" -
221+ // CHECK: Test Case 'ErrorHandling.test_shouldReportFailureOnUnwrapFailure_catchUnwrapFailure' failed \(\d+\.\d+ seconds\)
222+ func test_shouldReportFailureOnUnwrapFailure_catchUnwrapFailure( ) {
223+ do {
224+ let optional : String ? = nil
225+ _ = try XCTUnwrap ( optional)
226+ } catch { }
227+ }
228+
229+ // CHECK: Test Case 'ErrorHandling.test_shouldReportFailureOnUnwrapFailure_catchExpressionFailure' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
230+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+4]]: error: ErrorHandling.test_shouldReportFailureOnUnwrapFailure_catchExpressionFailure : XCTUnwrap threw error "anError\("an error message"\)" -
231+ // CHECK: Test Case 'ErrorHandling.test_shouldReportFailureOnUnwrapFailure_catchExpressionFailure' failed \(\d+\.\d+ seconds\)
232+ func test_shouldReportFailureOnUnwrapFailure_catchExpressionFailure( ) {
233+ do {
234+ _ = try XCTUnwrap ( functionShouldReturnOptionalButThrows ( ) )
235+ } catch { }
236+ }
237+
238+ struct CustomType {
239+ var name : String
240+ }
241+
242+ // CHECK: Test Case 'ErrorHandling.test_shouldReportCorrectTypeOnUnwrapFailure' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
243+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+4]]: error: ErrorHandling.test_shouldReportCorrectTypeOnUnwrapFailure : XCTUnwrap failed: expected non-nil value of type "CustomType" -
244+ // CHECK: Test Case 'ErrorHandling.test_shouldReportCorrectTypeOnUnwrapFailure' failed \(\d+\.\d+ seconds\)
245+ func test_shouldReportCorrectTypeOnUnwrapFailure( ) throws {
246+ let customTypeOptional : CustomType ? = nil
247+ _ = try XCTUnwrap ( customTypeOptional)
248+
249+ // Should not be reached:
250+ throw SomeError . shouldNotBeReached
251+ }
252+
253+ // CHECK: Test Case 'ErrorHandling.test_shouldReportCustomFileLineLocation' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
254+ // CHECK: custom_file.swift:1234: error: ErrorHandling.test_shouldReportCustomFileLineLocation : XCTUnwrap failed: expected non-nil value of type "CustomType" -
255+ // CHECK: Test Case 'ErrorHandling.test_shouldReportCustomFileLineLocation' failed \(\d+\.\d+ seconds\)
256+ func test_shouldReportCustomFileLineLocation( ) throws {
257+ let customTypeOptional : CustomType ? = nil
258+ _ = try XCTUnwrap ( customTypeOptional, file: " custom_file.swift " , line: 1234 )
259+
260+ // Should not be reached:
261+ throw SomeError . shouldNotBeReached
262+ }
263+
264+ // CHECK: Test Case 'ErrorHandling.test_shouldReportFailureNotOnMainThread' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
265+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+7]]: error: ErrorHandling.test_shouldReportFailureNotOnMainThread : XCTUnwrap failed: expected non-nil value of type "CustomType" -
266+ // CHECK: Test Case 'ErrorHandling.test_shouldReportFailureNotOnMainThread' failed \(\d+\.\d+ seconds\)
267+ func test_shouldReportFailureNotOnMainThread( ) throws {
268+ let queue = DispatchQueue ( label: " Test " )
269+ let semaphore = DispatchSemaphore ( value: 0 )
270+ queue. async {
271+ let customTypeOptional : CustomType ? = nil
272+ _ = try ? XCTUnwrap ( customTypeOptional)
273+ semaphore. signal ( )
274+ }
275+
276+ semaphore. wait ( )
277+ }
124278}
125279
126280// CHECK: Test Suite 'ErrorHandling' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
127- // CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
281+ // CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
128282
129283XCTMain ( [ testCase ( ErrorHandling . allTests) ] )
130284
131285// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
132- // CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
286+ // CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
133287// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
134- // CHECK: \t Executed \d+ tests, with \d+ failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
288+ // CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
0 commit comments