This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreLocation | |
struct PointOfInterest: Codable, Equatable { | |
var name: String | |
} | |
class PointOfInterestSample { | |
var tableView: UITableView = UITableView() | |
var tableValues:[PointOfInterest]? | |
func handleError(_ error: Error?) -> Void { | |
print(error!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol APIRequest { | |
associatedtype RequestDataType | |
associatedtype ResponseDataType | |
func makeRequest(from data: RequestDataType) throws -> URLRequest | |
func parseResponse(data: Data) throws -> ResponseDataType | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct PointsOfInterestRequest: APIRequest { | |
enum RequestError: Error { | |
case invalidCoordinate | |
case unknown | |
} | |
func makeRequest(from coordinate: CLLocationCoordinate2D) throws -> URLRequest { | |
guard CLLocationCoordinate2DIsValid(coordinate) else { | |
throw RequestError.invalidCoordinate | |
} | |
var components = URLComponents(string: "https://example.com/locations")! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class APIRequestLoader<T: APIRequest> { | |
let apiRequest: T | |
let urlSession: URLSession | |
init(apiRequest: T, urlSession: URLSession = .shared) { | |
self.apiRequest = apiRequest | |
self.urlSession = urlSession | |
} | |
func loadAPIRequest(requestData: T.RequestDataType, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unit test :- Prepare URLRequest -> Parse Response | |
class PointOfInterestRequestTests: XCTestCase { | |
let request = PointsOfInterestRequest() | |
func testMakingURLRequest() throws { | |
let coordinate = CLLocationCoordinate2D(latitude: 37.3293, longitude: -121.8893) | |
let urlRequest = try request.makeRequest(from: coordinate) | |
XCTAssertEqual(urlRequest.url?.scheme, "https") | |
XCTAssertEqual(urlRequest.url?.host, "example.com") | |
XCTAssertEqual(urlRequest.url?.query, "lat=37.3293&long=-121.8893") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unit test :- Prepare URLRequest -> Parse Response | |
class PointOfInterestRequestTests: XCTestCase { | |
let request = PointsOfInterestRequest() | |
func testMakingURLRequest() throws { | |
let coordinate = CLLocationCoordinate2D(latitude: 37.3293, longitude: -121.8893) | |
let urlRequest = try request.makeRequest(from: coordinate) | |
XCTAssertEqual(urlRequest.url?.scheme, "https") | |
XCTAssertEqual(urlRequest.url?.host, "example.com") | |
XCTAssertEqual(urlRequest.url?.query, "lat=37.3293&long=-121.8893") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MockURLProtocol: URLProtocol { | |
static var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data))? | |
override func startLoading() { | |
guard let handler = MockURLProtocol.requestHandler else { | |
XCTFail("Received unexpected request with no handler set") | |
return | |
} | |
do { | |
let (response, data) = try handler(request) | |
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CurrentLocationProvider { | |
static let authChangedNotification = Notification.Name("AuthChanged") | |
func notifyAuthChanged() { | |
let name = CurrentLocationProvider.authChangedNotification | |
NotificationCenter.default.post(name: name, object: self) | |
} | |
} | |
class PointsOfInterestTableViewController { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CurrentLocationProvider { | |
static let authChangedNotification = Notification.Name("AuthChanged") | |
let notificationCenter: NotificationCenter | |
init(notificationCenter: NotificationCenter = .default) { | |
self.notificationCenter = notificationCenter | |
} | |
func notifyAuthChanged() { | |
let name = CurrentLocationProvider.authChangedNotification | |
notificationCenter.post(name: name, object: self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PointsOfInterestTableViewControllerTests: XCTestCase { | |
func testNotification() { | |
let notificationCenter = NotificationCenter() | |
let observer = PointsOfInterestTableViewController(notificationCenter: | |
notificationCenter) | |
XCTAssertFalse(observer.didHandleNotification) | |
// Notification posted to just this center, isolating the test | |
let name = CurrentLocationProvider.authChangedNotification | |
notificationCenter.post(name: name, object: nil) | |
XCTAssertTrue(observer.didHandleNotification) |
OlderNewer