-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestNSError.swift
231 lines (190 loc) · 7.79 KB
/
TestNSError.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
struct SwiftCustomNSError: Error, CustomNSError {
}
class TestNSError : XCTestCase {
func test_LocalizedError_errorDescription() {
struct Error : LocalizedError {
var errorDescription: String? { return "error description" }
}
let error = Error()
XCTAssertEqual(error.localizedDescription, "error description")
}
func test_NSErrorAsError_localizedDescription() {
let nsError = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Localized!"])
let error = nsError as Error
XCTAssertEqual(error.localizedDescription, "Localized!")
}
func test_NSError_inDictionary() {
let error = NSError(domain: "domain", code: 42, userInfo: nil)
let nsdictionary = ["error": error] as NSDictionary
let dictionary = nsdictionary as? Dictionary<String, Error>
XCTAssertNotNil(dictionary)
XCTAssertEqual(error, dictionary?["error"] as? NSError)
}
func test_CustomNSError_domain() {
let name = testBundleName()
XCTAssertEqual(SwiftCustomNSError.errorDomain, "\(name).SwiftCustomNSError")
}
func test_CustomNSError_userInfo() {
let userInfo = SwiftCustomNSError().errorUserInfo
XCTAssertTrue(userInfo.isEmpty)
}
func test_CustomNSError_errorCode() {
enum SwiftError : Error, CustomNSError {
case zero
case one
case two
}
XCTAssertEqual(SwiftCustomNSError().errorCode, 1)
XCTAssertEqual(SwiftError.zero.errorCode, 0)
XCTAssertEqual(SwiftError.one.errorCode, 1)
XCTAssertEqual(SwiftError.two.errorCode, 2)
}
func test_CustomNSError_errorCodeRawInt() {
enum SwiftError : Int, Error, CustomNSError {
case minusOne = -1
case fortyTwo = 42
}
XCTAssertEqual(SwiftError.minusOne.errorCode, -1)
XCTAssertEqual(SwiftError.fortyTwo.errorCode, 42)
}
func test_CustomNSError_errorCodeRawUInt() {
enum SwiftError : UInt, Error, CustomNSError {
case fortyTwo = 42
}
XCTAssertEqual(SwiftError.fortyTwo.errorCode, 42)
}
func test_errorConvenience() {
let error = CocoaError.error(.fileReadNoSuchFile, url: URL(fileURLWithPath: #file))
let nsError = error as NSError
XCTAssertEqual(nsError._domain, NSCocoaErrorDomain)
XCTAssertEqual(nsError._code, CocoaError.fileReadNoSuchFile.rawValue)
if let filePath = nsError.userInfo[NSURLErrorKey] as? URL {
XCTAssertEqual(filePath, URL(fileURLWithPath: #file))
} else {
XCTFail()
}
}
#if !canImport(ObjectiveC) || DARWIN_COMPATIBILITY_TESTS
func test_ConvertErrorToNSError_domain() {
struct CustomSwiftError: Error {
}
XCTAssertTrue((CustomSwiftError() as NSError).domain.contains("CustomSwiftError"))
}
func test_ConvertErrorToNSError_errorCode() {
enum SwiftError: Error {
case zero
case one
case two
}
XCTAssertEqual((SwiftError.zero as NSError).code, 0)
XCTAssertEqual((SwiftError.one as NSError).code, 1)
XCTAssertEqual((SwiftError.two as NSError).code, 2)
}
func test_ConvertErrorToNSError_errorCodeRawInt() {
enum SwiftError: Int, Error {
case minusOne = -1
case fortyTwo = 42
}
XCTAssertEqual((SwiftError.minusOne as NSError).code, -1)
XCTAssertEqual((SwiftError.fortyTwo as NSError).code, 42)
}
func test_ConvertErrorToNSError_errorCodeRawUInt() {
enum SwiftError: UInt, Error {
case fortyTwo = 42
}
XCTAssertEqual((SwiftError.fortyTwo as NSError).code, 42)
}
func test_ConvertErrorToNSError_errorCodeWithAssosiatedValue() {
// Default error code for enum case is based on EnumImplStrategy::getTagIndex
enum SwiftError: Error {
case one // 2
case two // 3
case three(String) // 0
case four // 4
case five(String) // 1
}
XCTAssertEqual((SwiftError.one as NSError).code, 2)
XCTAssertEqual((SwiftError.two as NSError).code, 3)
XCTAssertEqual((SwiftError.three("three") as NSError).code, 0)
XCTAssertEqual((SwiftError.four as NSError).code, 4)
XCTAssertEqual((SwiftError.five("five") as NSError).code, 1)
}
#endif
}
class TestURLError: XCTestCase {
static let testURL = URL(string: "https://swift.org")!
let userInfo: [String: Any] = [
NSURLErrorFailingURLErrorKey: TestURLError.testURL,
NSURLErrorFailingURLStringErrorKey: TestURLError.testURL.absoluteString,
]
func test_errorCode() {
let e = URLError(.unsupportedURL)
XCTAssertEqual(e.errorCode, URLError.Code.unsupportedURL.rawValue)
}
func test_failingURL() {
let e = URLError(.badURL, userInfo: userInfo)
XCTAssertNotNil(e.failingURL)
XCTAssertEqual(e.failingURL, e.userInfo[NSURLErrorFailingURLErrorKey] as? URL)
}
func test_failingURLString() {
let e = URLError(.badURL, userInfo: userInfo)
XCTAssertNotNil(e.failureURLString)
XCTAssertEqual(e.failureURLString, e.userInfo[NSURLErrorFailingURLStringErrorKey] as? String)
}
}
class TestCocoaError: XCTestCase {
static let testURL = URL(string: "file:///")!
let userInfo: [String: Any] = [
NSURLErrorKey: TestCocoaError.testURL,
NSFilePathErrorKey: TestCocoaError.testURL.path,
NSUnderlyingErrorKey: POSIXError(.EACCES),
NSStringEncodingErrorKey: Int(String.Encoding.utf16.rawValue),
]
func test_errorCode() {
let e = CocoaError(.fileReadNoSuchFile)
XCTAssertEqual(e.errorCode, CocoaError.Code.fileReadNoSuchFile.rawValue)
XCTAssertEqual(e.isCoderError, false)
XCTAssertEqual(e.isExecutableError, false)
XCTAssertEqual(e.isFileError, true)
XCTAssertEqual(e.isFormattingError, false)
XCTAssertEqual(e.isPropertyListError, false)
XCTAssertEqual(e.isUbiquitousFileError, false)
XCTAssertEqual(e.isUserActivityError, false)
XCTAssertEqual(e.isValidationError, false)
XCTAssertEqual(e.isXPCConnectionError, false)
}
func test_filePath() {
let e = CocoaError(.fileWriteNoPermission, userInfo: userInfo)
XCTAssertNotNil(e.filePath)
XCTAssertEqual(e.filePath, TestCocoaError.testURL.path)
}
func test_url() {
// TODO: Re-enable once Foundation.URL is ported from FoundationEssentials
// let e = CocoaError(.fileReadNoSuchFile, userInfo: userInfo)
// XCTAssertNotNil(e.url)
// XCTAssertEqual(e.url, TestCocoaError.testURL)
}
func test_stringEncoding() {
let e = CocoaError(.fileReadUnknownStringEncoding, userInfo: userInfo)
XCTAssertNotNil(e.stringEncoding)
XCTAssertEqual(e.stringEncoding, .utf16)
}
func test_underlying() {
let e = CocoaError(.fileWriteNoPermission, userInfo: userInfo)
XCTAssertNotNil(e.underlying as? POSIXError)
XCTAssertEqual(e.underlying as? POSIXError, POSIXError.init(.EACCES))
}
func test_forceCast() {
let nsError = NSError(domain: NSCocoaErrorDomain, code: CocoaError.coderInvalidValue.rawValue)
let error = nsError as! CocoaError
XCTAssertEqual(error.errorCode, CocoaError.coderInvalidValue.rawValue)
}
}