PromiseKit/Tests/JS-A+/JSAdapter.swift
Loïs Di Qual 65f020801e Add official Promises/A+ compliance test suite (#805)
* Library exports properly

* A bit of progress

* Try emitting mocha events

* Progress

* Progress is real

* All tests now show

* All tests run

* Proper formatting

* Proper XCTFail

* Some progress with adapters

* Not sure what's up

* Tests run with new adapter

* Add then function

* Good progress on interface

* Lots of tests now pass

* Validate onFulfilled / onRejected before calling them

* Cleanup test file

* Add todo

* Move MockNodeEnvironment to proper folder

* Use invokeMethod to call pure functions

* Fix timers

* Add some debugging

* Fix timeouts

* Catch errors when calling handlers

* Move JSPromise to own file

* Refactor JSPromise to use then/catch

* Add a bit of doc

* Catch exceptions thrown by handlers, the proper way

* Consolidate stacktrace printing

* Add availability checks

* Fully comply to 2.2.7.1

* Allow only running one test

* Add isFunction checks

* Enable 2.3.1 only for now

* Add ignored tests, handle promises as return value

* Report failures back to swift layer

* Move adapter to own file

* Use dev mode in webpack config

* More doc

* Refactor a bit

* More stuff in JSUtils

* More doc

* Add readme

* Remove some whitespace

* Add typeError func

* Use on:nil in second then

* Throw TypeError when needed

* Comment fix

* Make test suite iOS 8+ compatible

* Fix Swift 4 compile

* Fix Swift 3.1 compile

* Use babel to transpile to ES5, adds support to iOS 8/9

* Move JSUtils to proper folder

* Fix warning

* Simplify format printing

* Add 3.2 split support back

* Slight improvement

* Revert console.log format solution

* Use a reasonable timeout

* Add omittingEmptySubsequences support for 3.1
2018-03-07 09:13:00 -05:00

54 lines
1.6 KiB
Swift

//
// JSAdapter.swift
// PMKJSA+Tests
//
// Created by Lois Di Qual on 3/2/18.
//
import Foundation
import JavaScriptCore
import PromiseKit
enum JSAdapter {
static let resolved: @convention(block) (JSValue) -> JSPromise = { value in
return JSPromise(promise: .value(value))
}
static let rejected: @convention(block) (JSValue) -> JSPromise = { reason in
let error = JSUtils.JSError(reason: reason)
let promise = Promise<JSValue>(error: error)
return JSPromise(promise: promise)
}
static let deferred: @convention(block) () -> JSValue = {
let context = JSContext.current()
guard let object = JSValue(object: NSDictionary(), in: context) else {
fatalError("Couldn't create object")
}
let pendingPromise = Promise<JSValue>.pending()
let jsPromise = JSPromise(promise: pendingPromise.promise)
// promise
object.setObject(jsPromise, forKeyedSubscript: "promise" as NSString)
// resolve
let resolve: @convention(block) (JSValue) -> Void = { value in
pendingPromise.resolver.fulfill(value)
}
object.setObject(resolve, forKeyedSubscript: "resolve" as NSString)
// reject
let reject: @convention(block) (JSValue) -> Void = { reason in
let error = JSUtils.JSError(reason: reason)
pendingPromise.resolver.reject(error)
}
object.setObject(reject, forKeyedSubscript: "reject" as NSString)
return object
}
}