Skip to content

Commit

Permalink
Merge pull request #51 from XYOracleNetwork/feature/location-serializ…
Browse files Browse the repository at this point in the history
…ation

Location Serialization
  • Loading branch information
JoelBCarter authored Nov 22, 2024
2 parents 8de48fb + ca4b183 commit ec881fb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/XyoClient/Witness/Location/LocationService.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CoreLocation
import Foundation

public class LocationService: NSObject, CLLocationManagerDelegate {
public class LocationService: NSObject, CLLocationManagerDelegate, LocationServiceProtocol {
private let locationManager = CLLocationManager()
private var locationCompletion: ((Result<CLLocation, Error>) -> Void)?

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CoreLocation
import Foundation

public protocol LocationServiceProtocol {
func requestAuthorization()
func requestLocation(completion: @escaping (Result<CLLocation, Error>) -> Void)
}
20 changes: 19 additions & 1 deletion Sources/XyoClient/Witness/Location/LocationWitness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ import CoreLocation
import Foundation

open class LocationWitness: WitnessModuleAsync {
private var _locationService: LocationServiceProtocol?

private var locationService = LocationService()
private var locationService: LocationServiceProtocol {
if let service = _locationService {
return service
} else {
let initialized = LocationService()
self._locationService = initialized
return initialized
}
}

override public init(account: AccountInstance? = nil) {
super.init(account: account)
}

public convenience init(locationService: LocationServiceProtocol) {
self.init(account: nil)
self._locationService = locationService
}

override open func observe(completion: @escaping ([Payload]?, Error?) -> Void) {
locationService.requestAuthorization()
Expand Down
52 changes: 52 additions & 0 deletions Tests/XyoClientTests/Witness/LocationWitness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import CoreLocation
import XCTest

@testable import XyoClient

private class MockLocationService: LocationServiceProtocol {
var didRequestAuthorization = false
var simulatedResult: Result<CLLocation, Error>?

func requestAuthorization() {
didRequestAuthorization = true
}

func requestLocation(completion: @escaping (Result<CLLocation, Error>) -> Void) {
if let result = simulatedResult {
completion(result)
}
}
}

@available(iOS 13.0, *)
final class LocationWitnessTests: XCTestCase {
static var allTests = [
(
"observe:returnsMultipleLocationPayloads",
testLocationWitness_observe_returnsMultipleLocationPayloads
)
]

@available(iOS 15, *)
func testLocationWitness_observe_returnsMultipleLocationPayloads() async throws {
let locationServiceMock = MockLocationService()
let latitude: Double = 1
let longitude: Double = 2
locationServiceMock.simulatedResult = .success(
CLLocation(latitude: latitude, longitude: longitude))
let sut = LocationWitness(locationService: locationServiceMock)
let results = try await sut.observe()
// XCTAssertEqual(results.count, 2)
// let locationPayload = try XCTUnwrap(
// results.compactMap { $0 as? LocationPayload }.first, "Missing location payload.")
// XCTAssertEqual(locationPayload.schema, LocationPayload.schema)
// XCTAssertEqual(locationPayload.location.coordinate.latitude, lattitiude)
// XCTAssertEqual(locationPayload.location.coordinate.longitude, longitude)
let iosLocationPayload = try XCTUnwrap(
results.compactMap { $0 as? IosLocationPayload }.first, "Missing iOS location payload.")
XCTAssertEqual(iosLocationPayload.schema, IosLocationPayload.schema)
XCTAssertEqual(iosLocationPayload.location.coordinate.latitude, lattitiude)
XCTAssertEqual(iosLocationPayload.location.coordinate.longitude, longitude)

}
}

0 comments on commit ec881fb

Please sign in to comment.