static.js
10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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()
}