Skip to content

Commit c85fa9f

Browse files
author
Clément Le Provost
authored
Swift 3.1 compliance (#294)
* Fix warning with Swift 3.1 * Fix compilation warnings in unit tests
1 parent af809de commit c85fa9f

File tree

8 files changed

+20
-21
lines changed

8 files changed

+20
-21
lines changed

Source/Network.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ internal class URLSessionLogger: NSObject, URLSession {
137137
stat.dataSize = Int(task.countOfBytesReceived)
138138
if let response = task.response as? HTTPURLResponse {
139139
stat.statusCode = response.statusCode
140-
} else if let error = task.error as? NSError {
140+
} else if let error = task.error as NSError? {
141141
stat.statusCode = error.code
142142
}
143143
self.stats.append(stat)

Tests/ClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ class ClientTests: OnlineTestCase {
472472
client.listIndexes { (content, error) in
473473
let stopTime = Date()
474474
let duration = stopTime.timeIntervalSince(startTime)
475-
guard let error = error as? NSError else { XCTFail("Request should have failed"); expectation.fulfill(); return }
475+
guard let error = error as NSError? else { XCTFail("Request should have failed"); expectation.fulfill(); return }
476476
XCTAssertEqual(NSURLErrorDomain, error.domain)
477477
XCTAssertEqual(NSURLErrorNotConnectedToInternet, error.code)
478478
XCTAssert(duration < min(self.client.searchTimeout, self.client.timeout)) // check that we failed without waiting for the timeout

Tests/IndexTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,19 @@ class IndexTests: OnlineTestCase {
306306
]
307307
index.addObjects(objects, completionHandler: { (content, error) -> Void in
308308
guard let content = content else {
309-
XCTFail("Error during addObjetcs: \(error)")
309+
XCTFail(String(describing: error))
310310
expectation.fulfill()
311311
return
312312
}
313313
self.index.waitTask(withID: content["taskID"] as! Int, completionHandler: { (content, error) -> Void in
314314
guard error == nil else {
315-
XCTFail("Error during waitTask: \(error)")
315+
XCTFail(String(describing: error))
316316
expectation.fulfill()
317317
return
318318
}
319319
self.index.getObjects(withIDs: ["1", "2"], attributesToRetrieve: ["name", "nonexistent"], completionHandler: { (content, error) -> Void in
320320
guard let content = content else {
321-
XCTFail("Error during getObjects: \(error)")
321+
XCTFail(String(describing: error))
322322
expectation.fulfill()
323323
return
324324
}
@@ -1110,20 +1110,20 @@ class IndexTests: OnlineTestCase {
11101110
index.searchCacheExpiringTimeInterval = timeout
11111111
index.addObjects(objects) { (content, error) in
11121112
guard error == nil else {
1113-
XCTFail("Error during addObjects: \(error)")
1113+
XCTFail(String(describing: error))
11141114
expectation.fulfill()
11151115
return
11161116
}
11171117
self.index.waitTask(withID: content!["taskID"] as! Int) { (content, error) in
11181118
guard error == nil else {
1119-
XCTFail("Error during waitTask: \(error)")
1119+
XCTFail(String(describing: error))
11201120
expectation.fulfill()
11211121
return
11221122
}
11231123
// Search a first time: there should be no cache.
11241124
self.index.search(Query()) { (content, error) in
11251125
guard error == nil else {
1126-
XCTFail("Error during search #1: \(error)")
1126+
XCTFail(String(describing: error))
11271127
expectation.fulfill()
11281128
return
11291129
}
@@ -1136,7 +1136,7 @@ class IndexTests: OnlineTestCase {
11361136
// Search a second time with the same query: we should hit the cache and not return an error.
11371137
self.index.search(Query()) { (content, error) in
11381138
guard error == nil else {
1139-
XCTFail("Error during search #2: \(error)")
1139+
XCTFail(String(describing: error))
11401140
expectation.fulfill()
11411141
return
11421142
}

Tests/NetworkTests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ class NetworkTests: XCTestCase {
8282
(content, error) -> Void in
8383
XCTAssertNil(content)
8484
XCTAssertNotNil(error)
85-
XCTAssert(error is NSError)
86-
XCTAssert((error as! NSError).domain == NSURLErrorDomain)
87-
XCTAssert((error as! NSError).code == NSURLErrorTimedOut)
85+
XCTAssertEqual((error! as NSError).domain, NSURLErrorDomain)
86+
XCTAssertEqual((error! as NSError).code, NSURLErrorTimedOut)
8887
expectation.fulfill()
8988
}
9089
self.waitForExpectations(timeout: expectationTimeout, handler: nil)
@@ -145,7 +144,7 @@ class NetworkTests: XCTestCase {
145144
(content, error) -> Void in
146145
XCTAssertNil(content)
147146
XCTAssertNotNil(error)
148-
let nsError = error as! NSError
147+
let nsError = error! as NSError
149148
XCTAssertEqual(NSCocoaErrorDomain, nsError.domain)
150149
XCTAssertEqual(NSPropertyListReadCorruptError, nsError.code)
151150
expectation.fulfill()
@@ -160,7 +159,7 @@ class NetworkTests: XCTestCase {
160159
client.listIndexes {
161160
(content, error) -> Void in
162161
XCTAssertNil(content)
163-
let nsError = error as! NSError
162+
let nsError = error! as NSError
164163
XCTAssertEqual(NSCocoaErrorDomain, nsError.domain)
165164
XCTAssertEqual(NSPropertyListReadCorruptError, nsError.code)
166165
expectation.fulfill()
@@ -178,7 +177,7 @@ class NetworkTests: XCTestCase {
178177
(content, error) -> Void in
179178
XCTAssertNil(content)
180179
XCTAssertNotNil(error)
181-
let nsError = error as! NSError
180+
let nsError = error! as NSError
182181
XCTAssertEqual(NSCocoaErrorDomain, nsError.domain)
183182
XCTAssertEqual(NSPropertyListReadCorruptError, nsError.code)
184183
expectation.fulfill()

Tests/ObjectiveCBridging.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ - (void)testQuery {
5858
// -------
5959
// Parameter accessors.
6060
[query setParameterWithName:@"foo" to:@"bar"];
61-
[query parameterWithName:@"foo"];
61+
XCTAssertNotNil([query parameterWithName:@"foo"]);
6262

6363
// Subscript.
6464
query[@"foo"] = [query[@"foo"] stringByAppendingString:@"baz"];

Tests/Offline/MirroredIndexTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class MirroredIndexTests: OfflineTestCase {
399399
index.search(Query()) { (content, error) in
400400
let stopTime = Date()
401401
let duration = stopTime.timeIntervalSince(startTime)
402-
guard let error = error as? NSError else { XCTFail("Request should have failed"); expectation.fulfill(); return }
402+
guard let error = error as NSError? else { XCTFail("Request should have failed"); expectation.fulfill(); return }
403403
XCTAssertEqual(NSURLErrorDomain, error.domain)
404404
XCTAssertEqual(NSURLErrorNotConnectedToInternet, error.code)
405405
XCTAssert(duration < min(self.client.searchTimeout, self.client.timeout)) // check that we failed without waiting for the timeout
@@ -408,7 +408,7 @@ class MirroredIndexTests: OfflineTestCase {
408408
reachability.reachable = true
409409
self.client.readHosts = [ "unknown.algolia.com" ]
410410
index.search(Query()) { (content, error) in
411-
guard let error = error as? NSError else { XCTFail("Request should have failed"); expectation.fulfill(); return }
411+
guard let error = error as NSError? else { XCTFail("Request should have failed"); expectation.fulfill(); return }
412412
XCTAssertEqual(NSURLErrorDomain, error.domain)
413413
// Check that we failed with something else than a connectivity error caused by reachability.
414414
XCTAssertNotEqual(NSURLErrorNotConnectedToInternet, error.code)

Tests/Offline/OfflineObjcBridging.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ - (void)testOfflineClient {
5151
OfflineClient* client = [[OfflineClient alloc] initWithAppID:@"APPID" apiKey:@"APIKEY"];
5252
[client enableOfflineModeWithLicenseKey:@"LICENSE_KEY"];
5353

54-
[client hasOfflineDataWithIndexName:@"name"];
54+
XCTAssertFalse([client hasOfflineDataWithIndexName:@"nonexistent"]);
5555

5656
// Operations
5757
// ----------

Tests/OnlineTestCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class OnlineTestCase: XCTestCase {
5252
// Although it's not shared with other test functions, it could remain from a previous execution.
5353
let expectation = self.expectation(description: "Delete index")
5454
client.deleteIndex(withName: index.name) { (content, error) -> Void in
55-
XCTAssertNil(error, "Error during deleteIndex: \(error)")
55+
XCTAssertNil(error)
5656
expectation.fulfill()
5757
}
5858
self.waitForExpectations(timeout: expectationTimeout, handler: nil)
@@ -63,7 +63,7 @@ class OnlineTestCase: XCTestCase {
6363

6464
let expectation = self.expectation(description: "Delete index")
6565
client.deleteIndex(withName: index.name) { (content, error) -> Void in
66-
XCTAssertNil(error, "Error during deleteIndex: \(error)")
66+
XCTAssertNil(error)
6767
expectation.fulfill()
6868
}
6969
self.waitForExpectations(timeout: expectationTimeout, handler: nil)

0 commit comments

Comments
 (0)