Add PromiseKit dependency

- Added PromiseKit dependency
This commit is contained in:
2018-11-15 22:08:00 -04:00
parent 2689d86c18
commit be7b6b5881
541 changed files with 46282 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
import PMKCoreLocation
import CoreLocation
import PromiseKit
import XCTest
#if os(iOS) || os(watchOS) || os(OSX)
import class Contacts.CNPostalAddress
#endif
class CLGeocoderTests: XCTestCase {
func test_reverseGeocodeLocation() {
class MockGeocoder: CLGeocoder {
override func reverseGeocodeLocation(_ location: CLLocation, completionHandler: @escaping CLGeocodeCompletionHandler) {
after(.seconds(0)).done {
completionHandler([dummyPlacemark], nil)
}
}
}
let ex = expectation(description: "")
MockGeocoder().reverseGeocode(location: CLLocation()).done { x in
XCTAssertEqual(x, [dummyPlacemark])
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
func test_geocodeAddressDictionary() {
class MockGeocoder: CLGeocoder {
override func geocodeAddressDictionary(_ addressDictionary: [AnyHashable : Any], completionHandler: @escaping CLGeocodeCompletionHandler) {
after(.seconds(0)).done {
completionHandler([dummyPlacemark], nil)
}
}
}
let ex = expectation(description: "")
MockGeocoder().geocode([:]).done { x in
XCTAssertEqual(x, [dummyPlacemark])
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
func test_geocodeAddressString() {
class MockGeocoder: CLGeocoder {
override func geocodeAddressString(_ addressString: String, completionHandler: @escaping CLGeocodeCompletionHandler) {
after(.seconds(0)).done {
completionHandler([dummyPlacemark], nil)
}
}
}
let ex = expectation(description: "")
MockGeocoder().geocode("").done { x in
XCTAssertEqual(x, [dummyPlacemark])
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
#if !os(tvOS) && swift(>=3.2)
func test_geocodePostalAddress() {
guard #available(iOS 11.0, OSX 10.13, watchOS 4.0, *) else { return }
class MockGeocoder: CLGeocoder {
override func geocodePostalAddress(_ postalAddress: CNPostalAddress, completionHandler: @escaping CLGeocodeCompletionHandler) {
after(.seconds(0)).done {
completionHandler([dummyPlacemark], nil)
}
}
}
let ex = expectation(description: "")
MockGeocoder().geocodePostalAddress(CNPostalAddress()).done { x in
XCTAssertEqual(x, [dummyPlacemark])
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
func test_geocodePostalAddressLocale() {
guard #available(iOS 11.0, OSX 10.13, watchOS 4.0, *) else { return }
class MockGeocoder: CLGeocoder {
override func geocodePostalAddress(_ postalAddress: CNPostalAddress, preferredLocale locale: Locale?, completionHandler: @escaping CLGeocodeCompletionHandler) {
after(.seconds(0)).done {
completionHandler([dummyPlacemark], nil)
}
}
}
let ex = expectation(description: "")
MockGeocoder().geocodePostalAddress(CNPostalAddress(), preferredLocale: nil).done { x in
XCTAssertEqual(x, [dummyPlacemark])
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
#endif
}
private let dummyPlacemark = CLPlacemark()

View File

@@ -0,0 +1,95 @@
import PMKCoreLocation
import CoreLocation
import PromiseKit
import XCTest
#if !os(tvOS)
class Test_CLLocationManager_Swift: XCTestCase {
func test_fulfills_with_multiple_locations() {
swizzle(CLLocationManager.self, #selector(CLLocationManager.startUpdatingLocation)) {
swizzle(CLLocationManager.self, #selector(CLLocationManager.authorizationStatus), isClassMethod: true) {
let ex = expectation(description: "")
CLLocationManager.requestLocation().done { x in
XCTAssertEqual(x, dummy)
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
}
}
func test_fufillsWithSatisfyingBlock() {
swizzle(CLLocationManager.self, #selector(CLLocationManager.startUpdatingLocation)) {
swizzle(CLLocationManager.self, #selector(CLLocationManager.authorizationStatus), isClassMethod: true) {
let ex = expectation(description: "")
let block: ((CLLocation) -> Bool) = { location in
return location.coordinate.latitude == dummy.last?.coordinate.latitude
}
CLLocationManager.requestLocation(satisfying: block).done({ locations in
locations.forEach { XCTAssert(block($0) == true, "Block should be successful for returned values") }
ex.fulfill()
})
waitForExpectations(timeout: 1)
}
}
}
#if os(iOS)
func test_requestAuthorization() {
let ex = expectation(description: "")
CLLocationManager.requestAuthorization().done {
XCTAssertEqual($0, CLAuthorizationStatus.restricted)
ex.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
}
#endif
}
/////////////////////////////////////////////////////////////// resources
private let dummy = [CLLocation(latitude: 0, longitude: 0), CLLocation(latitude: 10, longitude: 20)]
extension CLLocationManager {
@objc func pmk_startUpdatingLocation() {
after(.milliseconds(100)).done {
self.delegate!.locationManager?(self, didUpdateLocations: dummy)
}
}
@objc static func pmk_authorizationStatus() -> CLAuthorizationStatus {
#if os(macOS)
return .authorized
#else
return .authorizedWhenInUse
#endif
}
}
/////////////////////////////////////////////////////////////// utilities
import ObjectiveC
func swizzle(_ foo: AnyClass, _ from: Selector, isClassMethod: Bool = false, body: () -> Void) {
let originalMethod: Method
let swizzledMethod: Method
if isClassMethod {
originalMethod = class_getClassMethod(foo, from)!
swizzledMethod = class_getClassMethod(foo, Selector("pmk_\(from)"))!
} else {
originalMethod = class_getInstanceMethod(foo, from)!
swizzledMethod = class_getInstanceMethod(foo, Selector("pmk_\(from)"))!
}
method_exchangeImplementations(originalMethod, swizzledMethod)
body()
method_exchangeImplementations(swizzledMethod, originalMethod)
}
#endif

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.private.tcc.allow</key>
<array>
<string>kTCCServiceAddressBook</string>
<string>kTCCServiceCalendar</string>
<string>kTCCServicePhotos</string>
</array>
</dict>
</plist>