16
16
import SwiftXCTest
17
17
#endif
18
18
19
+ struct SwiftCustomNSError : Error , CustomNSError {
20
+ }
19
21
20
22
class TestNSError : XCTestCase {
21
23
22
24
static var allTests : [ ( String , ( TestNSError ) -> ( ) throws -> Void ) ] {
23
25
return [
24
26
( " test_LocalizedError_errorDescription " , test_LocalizedError_errorDescription) ,
25
27
( " test_NSErrorAsError_localizedDescription " , test_NSErrorAsError_localizedDescription) ,
28
+ ( " test_CustomNSError_domain " , test_CustomNSError_domain) ,
29
+ ( " test_CustomNSError_userInfo " , test_CustomNSError_userInfo) ,
30
+ ( " test_CustomNSError_errorCode " , test_CustomNSError_errorCode) ,
31
+ ( " test_CustomNSError_errorCodeRawInt " , test_CustomNSError_errorCodeRawInt) ,
32
+ ( " test_CustomNSError_errorCodeRawUInt " , test_CustomNSError_errorCodeRawUInt) ,
26
33
]
27
34
}
28
35
@@ -40,4 +47,45 @@ class TestNSError : XCTestCase {
40
47
let error = nsError as Error
41
48
XCTAssertEqual ( error. localizedDescription, " Localized! " )
42
49
}
50
+
51
+ func test_CustomNSError_domain( ) {
52
+ XCTAssertEqual ( SwiftCustomNSError . errorDomain, " TestFoundation.SwiftCustomNSError " )
53
+ }
54
+
55
+ func test_CustomNSError_userInfo( ) {
56
+ let userInfo = SwiftCustomNSError ( ) . errorUserInfo
57
+ XCTAssertTrue ( userInfo. isEmpty)
58
+ }
59
+
60
+ func test_CustomNSError_errorCode( ) {
61
+ enum SwiftError : Error , CustomNSError {
62
+ case zero
63
+ case one
64
+ case two
65
+ }
66
+
67
+ XCTAssertEqual ( SwiftCustomNSError ( ) . errorCode, 1 )
68
+
69
+ XCTAssertEqual ( SwiftError . zero. errorCode, 0 )
70
+ XCTAssertEqual ( SwiftError . one. errorCode, 1 )
71
+ XCTAssertEqual ( SwiftError . two. errorCode, 2 )
72
+ }
73
+
74
+ func test_CustomNSError_errorCodeRawInt( ) {
75
+ enum SwiftError : Int , Error , CustomNSError {
76
+ case minusOne = - 1
77
+ case fortyTwo = 42
78
+ }
79
+
80
+ XCTAssertEqual ( SwiftError . minusOne. errorCode, - 1 )
81
+ XCTAssertEqual ( SwiftError . fortyTwo. errorCode, 42 )
82
+ }
83
+
84
+ func test_CustomNSError_errorCodeRawUInt( ) {
85
+ enum SwiftError : UInt , Error , CustomNSError {
86
+ case fortyTwo = 42
87
+ }
88
+
89
+ XCTAssertEqual ( SwiftError . fortyTwo. errorCode, 42 )
90
+ }
43
91
}
0 commit comments