@@ -107,3 +107,96 @@ class TestNSError : XCTestCase {
107
107
}
108
108
}
109
109
}
110
+
111
+ class TestURLError : XCTestCase {
112
+
113
+ static var allTests : [ ( String , ( TestURLError ) -> ( ) throws -> Void ) ] {
114
+ return [
115
+ ( " test_errorCode " , TestURLError . test_errorCode) ,
116
+ ( " test_failingURL " , TestURLError . test_failingURL) ,
117
+ ( " test_failingURLString " , TestURLError . test_failingURLString) ,
118
+ ]
119
+ }
120
+
121
+ static let testURL = URL ( string: " https://swift.org " ) !
122
+ let userInfo : [ String : Any ] = [
123
+ NSURLErrorFailingURLErrorKey: TestURLError . testURL,
124
+ NSURLErrorFailingURLStringErrorKey: TestURLError . testURL. absoluteString,
125
+ ]
126
+
127
+ func test_errorCode( ) {
128
+ let e = URLError ( . unsupportedURL)
129
+ XCTAssertEqual ( e. errorCode, URLError . Code. unsupportedURL. rawValue)
130
+ }
131
+
132
+ func test_failingURL( ) {
133
+ let e = URLError ( . badURL, userInfo: userInfo)
134
+ XCTAssertNotNil ( e. failingURL)
135
+ XCTAssertEqual ( e. failingURL, e. userInfo [ NSURLErrorFailingURLErrorKey] as? URL )
136
+ }
137
+
138
+ func test_failingURLString( ) {
139
+ let e = URLError ( . badURL, userInfo: userInfo)
140
+ XCTAssertNotNil ( e. failureURLString)
141
+ XCTAssertEqual ( e. failureURLString, e. userInfo [ NSURLErrorFailingURLStringErrorKey] as? String )
142
+ }
143
+ }
144
+
145
+ class TestCocoaError : XCTestCase {
146
+
147
+ static var allTests : [ ( String , ( TestCocoaError ) -> ( ) throws -> Void ) ] {
148
+ return [
149
+ ( " test_errorCode " , TestCocoaError . test_errorCode) ,
150
+ ( " test_filePath " , TestCocoaError . test_filePath) ,
151
+ ( " test_url " , TestCocoaError . test_url) ,
152
+ ( " test_stringEncoding " , TestCocoaError . test_stringEncoding) ,
153
+ ( " test_underlying " , TestCocoaError . test_underlying) ,
154
+ ]
155
+ }
156
+
157
+ static let testURL = URL ( string: " file:/// " ) !
158
+ let userInfo : [ String : Any ] = [
159
+ NSURLErrorKey: TestCocoaError . testURL,
160
+ NSFilePathErrorKey: TestCocoaError . testURL. path,
161
+ NSUnderlyingErrorKey: POSIXError ( . EACCES) ,
162
+ NSStringEncodingErrorKey: String . Encoding. utf16. rawValue,
163
+ ]
164
+
165
+ func test_errorCode( ) {
166
+ let e = CocoaError ( . fileReadNoSuchFile)
167
+ XCTAssertEqual ( e. errorCode, CocoaError . Code. fileReadNoSuchFile. rawValue)
168
+ XCTAssertEqual ( e. isCoderError, false )
169
+ XCTAssertEqual ( e. isExecutableError, false )
170
+ XCTAssertEqual ( e. isFileError, true )
171
+ XCTAssertEqual ( e. isFormattingError, false )
172
+ XCTAssertEqual ( e. isPropertyListError, false )
173
+ XCTAssertEqual ( e. isUbiquitousFileError, false )
174
+ XCTAssertEqual ( e. isUserActivityError, false )
175
+ XCTAssertEqual ( e. isValidationError, false )
176
+ XCTAssertEqual ( e. isXPCConnectionError, false )
177
+ }
178
+
179
+ func test_filePath( ) {
180
+ let e = CocoaError ( . fileWriteNoPermission, userInfo: userInfo)
181
+ XCTAssertNotNil ( e. filePath)
182
+ XCTAssertEqual ( e. filePath, TestCocoaError . testURL. path)
183
+ }
184
+
185
+ func test_url( ) {
186
+ let e = CocoaError ( . fileReadNoSuchFile, userInfo: userInfo)
187
+ XCTAssertNotNil ( e. url)
188
+ XCTAssertEqual ( e. url, TestCocoaError . testURL)
189
+ }
190
+
191
+ func test_stringEncoding( ) {
192
+ let e = CocoaError ( . fileReadUnknownStringEncoding, userInfo: userInfo)
193
+ XCTAssertNotNil ( e. stringEncoding)
194
+ XCTAssertEqual ( e. stringEncoding, . utf16)
195
+ }
196
+
197
+ func test_underlying( ) {
198
+ let e = CocoaError ( . fileWriteNoPermission, userInfo: userInfo)
199
+ XCTAssertNotNil ( e. underlying as? POSIXError )
200
+ XCTAssertEqual ( e. underlying as? POSIXError , POSIXError . init ( . EACCES) )
201
+ }
202
+ }
0 commit comments