-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
453 lines (408 loc) · 10.7 KB
/
main.js
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
if (typeof require !== "undefined") {
var Y = require("../yyield");
var $ = require("jquery-deferred");
var _ = require("lodash");
}
// Setup logging
Y.log = function() {
if (arguments.length === 0) {
console.log();
}
else if (arguments.length == 1) {
console.log(arguments[0]);
}
else if (arguments.length == 2) {
console.log(arguments[0], arguments[1]);
}
else if (arguments.length == 3) {
console.log(arguments[0], arguments[1], arguments[2]);
}
else if (arguments.length == 4) {
console.log(arguments[0], arguments[1], arguments[2], arguments[3]);
}
else {
console.log("MULTIPLE");
}
};
// Utility function used in testing
var runCount = 0;
function* sleep(timeout) {
return function(cb) {
runCount++;
if (timeout <= 0) {
cb(null, timeout);
return;
}
setTimeout(function() {
cb(null, timeout);
}, timeout);
}
}
function asyncSleep(timeout, cb) {
setTimeout(function() { cb(); } , timeout)
}
function thrower(msg, cb) {
cb(new Error(msg));
}
var oldAsyncTest = asyncTest;
asyncTest = function genAwareAsyncTest(name, testFunc) {
if (!Y.isGeneratorFunction(testFunc)) {
oldAsyncTest.apply(this, arguments);
return;
}
oldAsyncTest(name, function() {
(function*() {
yield testFunc();
start();
}).run();
})
};
var oldThrows = throws;
throws = function genAwareThrows(throwingFunc, descOrTypeOrMessage, message) {
if (!Y.isGeneratorFunction(throwingFunc)) {
oldThrows.apply(this, arguments);
return;
}
oldThrows(function() {
(function*() {
yield throwingFunc();
start();
}).run();
}, descOrTypeOrMessage, message);
}
// TEST
// - empty return in generator
// - errors in all scenarios
asyncTest("sync 0 second sleep via generators", function*() {
yield sleep(0);
ok(true, "Completed synchronously")
});
asyncTest("Yielding on same generator object twice", function*() {
runCount = 0;
var timeout = 1;
var a = sleep(timeout);
var b = yield a;
var c = yield a;
equal(b, c, "result 1");
equal(c, timeout , "result 2");
equal(runCount, 1, "Just one call");
});
asyncTest("Yielding twice concurrently", function*() {
runCount = 0;
var timeout = 1;
var a = sleep(timeout);
var res = yield [a,a];
equal(res[0], timeout, "res[0]");
equal(res[1], timeout, "res[1]");
equal(runCount, 1, "Just one call");
});
asyncTest("Empty return should be ok", function*() {
yield (function*() {
yield a = sleep(1);
return;
})();
ok(true, "Empty return worked fine");
});
asyncTest("jQuery deferred", function*() {
var returnValue = "returned";
var ret = yield $.Deferred(function(deferred) {
setTimeout(function() {
deferred.resolve(returnValue)
}, 1);
});
equal(ret, returnValue, "Right return value from jQuery");
});
asyncTest("Undefined variable use should result in ReferenceError", function*() {
try {
var ret = yield (function*() { NOT_EXISTING(); })()
ok(false, "No error thrown");
}
catch(e) {
console.log("We caught it", e.stack);
ok(true, "Exception thrown");
ok(e instanceof ReferenceError, "e instanceof ReferenceError");
start();
}
});
asyncTest("Make sure we get ReferenceError in callback to generator", function*() {
(function*() {
var ret = yield (function*() { NOT_EXISTING(); })()
ok(false, "Function did not throw");
}).run(function(e, results) {
ok(e instanceof ReferenceError, "e instanceof ReferenceError");
start();
});
});
asyncTest("Multiple results from async function", function*(){
var a = yield function(cb) {
cb(null, 1, 2);
};
ok(a instanceof Array, "a instanceof Array");
equal(a.length, 2, "length === 2")
equal(a[0], 1);
equal(a[1], 2);
});
asyncTest("Multiple results from deferred", function*(){
var a = yield $.Deferred(function(d) {
d.resolve(1, 2);
});
ok(a instanceof Array, "a instanceof Array");
equal(a.length, 2, "length === 2")
equal(a[0], 1);
equal(a[1], 2);
});
asyncTest("Undefined variable use should result in ReferenceError in Deferred", function*() {
try {
yield $.Deferred(function(d) { d.reject(new Error()); });
ok(false, "Exception not thrown");
}
catch(e) {
ok(true, "Exception thrown");
ok(e instanceof Error, "e instanceof Error");
start();
}
});
asyncTest("Parallel wait for errors should result in error array", function*() {
try {
Y.parallelErrorsDefault = Y.PARALLEL_ERRORS_WAIT;
var op = (function*() { throw new Error(); })();
yield [op, op];
ok(false, "Exception not thrown");
}
catch(e) {
ok(true, "Exception thrown");
ok(e instanceof Error, "e instanceof Error");
ok(e instanceof Y.AggregateError, "e instanceof AggregateError");
equal(e.errors.length, 2, "length === 2");
start();
}
});
asyncTest("Parallel errors without wait should result in orphan callback being called and one error", function*() {
var defs = [$.Deferred(), $.Deferred()];
$.when(defs).then(function() {
start();
});
try {
Y.parallelErrorsDefault = Y.PARALLEL_ERRORS_THROW;
Y.onOrphanCompletion = function() {
ok(true, "orphanCompletion called");
defs[0].resolve();
}
var op = (function*() { throw new Error(); })();
yield [op, op];
ok(false, "Exception not thrown");
}
catch(e) {
ok(true, "Exception thrown");
ok(e instanceof Error, "e instanceof Error");
defs[1].resolve();
}
});
asyncTest("Ðeferred promise chaining", function() {
(function*() { return function(cb) { cb(null, "res"); } })
.run()
.then(function(res) {
equal(res, "res");
start();
});
});
asyncTest("Waiting for returned by run", function*() {
var captured;
yield (function*(){ captured = "res"; }).run();
equal(captured, "res");
});
asyncTest("Running generator function (non-invoked)", function*() {
var res = yield function*() { return function(cb) { cb(null, "res"); } }
equal(res, "res");
});
var SuccessErrorMock = function() {
var cbErr, cbRet;
return {
error: function(cb) { cbErr = cb; },
success: function(cb) { cbRet = cb; },
resolve: function(ret) { cbRet(ret); },
reject: function(e) { cbErr(e); }
}
}
asyncTest("Yielding Success/error chainer, success", function*() {
var mock = SuccessErrorMock();
setTimeout(function() { mock.resolve("res"); }, 1);
var a = yield mock;
equal(a, "res");
});
asyncTest("Yielding Success/error chainer, error", function*() {
var mock = SuccessErrorMock();
setTimeout(function() { mock.reject(new Error()); }, 1);
try {
var a = yield mock;
ok(false, "No exception thrown");
}
catch(e) {
ok(e instanceof Error, "Error thrown");
}
});
asyncTest("Yielding Success/error chainer, failure method", function*() {
var mock = SuccessErrorMock();
mock.failure = mock.error;
delete mock.error;
setTimeout(function() { mock.reject(new Error()); }, 1);
try {
var a = yield mock;
ok(false, "No exception thrown");
}
catch(e) {
ok(e instanceof Error, "Error thrown");
}
});
asyncTest("Foreach lodash override", function*() {
var arr = [1,2,3];
yield (_(arr).each(function*(item, i) {
equal(arr[i], item, "Item equal at " + i);
}));
});
asyncTest("map lodash override", function*() {
var mapped = yield _([5,1,3]).map(function*(ms) {
// To make it "difficult" we will make them not return in order
return Y.sleep(ms);
});
deepEqual(mapped, [5,1,3], "Equal");
});
asyncTest("filter lodash override test with even numbers returning not in order", function*() {
var arr = [0,3,2,1];
var filtered = yield _(arr).filter(function*(ms){
// Just making them return not in order
var same = yield Y.sleep(ms);
// Return even
return same % 2 == 0;
});
deepEqual(filtered, [0,2], "Filtered only contains even ");
})
asyncTest("reject lodash override test with even numbers returning not in order", function*() {
var arr = [0,3,2,1];
var filtered = yield _(arr).filter(function*(ms){
// Just making them return not in order
var same = yield Y.sleep(ms);
// Return even
return same % 2 !== 0;
});
deepEqual(filtered, [3,1], "Rejected only contains odd");
})
asyncTest("lodash map with generators can return boolean false values", function*() {
var arr = [false, false];
var mapped = yield _(arr).map(function*(a) { return a; });
deepEqual(mapped, [false,false], "Mapped only contains false");
});
asyncTest("thisArg works in lodash map", function*() {
var arr = [1];
var thisArg = { x: "test" }
var res = yield _.map([arr], function*(i) {
equal(this.x, "test");
}, thisArg);
});
asyncTest("thisArg works in lodash filter", function*() {
var arr = [1];
var thisArg = { x: "test" }
yield _.filter([arr], function*(i) {
equal(this.x, "test");
}, thisArg);
});
asyncTest("thisArg works in lodash filter", function*() {
var arr = [1];
var thisArg = { x: "test" }
yield _.filter([arr], function*(i) {
equal(this.x, "test");
}, thisArg);
});
asyncTest("thisArg works in lodash each", function*() {
var arr = [1];
var thisArg = { x: "test" }
yield _.each([arr], function*(i) {
equal(this.x, "test");
}, thisArg);
});
asyncTest("thisArg works in generator", function*() {
var thisArg = {x:"test"};
var gen = function*() {
equal(this, thisArg);
}
yield gen.call(thisArg);
});
asyncTest("run apply generator", function*() {
var thisArg = {x:"test"};
var args = [1];
var gen = function*() {
deepEqual(_.toArray(arguments), args);
equal(this, thisArg);
}
yield gen.apply(thisArg, args).run();
});
asyncTest("lodash bind works with generators", function*() {
var gen = function*() {
equal(this, "test");
};
yield _.bind(gen, "test");
});
asyncTest("lodash bindAll works with generators", function*() {
var a = {
gen: function*() {
deepEqual(this, a, "'this' scope right");
}
}
_.bindAll(a);
var f = a.gen;
yield f();
});
asyncTest("lodash bindAll works with named generators", function*() {
var a = {
genBound: function*() {
deepEqual(this, a, "'this' scope right");
},
genNotBound: function*() {
notDeepEqual(this, a, "'this' not object when not bound")
}
}
_.bindAll(a, "genBound");
var gb = a.genBound;
yield gb();
var gnb = a.genNotBound;
yield gnb();
});
asyncTest("Y.gen works with normal function", function*() {
var gen = Y.gen(function(cb) {
ok(true, "Entered func");
cb();
});
yield gen();
});
asyncTest("Y.gen works with object and private data", function*() {
var gen = Y.gen({
data: "test",
work: function(param, cb) {
equal(param, "param", "Correct parameter passing");
equal(this.data, "test", "Correct scope");
cb();
}
});
yield gen.work("param");
});
asyncTest("Y.gen works with function containing functions", function*() {
var obj = function() {}
obj.data = "test";
obj.work = function(param, cb) {
equal(param, "param", "Correct parameter passing");
equal(this.data, "test", "Correct scope");
cb();
};
var gen = Y.gen(obj);
yield gen.work("param");
});
asyncTest("Y.gen works with function circular references", function*() {
var obj = function() {}
obj.work = function(cb) {
ok(true, "Worked");
cb();
};
obj.work.fn = obj;
var gen = Y.gen(obj);
yield gen.work();
});