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,16 @@
import XCTest
class UITest_SLComposeViewController_Swift: PMKiOSUITestCase {
func test_can_cancel() {
let app = XCUIApplication()
app.tables.staticTexts["5"].tap()
sleep(5) // takes longer than usual
app.alerts.buttons["Cancel"].tap()
sleep(3) // takes longer than usual (this may only pass when you look at it)
XCTAssertTrue(value)
}
}

View File

@@ -0,0 +1,136 @@
@import PMKSocial;
@import XCTest;
@interface MockSLRequest: SLRequest
@property id response;
@property id data;
@property id error;
@end
@implementation MockSLRequest
- (void)performRequestWithHandler:(SLRequestHandler)handler {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
handler(_data, _response, _error);
});
}
@end
@implementation Test_SLRequest_ObjC: XCTestCase
- (void)test1 {
id url = [NSURL URLWithString:@"http://example.com"];
id input = @{@"3": @4};
MockSLRequest *request = (id) [MockSLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:@{@"1": @2}];
request.response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:200 HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"application/json"}];
request.data = [NSJSONSerialization dataWithJSONObject:input options:0 error:nil];
id ex = [self expectationWithDescription:@""];
[request promise].then(^(id json){
XCTAssertEqualObjects(json, input);
[ex fulfill];
});
[self waitForExpectationsWithTimeout:1 handler:nil];
}
- (void)test2 {
id url = [NSURL URLWithString:@"http://example.com"];
MockSLRequest *request = (id) [MockSLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:@{@"1": @2}];
request.response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:500 HTTPVersion:@"1.1" headerFields:@{}];
id ex = [self expectationWithDescription:@""];
[request promise].catch(^(id err){
XCTAssertEqualObjects(NSURLErrorDomain, [err domain]);
XCTAssertEqual([err code], NSURLErrorBadServerResponse);
[ex fulfill];
});
[self waitForExpectationsWithTimeout:1 handler:nil];
}
- (void)test3 {
id url = [NSURL URLWithString:@"http://example.com"];
id data = [NSData dataWithBytes:"abc" length:3];
MockSLRequest *request = (id) [MockSLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:@{@"1": @2}];
request.response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:200 HTTPVersion:@"1.1" headerFields:@{}];
request.data = data;
id ex = [self expectationWithDescription:@""];
[request promise].then(^(id rspdata){
XCTAssertEqualObjects(data, rspdata);
[ex fulfill];
});
[self waitForExpectationsWithTimeout:1 handler:nil];
}
- (void)test4 {
id url = [NSURL URLWithString:@"http://example.com"];
MockSLRequest *request = (id) [MockSLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:@{@"1": @2}];
request.response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:200 HTTPVersion:@"1.1" headerFields:@{}];
request.error = [NSError errorWithDomain:@"Cat" code:123 userInfo:nil];
id ex = [self expectationWithDescription:@""];
[request promise].catch(^(NSError *error){
XCTAssertEqual(error.code, 123);
XCTAssertEqualObjects(error.domain, @"Cat");
[ex fulfill];
});
[self waitForExpectationsWithTimeout:1 handler:nil];
}
@end
#ifndef TARGET_OS_MAC
#import <PromiseKit/NSError+Cancellation.h>
@import Social.SLComposeViewController;
@import Stubbilino;
#import "UIViewController+AnyPromise.h"
@implementation Test_SLComposeViewController_ObjC: XCTestCase
- (void)__test:(SLComposeViewControllerResult)dummy :(void (^)(AnyPromise *, id expectation))block {
id rootvc = [UIViewController new];
id ex = [self expectationWithDescription:@""];
SLComposeViewController *composevc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
id stub = [Stubbilino stubObject:rootvc];
[stub stubMethod:@selector(presentViewController:animated:completion:) withBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
composevc.completionHandler(dummy);
});
}];
block([rootvc promiseViewController:composevc animated:NO completion:nil], ex);
[self waitForExpectationsWithTimeout:1 handler:nil];
}
- (void)test1 {
NSInteger dummy = SLComposeViewControllerResultDone;
[self __test:dummy :^(AnyPromise *promise, id expectation) {
promise.then(^(id result){
XCTAssertEqual([result integerValue], dummy);
[expectation fulfill];
});
}];
}
- (void)test2 {
[self __test:SLComposeViewControllerResultCancelled :^(AnyPromise *promise, id expectation) {
promise.catchWithPolicy(PMKCatchPolicyAllErrors, ^(NSError *error){
XCTAssertTrue(error.cancelled);
[expectation fulfill];
});
}];
}
@end
#endif

View File

@@ -0,0 +1,56 @@
import PromiseKit
import PMKSocial
import Social
import XCTest
class SLRequestTests: XCTestCase {
func testSLRequest() {
// I tried to just override SLRequest, but Swift wouldn't let me
// then use the long initializer, and an exception is thrown inside
// init()
swizzle(SLRequest.self, #selector(SLRequest.perform(handler:))) {
let url = URL(string: "https://api.twitter.com/1.1/statuses/user_timeline.json")
let params = ["foo": "bar"]
let rq = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .GET, url: url, parameters: params)!
let ex = expectation(description: "")
rq.perform().done {
XCTAssertEqual($0.data, Data())
ex.fulfill()
}.catch {
XCTFail("\($0)")
}
waitForExpectations(timeout: 1, handler: nil)
}
}
}
extension SLRequest {
@objc private func pmk_performRequestWithHandler(_ handler: @escaping SLRequestHandler) {
after(seconds: 0).done { _ in
let rsp = HTTPURLResponse(url: URL(string: "http://example.com")!, statusCode: 200, httpVersion: "2.0", headerFields: [:])
handler(Data(), rsp, nil)
}
}
}
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)
}