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,22 @@
import PMKFoundation
import Foundation
import PromiseKit
import XCTest
class NSNotificationCenterTests: XCTestCase {
func test() {
let ex = expectation(description: "")
let userInfo = ["a": 1]
NotificationCenter.default.observe(once: PMKTestNotification).done { value in
XCTAssertEqual(value.userInfo?.count, 1)
ex.fulfill()
}
NotificationCenter.default.post(name: PMKTestNotification, object: nil, userInfo: userInfo)
waitForExpectations(timeout: 1)
}
}
private let PMKTestNotification = Notification.Name("PMKTestNotification")

View File

@@ -0,0 +1,76 @@
import PMKFoundation
import Foundation
import PromiseKit
import XCTest
class NSObjectTests: XCTestCase {
func testKVO() {
let ex = expectation(description: "")
let foo = Foo()
foo.observe(.promise, keyPath: "bar").done { newValue in
XCTAssertEqual(newValue as? String, "moo")
ex.fulfill()
}.catch { _ in
XCTFail()
}
foo.bar = "moo"
waitForExpectations(timeout: 1)
}
func testAfterlife() {
let ex = expectation(description: "")
var killme: NSObject!
autoreleasepool {
func innerScope() {
killme = NSObject()
after(life: killme).done { _ in
//
ex.fulfill()
}
}
innerScope()
after(.milliseconds(200)).done {
killme = nil
}
}
waitForExpectations(timeout: 1)
}
func testMultiObserveAfterlife() {
let ex1 = expectation(description: "")
let ex2 = expectation(description: "")
var killme: NSObject!
autoreleasepool {
func innerScope() {
killme = NSObject()
after(life: killme).done { _ in
ex1.fulfill()
}
after(life: killme).done { _ in
ex2.fulfill()
}
}
innerScope()
after(.milliseconds(200)).done {
killme = nil
}
}
waitForExpectations(timeout: 1)
}
}
private class Foo: NSObject {
@objc dynamic var bar: String = "bar"
}

View File

@@ -0,0 +1,50 @@
import PMKFoundation
import Foundation
import PromiseKit
import XCTest
#if os(macOS)
class NSTaskTests: XCTestCase {
func test1() {
let ex = expectation(description: "")
let task = Process()
task.launchPath = "/usr/bin/basename"
task.arguments = ["/foo/doe/bar"]
task.launch(.promise).done { stdout, _ in
let stdout = String(data: stdout.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
XCTAssertEqual(stdout, "bar\n")
ex.fulfill()
}
waitForExpectations(timeout: 10)
}
func test2() {
let ex = expectation(description: "")
let dir = "PMKAbsentDirectory"
let task = Process()
task.launchPath = "/bin/ls"
task.arguments = [dir]
task.launch(.promise).done { _ in
XCTFail()
}.catch { err in
do {
throw err
} catch Process.PMKError.execution(let proc, let stdout, let stderr) {
let expectedStderr = "ls: \(dir): No such file or directory\n"
XCTAssertEqual(stderr, expectedStderr)
XCTAssertEqual(proc.terminationStatus, 1)
XCTAssertEqual(stdout?.count ?? 0, 0)
} catch {
XCTFail()
}
ex.fulfill()
}
waitForExpectations(timeout: 10)
}
}
#endif

View File

@@ -0,0 +1,55 @@
@import PMKFoundation;
@import OHHTTPStubs;
@import Foundation;
@import PromiseKit;
@import XCTest;
@implementation NSURLSessionTests: XCTestCase
- (void)tearDown {
[OHHTTPStubs removeAllStubs];
}
- (void)test200 {
id stubData = [NSData dataWithBytes:"a" length:1];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *rq){
return [rq.URL.host isEqualToString:@"example.com"];
} withStubResponse:^(NSURLRequest *request){
return [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:@{@"Content-Type": @"text/html"}];
}];
id ex = [self expectationWithDescription:@""];
id rq = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
[[NSURLSession sharedSession] promiseDataTaskWithRequest:rq].then(^{
[ex fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)testBadJSON {
id stubData = [NSData dataWithBytes:"[a: 3]" length:1];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *rq){
return [rq.URL.host isEqualToString:@"example.com"];
} withStubResponse:^(NSURLRequest *request){
return [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:@{@"Content-Type": @"application/json"}];
}];
id ex = [self expectationWithDescription:@""];
id rq = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
[[NSURLSession sharedSession] promiseDataTaskWithRequest:rq].catch(^(NSError *err){
XCTAssertEqualObjects(err.domain, NSCocoaErrorDomain); //TODO this is why we should replace this domain
XCTAssertEqual(err.code, 3840);
XCTAssertEqualObjects(err.userInfo[PMKURLErrorFailingDataKey], stubData);
XCTAssertNotNil(err.userInfo[PMKURLErrorFailingURLResponseKey]);
[ex fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
}
@end

View File

@@ -0,0 +1,76 @@
import PMKFoundation
import OHHTTPStubs
import PromiseKit
import XCTest
class NSURLSessionTests: XCTestCase {
func test1() {
let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]]
OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in
return OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: nil)
}
let ex = expectation(description: "")
let rq = URLRequest(url: URL(string: "http://example.com")!)
firstly {
URLSession.shared.dataTask(.promise, with: rq)
}.compactMap {
try JSONSerialization.jsonObject(with: $0.data) as? NSDictionary
}.done { rsp in
XCTAssertEqual(json, rsp)
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
func test2() {
// test that URLDataPromise chains thens
// this test because I dont trust the Swift compiler
let dummy = ("fred" as NSString).data(using: String.Encoding.utf8.rawValue)!
OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in
return OHHTTPStubsResponse(data: dummy, statusCode: 200, headers: [:])
}
let ex = expectation(description: "")
let rq = URLRequest(url: URL(string: "http://example.com")!)
after(.milliseconds(100)).then {
URLSession.shared.dataTask(.promise, with: rq)
}.done { x in
XCTAssertEqual(x.data, dummy)
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
/// test that our convenience String constructor applies
func test3() {
let dummy = "fred"
OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in
let data = dummy.data(using: .utf8)!
return OHHTTPStubsResponse(data: data, statusCode: 200, headers: [:])
}
let ex = expectation(description: "")
let rq = URLRequest(url: URL(string: "http://example.com")!)
after(.milliseconds(100)).then {
URLSession.shared.dataTask(.promise, with: rq)
}.map(String.init).done {
XCTAssertEqual($0, dummy)
ex.fulfill()
}
waitForExpectations(timeout: 1)
}
override func tearDown() {
OHHTTPStubs.removeAllStubs()
}
}