08-27-周三_17-09-29
This commit is contained in:
434
node_modules/kew/test/chain.js
generated
vendored
Normal file
434
node_modules/kew/test/chain.js
generated
vendored
Normal file
@@ -0,0 +1,434 @@
|
||||
var Q = require('../kew')
|
||||
var originalQ = require('q')
|
||||
|
||||
// test that fin() works with a synchronous resolve
|
||||
exports.testSynchronousThenAndFin = function (test) {
|
||||
var vals = ['a', 'b']
|
||||
var counter = 0
|
||||
|
||||
var promise1 = Q.resolve(vals[0])
|
||||
var promise2 = promise1.fin(function () {
|
||||
counter++
|
||||
})
|
||||
var promise3 = promise2.then(function (data) {
|
||||
if (data === vals[0]) return vals[1]
|
||||
})
|
||||
var promise4 = promise3.fin(function () {
|
||||
counter++
|
||||
})
|
||||
|
||||
Q.all([promise2, promise4])
|
||||
.then(function (data) {
|
||||
test.equal(counter, 2, "fin() should have been called twice")
|
||||
test.equal(data[0], vals[0], "first fin() should return the first val")
|
||||
test.equal(data[1], vals[1], "second fin() should return the second val")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test that fin() works with a synchronous reject
|
||||
exports.testSynchronousFailAndFin = function (test) {
|
||||
var errs = []
|
||||
errs.push(new Error('nope 1'))
|
||||
errs.push(new Error('nope 2'))
|
||||
var counter = 0
|
||||
|
||||
var promise1 = Q.reject(errs[0])
|
||||
var promise2 = promise1.fin(function () {
|
||||
counter++
|
||||
})
|
||||
var promise3 = promise2.fail(function (e) {
|
||||
if (e === errs[0]) throw errs[1]
|
||||
})
|
||||
var promise4 = promise3.fin(function () {
|
||||
counter++
|
||||
})
|
||||
|
||||
Q.all([
|
||||
promise2.fail(function (e) {
|
||||
return e === errs[0]
|
||||
}),
|
||||
promise4.fail(function (e) {
|
||||
return e === errs[1]
|
||||
})
|
||||
])
|
||||
.then(function (data) {
|
||||
test.equal(counter, 2, "fin() should have been called twice")
|
||||
test.equal(data[0] && data[1], true, "all promises should return true")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test that fin() works with an asynchrnous resolve
|
||||
exports.testAsynchronousThenAndFin = function (test) {
|
||||
var vals = ['a', 'b']
|
||||
var counter = 0
|
||||
|
||||
var defer = Q.defer()
|
||||
setTimeout(function () {
|
||||
defer.resolve(vals[0])
|
||||
})
|
||||
var promise1 = defer.promise
|
||||
var promise2 = promise1.fin(function () {
|
||||
counter++
|
||||
})
|
||||
var promise3 = promise2.then(function (data) {
|
||||
if (data !== vals[0]) return
|
||||
|
||||
var defer = Q.defer()
|
||||
setTimeout(function () {
|
||||
defer.resolve(vals[1])
|
||||
})
|
||||
return defer.promise
|
||||
})
|
||||
var promise4 = promise3.fin(function () {
|
||||
counter++
|
||||
})
|
||||
|
||||
Q.all([promise2, promise4])
|
||||
.then(function (data) {
|
||||
test.equal(counter, 2, "fin() should have been called twice")
|
||||
test.equal(data[0], vals[0], "first fin() should return the first val")
|
||||
test.equal(data[1], vals[1], "second fin() should return the second val")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test that fin() works with an asynchronous reject
|
||||
exports.testAsynchronousFailAndFin = function (test) {
|
||||
var errs = []
|
||||
errs.push(new Error('nope 1'))
|
||||
errs.push(new Error('nope 2'))
|
||||
var counter = 0
|
||||
|
||||
var defer = Q.defer()
|
||||
setTimeout(function () {
|
||||
defer.reject(errs[0])
|
||||
}, 10)
|
||||
var promise1 = defer.promise
|
||||
var promise2 = promise1.fin(function () {
|
||||
counter++
|
||||
})
|
||||
var promise3 = promise2.fail(function (e) {
|
||||
if (e !== errs[0]) return
|
||||
|
||||
var defer = Q.defer()
|
||||
setTimeout(function () {
|
||||
defer.reject(errs[1])
|
||||
}, 10)
|
||||
|
||||
return defer.promise
|
||||
})
|
||||
var promise4 = promise3.fin(function () {
|
||||
counter++
|
||||
})
|
||||
|
||||
Q.all([
|
||||
promise2.fail(function (e) {
|
||||
return e === errs[0]
|
||||
}),
|
||||
promise4.fail(function (e) {
|
||||
return e === errs[1]
|
||||
})
|
||||
])
|
||||
.then(function (data) {
|
||||
test.equal(counter, 2, "fin() should have been called twice")
|
||||
test.equal(data[0] && data[1], true, "all promises should return true")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test several thens chaining
|
||||
exports.testChainedThens = function (test) {
|
||||
var promise1 = Q.resolve('a')
|
||||
var promise2 = promise1.then(function(data) {
|
||||
return data + 'b'
|
||||
})
|
||||
var promise3 = promise2.then(function (data) {
|
||||
return data + 'c'
|
||||
})
|
||||
// testing the same promise again to make sure they can run side by side
|
||||
var promise4 = promise2.then(function (data) {
|
||||
return data + 'c'
|
||||
})
|
||||
|
||||
Q.all([promise1, promise2, promise3, promise4])
|
||||
.then(function (data) {
|
||||
test.equal(data[0], 'a')
|
||||
test.equal(data[1], 'ab')
|
||||
test.equal(data[2], 'abc')
|
||||
test.equal(data[3], 'abc')
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test several fails chaining
|
||||
exports.testChainedFails = function (test) {
|
||||
var errs = []
|
||||
errs.push(new Error("first err"))
|
||||
errs.push(new Error("second err"))
|
||||
errs.push(new Error("third err"))
|
||||
|
||||
var promise1 = Q.reject(errs[0])
|
||||
var promise2 = promise1.fail(function (e) {
|
||||
if (e === errs[0]) throw errs[1]
|
||||
})
|
||||
var promise3 = promise2.fail(function (e) {
|
||||
if (e === errs[1]) throw errs[2]
|
||||
})
|
||||
var promise4 = promise2.fail(function (e) {
|
||||
if (e === errs[1]) throw errs[2]
|
||||
})
|
||||
|
||||
Q.all([
|
||||
promise1.fail(function (e) {
|
||||
return e === errs[0]
|
||||
}),
|
||||
promise2.fail(function (e) {
|
||||
return e === errs[1]
|
||||
}),
|
||||
promise3.fail(function (e) {
|
||||
return e === errs[2]
|
||||
}),
|
||||
promise4.fail(function (e) {
|
||||
return e === errs[2]
|
||||
})
|
||||
])
|
||||
.then(function (data) {
|
||||
test.equal(data[0] && data[1] && data[2] && data[3], true)
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test that we can call end without callbacks and not fail
|
||||
exports.testEndNoCallbacks = function (test) {
|
||||
Q.resolve(true).end()
|
||||
test.ok("Ended successfully")
|
||||
test.done()
|
||||
}
|
||||
|
||||
// test that we can call end with callbacks and fail
|
||||
exports.testEndNoCallbacksThrows = function (test) {
|
||||
var testError = new Error('Testing')
|
||||
try {
|
||||
Q.reject(testError).end()
|
||||
test.fail("Should throw an error")
|
||||
} catch (e) {
|
||||
test.equal(e, testError, "Should throw the correct error")
|
||||
}
|
||||
test.done()
|
||||
}
|
||||
|
||||
// test chaining when a promise returns a promise
|
||||
exports.testChainedPromises = function (test) {
|
||||
var err = new Error('nope')
|
||||
var val = 'ok'
|
||||
|
||||
var shouldFail = Q.reject(err)
|
||||
var shouldSucceed = Q.resolve(val)
|
||||
|
||||
Q.resolve("start")
|
||||
.then(function () {
|
||||
return shouldFail
|
||||
})
|
||||
.fail(function (e) {
|
||||
if (e === err) return shouldSucceed
|
||||
else throw e
|
||||
})
|
||||
.then(function (data) {
|
||||
test.equal(data, val, "val should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test .end() is called with no parent scope (causing an uncaught exception)
|
||||
exports.testChainedEndUncaught = function (test) {
|
||||
var uncaughtErrors = 0
|
||||
var errs = []
|
||||
errs.push(new Error('nope 1'))
|
||||
errs.push(new Error('nope 2'))
|
||||
errs.push(new Error('nope 3'))
|
||||
|
||||
var cb = function (e) {
|
||||
uncaughtErrors++
|
||||
if (e === errs[2]) {
|
||||
test.equal(uncaughtErrors, 3, "Errors should be uncaught")
|
||||
process.removeListener('uncaughtException', cb)
|
||||
test.done()
|
||||
}
|
||||
}
|
||||
process.on('uncaughtException', cb)
|
||||
|
||||
var defer = Q.defer()
|
||||
defer.promise.end()
|
||||
|
||||
var promise1 = defer.promise
|
||||
var promise2 = promise1.fail(function (e) {
|
||||
if (e === errs[0]) throw errs[1]
|
||||
})
|
||||
var promise3 = promise2.fail(function (e) {
|
||||
if (e === errs[1]) throw errs[2]
|
||||
})
|
||||
|
||||
promise1.end()
|
||||
promise2.end()
|
||||
promise3.end()
|
||||
|
||||
setTimeout(function () {
|
||||
defer.reject(errs[0])
|
||||
}, 10)
|
||||
}
|
||||
|
||||
// test .end() is called with a parent scope and is caught
|
||||
exports.testChainedCaught = function (test) {
|
||||
var err = new Error('nope')
|
||||
|
||||
try {
|
||||
Q.reject(err).end()
|
||||
} catch (e) {
|
||||
test.equal(e, err, "Error should be caught")
|
||||
test.done()
|
||||
}
|
||||
}
|
||||
|
||||
// test a mix of fails and thens
|
||||
exports.testChainedMixed = function (test) {
|
||||
var errs = []
|
||||
errs.push(new Error('nope 1'))
|
||||
errs.push(new Error('nope 2'))
|
||||
errs.push(new Error('nope 3'))
|
||||
|
||||
var vals = [3, 2, 1]
|
||||
|
||||
var promise1 = Q.reject(errs[0])
|
||||
var promise2 = promise1.fail(function (e) {
|
||||
if (e === errs[0]) return vals[0]
|
||||
})
|
||||
var promise3 = promise2.then(function (data) {
|
||||
if (data === vals[0]) throw errs[1]
|
||||
})
|
||||
var promise4 = promise3.fail(function (e) {
|
||||
if (e === errs[1]) return vals[1]
|
||||
})
|
||||
var promise5 = promise4.then(function (data) {
|
||||
if (data === vals[1]) throw errs[2]
|
||||
})
|
||||
var promise6 = promise5.fail(function (e) {
|
||||
if (e === errs[2]) return vals[2]
|
||||
})
|
||||
|
||||
Q.all([
|
||||
promise1.fail(function (e) {
|
||||
return e === errs[0]
|
||||
}),
|
||||
promise2.then(function (data) {
|
||||
return data === vals[0]
|
||||
}),
|
||||
promise3.fail(function (e) {
|
||||
return e === errs[1]
|
||||
}),
|
||||
promise4.then(function (data) {
|
||||
return data === vals[1]
|
||||
}),
|
||||
promise5.fail(function (e) {
|
||||
return e === errs[2]
|
||||
}),
|
||||
promise6.then(function (data) {
|
||||
return data === vals[2]
|
||||
})
|
||||
])
|
||||
.then(function (data) {
|
||||
test.equal(data[0] && data[1] && data[2] && data[3] && data[4] && data[5], true, "All values should return true")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testInteroperabilityWithOtherPromises = function(test) {
|
||||
var promise1 = Q.defer()
|
||||
promise1.then(function(value) {
|
||||
return originalQ(1 + value)
|
||||
}).then(function(result) {
|
||||
test.equal(result, 11)
|
||||
})
|
||||
|
||||
var promise2 = Q.defer(),
|
||||
errToThrow = new Error('error')
|
||||
promise2.then(function() {
|
||||
return originalQ.reject(errToThrow)
|
||||
}).fail(function(err) {
|
||||
test.equal(err, errToThrow)
|
||||
})
|
||||
|
||||
promise1.resolve(10)
|
||||
promise2.resolve()
|
||||
|
||||
Q.all([promise1, promise2]).then(function() {
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testAllSettled = function(test) {
|
||||
var promise1 = Q.resolve('woot')
|
||||
var promise2 = Q.reject(new Error('oops'))
|
||||
|
||||
Q.allSettled([promise1, promise2, 'just a string'])
|
||||
.then(function (data) {
|
||||
test.equals('fulfilled', data[0].state)
|
||||
test.equals('woot', data[0].value)
|
||||
test.equals('rejected', data[1].state)
|
||||
test.equals('oops', data[1].reason.message)
|
||||
test.equals('fulfilled', data[2].state)
|
||||
test.equals('just a string', data[2].value)
|
||||
})
|
||||
|
||||
Q.allSettled([])
|
||||
.then(function (data) {
|
||||
test.equals(0, data.length)
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testTimeout = function(test) {
|
||||
var promise = Q.delay(50).timeout(45, 'Timeout message')
|
||||
promise.then(function () {
|
||||
test.fail('The promise is supposed to be timeout')
|
||||
})
|
||||
.fail(function (e) {
|
||||
test.equals('Timeout message', e.message, 'The error message should be the one passed into timeout()')
|
||||
})
|
||||
.fin(test.done)
|
||||
}
|
||||
|
||||
exports.testNotTimeout = function(test) {
|
||||
var promise = Q.delay('expected data', 40).timeout(45, 'Timeout message')
|
||||
promise.then(function (data) {
|
||||
test.equals('expected data', data, 'The data should be the data from the original promise')
|
||||
})
|
||||
.fail(function (e) {
|
||||
test.fail('The promise is supposed to be resolved before the timeout')
|
||||
})
|
||||
.fin(test.done)
|
||||
}
|
||||
|
||||
exports.testNotTimeoutButReject = function(test) {
|
||||
var promise = Q.delay(40).then(function() {throw new Error('Reject message')}).timeout(45, 'Timeout message')
|
||||
promise.then(function (data) {
|
||||
test.fail('The promise is supposed to be rejected')
|
||||
})
|
||||
.fail(function (e) {
|
||||
test.equals('Reject message', e.message, 'The error message should be from the original promise')
|
||||
})
|
||||
.fin(test.done)
|
||||
}
|
||||
|
||||
exports.testDelay = function (test) {
|
||||
var timePassed = false
|
||||
setTimeout(function () {
|
||||
timePassed = true
|
||||
}, 10)
|
||||
Q.resolve('expected').delay(20).then(function (result) {
|
||||
test.equal('expected', result)
|
||||
test.ok(timePassed)
|
||||
test.done()
|
||||
})
|
||||
}
|
135
node_modules/kew/test/closure_test.js
generated
vendored
Normal file
135
node_modules/kew/test/closure_test.js
generated
vendored
Normal file
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* @fileoverview A sample file to test type-checking
|
||||
*/
|
||||
|
||||
var kew = require('../kew')
|
||||
var Promise = kew.Promise
|
||||
var all = kew.all
|
||||
var allSettled = kew.allSettled
|
||||
var fcall = kew.fcall
|
||||
var nfcall = kew.nfcall
|
||||
var bindPromise = kew.bindPromise
|
||||
|
||||
/**
|
||||
@param {Array} result
|
||||
*/
|
||||
var callback = function (result) {};
|
||||
|
||||
/**
|
||||
@param {Array} result
|
||||
@param {Array} context
|
||||
*/
|
||||
var callbackWithContext = function (result, context) {};
|
||||
|
||||
/**
|
||||
* @param {number} n
|
||||
* @param {*} result
|
||||
*/
|
||||
var callbackNeedsBind = function (n, result) {};
|
||||
|
||||
/**
|
||||
@param {Error} error
|
||||
*/
|
||||
var errorCallback = function (error) {};
|
||||
|
||||
/**
|
||||
@param {Error} error
|
||||
@param {Array} context
|
||||
*/
|
||||
var errorCallbackWithContext = function (error, context) {};
|
||||
|
||||
/** @return {kew.Promise.<string>} */
|
||||
var stringPromise = function () {
|
||||
return kew.resolve('string')
|
||||
}
|
||||
|
||||
var exampleThen = function () {
|
||||
var examplePromise = new Promise();
|
||||
examplePromise.then(callback);
|
||||
examplePromise.setContext([]);
|
||||
examplePromise.then(callbackWithContext);
|
||||
|
||||
examplePromise.then(null, errorCallback);
|
||||
examplePromise.then(null, errorCallbackWithContext);
|
||||
};
|
||||
|
||||
|
||||
var thenBound = function () {
|
||||
stringPromise().thenBound(callbackNeedsBind, null, 3).failBound(callbackNeedsBind, null, 3);
|
||||
};
|
||||
|
||||
var examplePromise = function () {
|
||||
var promise = new Promise(callback);
|
||||
promise = new Promise(callbackWithContext);
|
||||
promise = new Promise(null, errorCallback);
|
||||
promise = new Promise(null, errorCallbackWithContext);
|
||||
};
|
||||
|
||||
var exampleFail = function () {
|
||||
var promise = new Promise();
|
||||
promise.fail(errorCallback);
|
||||
promise.fail(errorCallbackWithContext);
|
||||
};
|
||||
|
||||
var exampleResolver = function () {
|
||||
var promise = new Promise();
|
||||
var resolver = promise.makeNodeResolver();
|
||||
// success
|
||||
resolver(null, {});
|
||||
// failure
|
||||
resolver(new Error(), null);
|
||||
};
|
||||
|
||||
var exampleAll = function () {
|
||||
// should not compile, but does
|
||||
all([5]);
|
||||
all([{}]);
|
||||
all([null]);
|
||||
all([new Promise(), {}]);
|
||||
all([new Promise(), null]);
|
||||
|
||||
// good
|
||||
var promise = all([]);
|
||||
all([new Promise(), new Promise()]);
|
||||
};
|
||||
|
||||
var exampleAllSettled = function () {
|
||||
allSettled([]);
|
||||
allSettled([5, {}, null, 'string']);
|
||||
var promise = allSettled([new Promise()]);
|
||||
promise.then(function(results){});
|
||||
};
|
||||
|
||||
var exampleTimeout = function () {
|
||||
var promise = new Promise();
|
||||
var timeoutPromise = promise.timeout(50);
|
||||
timeoutPromise.then(function(result){});
|
||||
};
|
||||
|
||||
var noArgsFunction = function () {};
|
||||
|
||||
var exampleFcall = function () {
|
||||
fcall(noArgsFunction);
|
||||
fcall(callback, []);
|
||||
fcall(callbackWithContext, [], 5);
|
||||
};
|
||||
|
||||
/** @param {function(Error, *)} nodeCallback */
|
||||
var noArgsWithNodeCallback = function (nodeCallback) {};
|
||||
|
||||
/**
|
||||
@param {!Array} argument
|
||||
@param {function(Error, *)} nodeCallback
|
||||
*/
|
||||
var oneArgWithNodeCallback = function (argument, nodeCallback) {};
|
||||
|
||||
var exampleNfcall = function () {
|
||||
var promise = nfcall(noArgsWithNodeCallback);
|
||||
promise = nfcall(oneArgWithNodeCallback, []);
|
||||
};
|
||||
|
||||
var exampleBindPromise = function () {
|
||||
callback = bindPromise(noArgsWithNodeCallback, null);
|
||||
callback = bindPromise(noArgsWithNodeCallback, {});
|
||||
callback = bindPromise(oneArgWithNodeCallback, null, []);
|
||||
};
|
89
node_modules/kew/test/context.js
generated
vendored
Normal file
89
node_modules/kew/test/context.js
generated
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
var Q = require('../kew')
|
||||
|
||||
// test that contexts are propogated based on position
|
||||
exports.testContextWithDelay = function (test) {
|
||||
|
||||
Q.resolve(true)
|
||||
.setContext({id: 1})
|
||||
.then(function (val, context) {
|
||||
test.equal(context.id, 1, 'Should return the first context')
|
||||
return Q.delay(500)
|
||||
})
|
||||
.setContext({id: 2})
|
||||
.then(function (val, context) {
|
||||
test.equal(context.id, 2, 'Should return the second context')
|
||||
return Q.delay(500)
|
||||
})
|
||||
.clearContext()
|
||||
.then(function (val, context) {
|
||||
test.equal(typeof context, 'undefined', 'Should return an undefined context')
|
||||
return Q.delay(500)
|
||||
})
|
||||
.setContext({id: 3})
|
||||
.fin(test.done)
|
||||
}
|
||||
|
||||
// test adding and removing contexts
|
||||
exports.testGeneralContextFlow = function (test) {
|
||||
Q.resolve(true)
|
||||
// test no context exists
|
||||
.then(function (val, context) {
|
||||
test.equal(typeof context, 'undefined', 'Context should be undefined')
|
||||
throw new Error()
|
||||
})
|
||||
.fail(function (e, context) {
|
||||
test.equal(typeof context, 'undefined', 'Context should be undefined')
|
||||
})
|
||||
|
||||
// set the context and mutate it
|
||||
.setContext({counter: 1})
|
||||
.then(function (val, context) {
|
||||
test.equal(context.counter, 1, 'Counter should be 1')
|
||||
context.counter++
|
||||
})
|
||||
.then(function (val, context) {
|
||||
test.equal(context.counter, 2, 'Counter should be 2')
|
||||
context.counter++
|
||||
throw new Error()
|
||||
})
|
||||
.fail(function (e, context) {
|
||||
test.equal(context.counter, 3, 'Counter should be 3')
|
||||
})
|
||||
|
||||
// return a context
|
||||
.then(function (val, context) {
|
||||
return Q.resolve(false)
|
||||
.setContext({counter: 0})
|
||||
})
|
||||
.then(function (val, context) {
|
||||
test.equal(context.counter, 0, 'Counter should be 0')
|
||||
throw new Error()
|
||||
})
|
||||
.fail(function (e, context) {
|
||||
test.equal(context.counter, 0, 'Counter should be 0')
|
||||
})
|
||||
|
||||
// returning a promise with a cleared context won't clear the parent context
|
||||
.then(function (val, context) {
|
||||
return Q.resolve(false).clearContext()
|
||||
})
|
||||
.then(function (val, context) {
|
||||
test.equal(context.counter, 0, 'Counter should be 0')
|
||||
throw new Error()
|
||||
})
|
||||
.fail(function (e, context) {
|
||||
test.equal(context.counter, 0, 'Counter should be 0')
|
||||
})
|
||||
|
||||
// test that clearing the context works
|
||||
.clearContext()
|
||||
.then(function (val, context) {
|
||||
test.equal(typeof context, 'undefined', 'Context should be undefined')
|
||||
throw new Error()
|
||||
})
|
||||
.fail(function (e, context) {
|
||||
test.equal(typeof context, 'undefined', 'Context should be undefined')
|
||||
})
|
||||
|
||||
.fin(test.done)
|
||||
}
|
120
node_modules/kew/test/defer.js
generated
vendored
Normal file
120
node_modules/kew/test/defer.js
generated
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
var Q = require('../kew')
|
||||
|
||||
// create a deferred which returns a promise
|
||||
exports.testDeferredResolve = function (test) {
|
||||
var val = "ok"
|
||||
var defer = Q.defer()
|
||||
|
||||
defer.promise
|
||||
.then(function (data) {
|
||||
test.equal(data, val, "Promise successfully returned")
|
||||
test.done()
|
||||
})
|
||||
|
||||
setTimeout(function () {
|
||||
defer.resolve(val)
|
||||
}, 50)
|
||||
}
|
||||
|
||||
// make sure a deferred can only resolve once
|
||||
exports.testDeferredResolveOnce = function (test) {
|
||||
var defer = Q.defer()
|
||||
|
||||
try {
|
||||
defer.resolve(true)
|
||||
defer.resolve(true)
|
||||
test.fail("Unable to resolve the same deferred twice")
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
test.done()
|
||||
}
|
||||
|
||||
// create a deferred which returns a failed promise
|
||||
exports.testDeferredReject = function (test) {
|
||||
var err = new Error("hello")
|
||||
var defer = Q.defer()
|
||||
|
||||
defer.promise
|
||||
.fail(function (e) {
|
||||
test.equal(e, err, "Promise successfully failed")
|
||||
test.done()
|
||||
})
|
||||
|
||||
setTimeout(function () {
|
||||
defer.reject(err)
|
||||
}, 50)
|
||||
}
|
||||
|
||||
// make sure a deferred can only reject once
|
||||
exports.testDeferredRejectOnce = function (test) {
|
||||
var defer = Q.defer()
|
||||
|
||||
try {
|
||||
defer.reject(new Error("nope 1"))
|
||||
defer.reject(new Error("nope 2"))
|
||||
test.fail("Unable to reject the same deferred twice")
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
test.done()
|
||||
}
|
||||
|
||||
// make sure a deferred can only reject once
|
||||
exports.testDeferAndRejectFail = function (test) {
|
||||
var defer
|
||||
|
||||
try {
|
||||
defer = Q.defer()
|
||||
defer.reject(new Error("nope 1"))
|
||||
defer.resolve(true)
|
||||
test.fail("Unable to reject and resolve the same deferred")
|
||||
} catch (e) {
|
||||
test.ok(true, "Unable to reject and resolve same deferred")
|
||||
}
|
||||
|
||||
try {
|
||||
defer = Q.defer()
|
||||
defer.resolve(true)
|
||||
defer.reject(new Error("nope 1"))
|
||||
test.fail("Unable to reject and resolve the same deferred")
|
||||
} catch (e) {
|
||||
test.ok(true, "Unable to reject and resolve same deferred")
|
||||
}
|
||||
|
||||
test.done()
|
||||
}
|
||||
|
||||
// create a deferred which resolves with a node-standard callback
|
||||
exports.testDeferredResolverSuccess = function (test) {
|
||||
var val = "ok"
|
||||
var defer = Q.defer()
|
||||
var callback = defer.makeNodeResolver()
|
||||
|
||||
defer.promise
|
||||
.then(function (data) {
|
||||
test.equal(data, val, "Promise successfully returned")
|
||||
test.done()
|
||||
})
|
||||
|
||||
setTimeout(function () {
|
||||
callback(null, val)
|
||||
}, 50)
|
||||
}
|
||||
|
||||
// create a deferred which rejects with a node-standard callback
|
||||
exports.testDeferredResolverSuccess = function (test) {
|
||||
var err = new Error("hello")
|
||||
var defer = Q.defer()
|
||||
var callback = defer.makeNodeResolver()
|
||||
|
||||
defer.promise
|
||||
.fail(function (e) {
|
||||
test.equal(e, err, "Promise successfully failed")
|
||||
test.done()
|
||||
})
|
||||
|
||||
setTimeout(function () {
|
||||
callback(err)
|
||||
}, 50)
|
||||
}
|
9
node_modules/kew/test/externs_node.js
generated
vendored
Normal file
9
node_modules/kew/test/externs_node.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
/* Node externs for Closure Compiler (just enough for kew.js). */
|
||||
|
||||
/** @const */
|
||||
var module = {};
|
||||
|
||||
/** @const */
|
||||
var process = {};
|
||||
/** @param {function()} callback */
|
||||
process.nextTick = function (callback) {};
|
45
node_modules/kew/test/later.js
generated
vendored
Normal file
45
node_modules/kew/test/later.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
var Q = require('../kew')
|
||||
|
||||
function synchronous (callback) {
|
||||
callback()
|
||||
}
|
||||
|
||||
var asynchronous = Q.getNextTickFunction()
|
||||
|
||||
exports.testAsynchronousSynchronous = function (test) {
|
||||
Q.setNextTickFunction(synchronous)
|
||||
|
||||
var number = 5
|
||||
|
||||
Q.resolve(true).then(function () {
|
||||
number = 6
|
||||
})
|
||||
test.equals(number, 6, 'Q should resolve synchronously')
|
||||
|
||||
Q.setNextTickFunction(asynchronous)
|
||||
|
||||
Q.resolve(true).then(function () {
|
||||
number = 7
|
||||
})
|
||||
test.equals(number, 6, 'Q should resolve asynchronously')
|
||||
test.done()
|
||||
}
|
||||
|
||||
exports.testSetImmediate = function (test) {
|
||||
if (typeof setImmediate == 'undefined') {
|
||||
test.done()
|
||||
return
|
||||
}
|
||||
|
||||
Q.setNextTickFunction(setImmediate)
|
||||
|
||||
var number = 5
|
||||
Q.resolve(true).then(function () {
|
||||
number = 6
|
||||
})
|
||||
test.equals(number, 5, 'Q should resolve asynchronously')
|
||||
setImmediate(function () {
|
||||
test.equals(number, 6, 'Q should schedule _successFn synchronously')
|
||||
test.done()
|
||||
})
|
||||
}
|
59
node_modules/kew/test/scopes.js
generated
vendored
Normal file
59
node_modules/kew/test/scopes.js
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
var Q = require('../kew')
|
||||
|
||||
exports.testThen = function (test) {
|
||||
var detectedScope = null
|
||||
Q.resolve(true).then(function () {
|
||||
detectedScope = this
|
||||
}).then(function () {
|
||||
test.ok(Q.isPromise(detectedScope), 'then() should be called in context of promise')
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testFail = function (test) {
|
||||
var detectedScope = null
|
||||
Q.reject(new Error()).fail(function () {
|
||||
detectedScope = this
|
||||
}).then(function () {
|
||||
test.ok(Q.isPromise(detectedScope), 'fail() should be called in context of promise')
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testThenBound = function (test) {
|
||||
var detectedScope = scope
|
||||
var scope = {}
|
||||
Q.resolve(true).thenBound(function () {
|
||||
detectedScope = scope
|
||||
}, scope).then(function () {
|
||||
test.ok(detectedScope === scope, 'thenScoped() should be called in context of scope')
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testFailBound = function (test) {
|
||||
var detectedScope = scope
|
||||
var scope = {}
|
||||
Q.reject(new Error()).failBound(function () {
|
||||
detectedScope = scope
|
||||
}, scope).then(function () {
|
||||
test.equal(detectedScope, scope, 'failBound() should be called in context of scope')
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testThenBoundWithArgs = function (test) {
|
||||
var detectedScope = scope
|
||||
var scope = {}
|
||||
Q.resolve(-1).thenBound(function (a, b, c, d) {
|
||||
test.equal(a, 1)
|
||||
test.equal(b, 2)
|
||||
test.equal(c, 3)
|
||||
test.equal(d, -1)
|
||||
detectedScope = scope
|
||||
}, scope, 1, 2, 3).then(function () {
|
||||
test.ok(detectedScope === scope, 'failScoped() should be called in context of scope')
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
59
node_modules/kew/test/spread.js
generated
vendored
Normal file
59
node_modules/kew/test/spread.js
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
var Q = require('../kew')
|
||||
|
||||
exports.testSpreadStatic = function (test) {
|
||||
Q.spread([Q.resolve('a'), 'b'], function (a, b) {
|
||||
test.equal('a', a)
|
||||
test.equal('b', b)
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testSpreadMethod = function (test) {
|
||||
Q.resolve(true)
|
||||
.then(function () {
|
||||
return ['a', 'b']
|
||||
})
|
||||
.spread(function (a, b) {
|
||||
test.equal('a', a)
|
||||
test.equal('b', b)
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testSpreadBoundMethod = function (test) {
|
||||
Q.resolve(true)
|
||||
.then(function () {
|
||||
return [Q.resolve('a'), 'b']
|
||||
})
|
||||
.spreadBound(function (c, a, b) {
|
||||
test.equal('scope', this.scope)
|
||||
test.equal('c', c)
|
||||
test.equal('a', a)
|
||||
test.equal('b', b)
|
||||
test.done()
|
||||
}, {scope: 'scope'}, 'c')
|
||||
}
|
||||
|
||||
exports.testAllSynchronization1 = function (test) {
|
||||
var order = []
|
||||
Q.resolve(true)
|
||||
.then(function () {
|
||||
var promiseA = Q.fcall(function () {
|
||||
order.push('a')
|
||||
})
|
||||
var promiseB = Q.fcall(function () {
|
||||
order.push('b')
|
||||
})
|
||||
|
||||
test.deepEqual([], order)
|
||||
|
||||
var promiseAB = Q.all([promiseA, promiseB])
|
||||
test.deepEqual([], order)
|
||||
|
||||
return [promiseA, promiseB]
|
||||
})
|
||||
.then(function (results) {
|
||||
test.deepEqual(['a', 'b'], order)
|
||||
test.done()
|
||||
})
|
||||
}
|
393
node_modules/kew/test/static.js
generated
vendored
Normal file
393
node_modules/kew/test/static.js
generated
vendored
Normal file
@@ -0,0 +1,393 @@
|
||||
var Q = require('../kew')
|
||||
var originalQ = require('q')
|
||||
|
||||
// create a promise from a literal
|
||||
exports.testQResolve = function (test) {
|
||||
var val = "ok"
|
||||
|
||||
Q.resolve(val)
|
||||
.then(function (data) {
|
||||
test.equal(data, val, "Promise successfully returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// create a failed promise from an error literal
|
||||
exports.testQReject = function (test) {
|
||||
var err = new Error("hello")
|
||||
|
||||
Q.reject(err)
|
||||
.fail(function (e) {
|
||||
test.equal(e, err, "Promise successfully failed")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// Test Q.stats
|
||||
exports.testQStatistics = function (test) {
|
||||
var err = new Error("hello")
|
||||
|
||||
var errorsEmitted = Q.stats.errorsEmitted
|
||||
var errorsHandled = Q.stats.errorsHandled
|
||||
|
||||
var rejected = Q.reject(err)
|
||||
test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted")
|
||||
test.equal(errorsHandled, Q.stats.errorsHandled, "Error hasn't been handled yet")
|
||||
|
||||
rejected.fail(function (e) {
|
||||
test.equal(e, err, "Promise successfully failed")
|
||||
test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted")
|
||||
test.equal(errorsHandled + 1, Q.stats.errorsHandled, "One additional error handled")
|
||||
})
|
||||
|
||||
rejected.fail(function (e) {
|
||||
test.equal(e, err, "Promise successfully failed")
|
||||
test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted")
|
||||
test.equal(errorsHandled + 1, Q.stats.errorsHandled, "Only count error handling once")
|
||||
})
|
||||
test.done()
|
||||
}
|
||||
|
||||
exports.testQDeferredStatistics = function (test) {
|
||||
var err = new Error("hello")
|
||||
|
||||
var errorsEmitted = Q.stats.errorsEmitted
|
||||
var errorsHandled = Q.stats.errorsHandled
|
||||
|
||||
var deferred = Q.defer()
|
||||
|
||||
deferred.fail(function (e) {
|
||||
test.equal(e, err, "Promise successfully failed")
|
||||
test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted")
|
||||
test.equal(errorsHandled + 1, Q.stats.errorsHandled, "One additional error handled")
|
||||
test.done()
|
||||
})
|
||||
|
||||
var rejected = deferred.reject(err)
|
||||
|
||||
}
|
||||
|
||||
// test Q.all with an empty array
|
||||
exports.testQEmptySuccess = function (test) {
|
||||
var promises = []
|
||||
|
||||
// make sure all results come back
|
||||
Q.all(promises)
|
||||
.then(function (data) {
|
||||
test.equal(data.length, 0, "No records should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test Q.all with only literals
|
||||
exports.testQAllLiteralsSuccess = function (test) {
|
||||
var vals = [3, 2, 1]
|
||||
var promises = []
|
||||
|
||||
promises.push(vals[0])
|
||||
promises.push(vals[1])
|
||||
promises.push(vals[2])
|
||||
|
||||
// make sure all results come back
|
||||
Q.all(promises)
|
||||
.then(function (data) {
|
||||
test.equal(data[0], vals[0], "First val should be returned")
|
||||
test.equal(data[1], vals[1], "Second val should be returned")
|
||||
test.equal(data[2], vals[2], "Third val should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test Q.all with only promises
|
||||
exports.testQAllPromisesSuccess = function (test) {
|
||||
var vals = [3, 2, 1]
|
||||
var promises = []
|
||||
|
||||
promises.push(Q.resolve(vals[0]))
|
||||
promises.push(Q.resolve(vals[1]))
|
||||
promises.push(Q.resolve(vals[2]))
|
||||
|
||||
// make sure all results come back
|
||||
Q.all(promises)
|
||||
.then(function (data) {
|
||||
test.equal(data[0], vals[0], "First val should be returned")
|
||||
test.equal(data[1], vals[1], "Second val should be returned")
|
||||
test.equal(data[2], vals[2], "Third val should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// create a promise which waits for other promises
|
||||
exports.testQAllAssortedSuccess = function (test) {
|
||||
var vals = [3, 2, 1]
|
||||
var promises = []
|
||||
|
||||
// a promise that returns the value immediately
|
||||
promises.push(Q.resolve(vals[0]))
|
||||
|
||||
// the value itself
|
||||
promises.push(vals[1])
|
||||
|
||||
// a promise which returns in 10ms
|
||||
var defer = Q.defer()
|
||||
promises.push(defer.promise)
|
||||
setTimeout(function () {
|
||||
defer.resolve(vals[2])
|
||||
}, 10)
|
||||
|
||||
// make sure all results come back
|
||||
Q.all(promises)
|
||||
.then(function (data) {
|
||||
test.equal(data[0], vals[0], "First val should be returned")
|
||||
test.equal(data[1], vals[1], "Second val should be returned")
|
||||
test.equal(data[2], vals[2], "Third val should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test Q.all with a failing promise
|
||||
exports.testQAllError = function (test) {
|
||||
var vals = [3, 2, 1]
|
||||
var err = new Error("hello")
|
||||
var promises = []
|
||||
|
||||
promises.push(vals[0])
|
||||
promises.push(vals[1])
|
||||
|
||||
var defer = Q.defer()
|
||||
promises.push(defer.promise)
|
||||
defer.reject(err)
|
||||
|
||||
// make sure all results come back
|
||||
Q.all(promises)
|
||||
.fail(function (e) {
|
||||
test.equal(e, err)
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test all var_args
|
||||
exports.testAllVarArgs = function (test) {
|
||||
var promises = ['a', 'b']
|
||||
|
||||
Q.all.apply(Q, promises)
|
||||
.then(function (results) {
|
||||
test.equal(promises[0], results[0], "First element should be returned")
|
||||
test.equal(promises[1], results[1], "Second element should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test all array
|
||||
exports.testAllArray = function (test) {
|
||||
var promises = ['a', 'b']
|
||||
|
||||
Q.all(promises)
|
||||
.then(function (results) {
|
||||
test.equal(promises[0], results[0], "First element should be returned")
|
||||
test.equal(promises[1], results[1], "Second element should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testAllIsPromiseLike = function(test) {
|
||||
var promises = ['a', originalQ('b')]
|
||||
|
||||
Q.all(promises)
|
||||
.then(function (results) {
|
||||
test.equal(promises[0], 'a', "First element should be returned")
|
||||
test.equal(promises[1], 'b', "Second element should be returned")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test delay
|
||||
exports.testDelay = function (test) {
|
||||
var val = "Hello, there"
|
||||
var startTime = Date.now()
|
||||
|
||||
Q.resolve(val)
|
||||
.then(function (v) {
|
||||
return Q.delay(v, 1000)
|
||||
})
|
||||
.then(function (returnVal) {
|
||||
test.equal(returnVal, val, "Val should be passed through")
|
||||
|
||||
var diff = Date.now() - startTime
|
||||
|
||||
// clock granularity may be off by 15
|
||||
test.equal(diff >= 1000 - 15, true, "Should have waited a second. Actually waited " + diff)
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test fcall
|
||||
exports.testFcall = function (test) {
|
||||
var calledYet = false
|
||||
var adder = function (a, b) {
|
||||
calledYet = true
|
||||
return a + b
|
||||
}
|
||||
|
||||
Q.fcall(adder, 2, 3)
|
||||
.then(function (val) {
|
||||
test.equal(val, 5, "Val should be 2 + 3")
|
||||
test.done()
|
||||
})
|
||||
test.ok(!calledYet, "fcall() should delay function invocation until next tick")
|
||||
}
|
||||
|
||||
// test fcall
|
||||
exports.testFcallError = function (test) {
|
||||
var error = function () {
|
||||
throw new Error('my error')
|
||||
}
|
||||
|
||||
Q.fcall(error)
|
||||
.then(function (val) {
|
||||
test.fail('fcall should throw exception')
|
||||
}, function (err) {
|
||||
test.equal('my error', err.message)
|
||||
})
|
||||
.then(function () {
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test fcall works when fn returns a promise
|
||||
exports.testFcallGivenPromise = function (test) {
|
||||
var calledYet = false
|
||||
var eventualAdd = function (a, b) {
|
||||
calledYet = true
|
||||
return Q.resolve(a + b)
|
||||
}
|
||||
|
||||
Q.fcall(eventualAdd, 2, 3)
|
||||
.then(function (val) {
|
||||
test.equal(val, 5, "Val should be 2 + 3")
|
||||
test.done()
|
||||
})
|
||||
test.ok(!calledYet, "fcall() should delay function invocation until next tick")
|
||||
}
|
||||
|
||||
// test nfcall, successful case
|
||||
exports.testNfcall = function (test) {
|
||||
var nodeStyleEventualAdder = function (a, b, callback) {
|
||||
setTimeout(function () {
|
||||
callback(undefined, a + b)
|
||||
}, 2)
|
||||
}
|
||||
|
||||
Q.nfcall(nodeStyleEventualAdder, 2, 3)
|
||||
.then(function (val) {
|
||||
test.equal(val, 5, "Val should be 2 + 3")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test nfcall, error case
|
||||
exports.testNfcallErrors = function (test) {
|
||||
var err = new Error('fail')
|
||||
|
||||
var nodeStyleFailer = function (a, b, callback) {
|
||||
setTimeout(function() {
|
||||
callback(err)
|
||||
}, 2)
|
||||
}
|
||||
|
||||
Q.nfcall(nodeStyleFailer, 2, 3)
|
||||
.fail(function (e) {
|
||||
test.equal(e, err, "Promise successfully failed")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test fcall
|
||||
exports.testNFcallErrorSync = function (test) {
|
||||
var error = function () {
|
||||
throw new Error('my error')
|
||||
}
|
||||
|
||||
Q.nfcall(error)
|
||||
.then(function (val) {
|
||||
test.fail('nfcall should throw exception')
|
||||
}, function (err) {
|
||||
test.equal('my error', err.message)
|
||||
})
|
||||
.then(function () {
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
exports.testNcall = function (test) {
|
||||
function TwoAdder() {
|
||||
this.a = 2
|
||||
}
|
||||
TwoAdder.prototype.add = function (num, callback) {
|
||||
setTimeout(function () {
|
||||
callback(null, this.a + num)
|
||||
}.bind(this), 10)
|
||||
}
|
||||
var adder = new TwoAdder()
|
||||
Q.ncall(adder.add, adder, 3)
|
||||
.then(function (val) {
|
||||
test.equal(val, 5, "Val should be 2 + 3")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test binding a callback function with a promise
|
||||
exports.testBindPromise = function (test) {
|
||||
var adder = function (a, b, callback) {
|
||||
callback(null, a + b)
|
||||
}
|
||||
|
||||
var boundAdder = Q.bindPromise(adder, null, 2)
|
||||
boundAdder(3)
|
||||
.then(function (val) {
|
||||
test.equal(val, 5, "Val should be 2 + 3")
|
||||
test.done()
|
||||
})
|
||||
}
|
||||
|
||||
// test checking whether something is a promise
|
||||
exports.testIsPromise = function (test) {
|
||||
var kewPromise = Q.defer()
|
||||
var qPromise = originalQ(10)
|
||||
var kewLikeObject = {
|
||||
promise: function () {
|
||||
return 'not a promise sucka!'
|
||||
},
|
||||
then: function (fn) {
|
||||
fn('like a promise, brah!')
|
||||
}
|
||||
}
|
||||
test.equal(Q.isPromise(kewPromise), true, 'A Kew promise is a promise')
|
||||
test.equal(Q.isPromise(qPromise), false, 'A Q promise is not a promise')
|
||||
test.equal(Q.isPromise(kewLikeObject), false, 'A pretend promise is not a promise')
|
||||
test.done()
|
||||
}
|
||||
|
||||
// test checking whether something is a promise-like object
|
||||
exports.testIsPromiseLike = function (test) {
|
||||
var kewPromise = Q.defer()
|
||||
var qPromise = originalQ(10)
|
||||
var kewLikeObject = {
|
||||
promise: function () {
|
||||
return 'not a promise sucka!'
|
||||
},
|
||||
then: function (fn) {
|
||||
fn('like a promise, brah!')
|
||||
}
|
||||
}
|
||||
var kewLikeFunction = function() {}
|
||||
kewLikeFunction.then = function(fn) {
|
||||
fn('like a promise, brah!')
|
||||
}
|
||||
test.equal(Q.isPromiseLike(kewPromise), true, 'A Kew promise is promise-like')
|
||||
test.equal(Q.isPromiseLike(qPromise), true, 'A Q promise is promise-like')
|
||||
test.equal(Q.isPromiseLike(kewLikeObject), true, 'A pretend promise is a promise-like')
|
||||
test.equal(Q.isPromiseLike(kewLikeFunction), true, 'A pretend function promise is a promise-like')
|
||||
|
||||
test.done()
|
||||
}
|
Reference in New Issue
Block a user