-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
656 lines (561 loc) · 21.7 KB
/
test.html
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<!doctype html>
<html>
<head>
<script>
window.Generator = window.oldGenerator = {};
</script>
<script src="lyfe.js"></script>
<script>
function test(name, testFunction) {
document.write("<div style='float:left; width: 50%;'>");
document.write(name + ": ");
var result, exception;
try {
result = testFunction();
} catch (e) {
exception = e;
}
if (result)
document.write("<b style='color:green'>passed</b>");
else if (exception)
document.write("<b style='color:red'>exception: " + exception.message + "</b>");
else
document.write("<b style='color:red'>failed</b>");
document.write("</div>");
}
function arrEqual (a1, a2) {
if (a1.length != a2.length)
return false;
for (var i = 0; i < a1.length; i++)
if (a1[i] !== a2[i])
return false;
return true;
}
</script>
</head>
<body>
<script>
var helloWorld = function () { alert("hello world"); };
var a = [13, null, helloWorld, -10.5, "lolcat"];
test("noConflict", function () {
var newGenerator = window.Generator;
if (newGenerator === oldGenerator)
return false;
var localGenerator = newGenerator.noConflict();
if (localGenerator !== newGenerator)
return false;
if (window.Generator !== oldGenerator)
return false;
window.Generator = newGenerator;
return true;
});
test("array constructor", function () {
var roundtrip = Generator(a).toArray();
return arrEqual(a, roundtrip);
});
test("function constructor", function () {
var g = Generator(function (Yield) {
Yield(13);
Yield(null);
Yield(helloWorld);
Yield(-10.5);
Yield("lolcat");
});
return arrEqual(a, g.toArray());
});
test("object constructor", function () {
var g = Generator({
a: 13,
nothing: null,
hello: helloWorld
});
var res = g.toArray();
if (res.length !== 3)
return false;
if (!arrEqual(res[0], ["a", 13]))
return false;
if (!arrEqual(res[1], ["nothing", null]))
return false;
if (!arrEqual(res[2], ["hello", helloWorld]))
return false;
return true;
});
test("filter", function () {
var nonNumbers = Generator(a).filter(function (val) { return typeof val !== "number" }).toArray();
return arrEqual(nonNumbers, [null, helloWorld, "lolcat"]);
});
test("take", function () {
return arrEqual(Generator(a).take(3).toArray(), a.slice(0, 3));
});
test("take zero", function () {
return arrEqual(Generator(a).take(0).toArray(), []);
});
test("skip", function () {
return arrEqual(Generator(a).skip(3).toArray(), a.slice(3));
});
test("skip zero", function () {
return arrEqual(Generator(a).skip(0).toArray(), a);
});
test("skip and take", function () {
return arrEqual(Generator(a).skip(2).take(2).toArray(), [helloWorld, -10.5]);
});
test("map", function () {
var types = Generator(a).map(function (val) { return typeof val; }).toArray();
return arrEqual(types, ["number", "object", "function", "number", "string"]);
});
test("zipWithArray, short array", function () {
var zipped = Generator(a).zipWithArray([2, 4, 3, 1], function (a, b) { return (typeof a).substr(0, b); }).toArray();
return arrEqual(zipped, ["nu", "obje", "fun", "n"]);
});
test("zipWithArray, long array", function () {
var zipped = Generator(a).zipWithArray([2, 4, 3, 1, 3, 4, 1, 9, 2], function (a, b) { return (typeof a).substr(0, b); }).toArray();
return arrEqual(zipped, ["nu", "obje", "fun", "n", "str"]);
});
test("changing source array in zipWithArray", function () {
var arr = [2, 4, 3, 1];
var zipGen = Generator(a).zipWithArray(arr, function (a, b) { return (typeof a).substr(0, b); });
var zipped1 = zipGen.toArray();
arr.push(1, 2);
arr.shift();
var zipped2 = zipGen.toArray();
return arrEqual(zipped2, ["numb", "obj", "f", "n", "st"]);
});
test("reduce", function () {
var sixFactorial = Generator([1, 2, 3, 4, 5, 6]).reduce(function (a, b) { return a * b; });
return sixFactorial === 720;
});
test("reduce with custom start value", function () {
var concatenated = Generator(["f", "ai", "l", "ed"]).reduce(function (a, b) { return a + b; }, "not ");
return concatenated === "not failed";
});
test("and", function () {
var b = [ "more", 2 / 7, { key: "value" } ];
return arrEqual(Generator(a).and(Generator(b)).toArray(), a.concat(b));
});
test("and with array", function () {
var b = [ "more", 2 / 7, { key: "value" } ];
return arrEqual(Generator(a).and(b).toArray(), a.concat(b));
});
test("takeWhile", function () {
var noFunctions = Generator(a).takeWhile(function (val) { return typeof val !== "function" }).toArray();
return arrEqual(noFunctions, [13, null]);
});
test("skipWhile", function () {
var startWithFalsy = Generator(a).skipWhile(function (val) { return val; }).toArray();
return arrEqual(startWithFalsy, a.slice(1));
});
test("all", function () {
var r1 = Generator(a).all();
var r2 = Generator(["yes", 1, {}]).all();
return !r1 && r2;
});
test("all short-circuits", function () {
var goodCount = 0;
var good = function () {
goodCount++;
return true;
};
var bad = function () {
return false;
}
var result = Generator([good, good, bad, good, bad]).map(function (f) { return f(); }).all();
return goodCount === 2 && !result;
});
test("any", function () {
var r1 = Generator(a).any();
var r2 = Generator(["", null, 0]).any();
return r1 && !r2;
});
test("any short-circuits", function () {
var goodCount = 0;
var good = function () {
goodCount++;
return true;
};
var bad = function () {
return false;
}
var result = Generator([bad, bad, good, good, bad, good, bad]).map(function (f) { return f(); }).any();
return goodCount === 1 && result;
});
test("all and any with nothing", function () {
var nothing = Generator([]);
return nothing.all() && !nothing.any();
});
test("first", function () {
var g = Generator(a);
return (g.skip(2).first() === helloWorld) && (typeof g.skip(20).first() === "undefined");
});
test("groupBy", function () {
var grouped = Generator(a).groupBy(function (v) { return (typeof v).substr(1,1) });
var expected_groups = ["u", "b", "t"];
var expected_contents = {
u: [13, helloWorld, -10.5],
b: [null],
t: ["lolcat"]
};
good = true;
grouped.forEach(function (val) {
var g = expected_groups.shift();
if (val.key !== g) {
good = false;
return;
}
if (!arrEqual(val.toArray(), expected_contents[g]))
good = false;
});
return good;
});
test("send to Yield", function () {
var gen = Generator(function (Yield) {
Yield("first");
var other = Yield("second");
Yield(other);
Yield("end");
});
var arr = [];
gen.forEach(function (val) {
arr.push(val);
return "third";
});
return arrEqual(arr, ["first", "second", "third", "end"]);
});
test("stop", function () {
var good = true;
var gen = Generator(function (Yield) {
Yield(42);
Yield(1337);
Yield(-5);
good = false;
Yield(100);
});
var lastValue;
gen.forEach(function (val, index, stop) {
lastValue = val;
if (val < 0)
stop();
});
return good && lastValue === -5;
});
test("cleanup when stopping outer", function () {
var innerClean = false,
outerClean = false;
var inner = Generator(function (Yield, yieldMany) {
try {
yieldMany(Generator([1, 2, 3, 4]));
} finally {
innerClean = true;
}
});
var outer = Generator(function (Yield, yieldMany, stop) {
try {
inner.forEach(function (val, index) {
Yield(val);
if (val > 1)
stop();
});
} finally {
outerClean = true;
}
});
var result = outer.toArray();
return arrEqual(result, [1, 2]) && innerClean && outerClean;
});
test("cleanup when stopping inner", function () {
var innerClean = false,
outerClean = false;
var inner = Generator(function (Yield, yieldMany) {
try {
yieldMany(Generator([1, 2, 3, 4]));
} finally {
innerClean = true;
}
});
var outer = Generator(function (Yield) {
try {
inner.forEach(function (val, index, stop) {
Yield(val);
if (val > 1)
stop();
});
} finally {
outerClean = true;
}
});
var result = outer.toArray();
return arrEqual(result, [1, 2]) && innerClean && outerClean;
});
test("yieldMany", function () {
var nested = [ [1, 7], 9, [3, [1, 8], 5], [2, [2, 3, 4]]];
var flatten = function (element) {
if (typeof element === "number")
return Generator([element]);
return Generator(function (Yield, yieldMany) {
Generator(element).forEach(function (val) {
yieldMany(flatten(val));
});
});
};
return arrEqual(flatten(nested).toArray(), [1, 7, 9, 3, 1, 8, 5, 2, 2, 3, 4]);
});
test("yieldMany with array", function () {
var nested = [ [1, 7], 9, [3, [1, 8], 5], [2, [2, 3, 4]]];
var flatten = function (element) {
if (typeof element === "number")
return [element];
return Generator(function (Yield, yieldMany) {
Generator(element).forEach(function (val) {
yieldMany(flatten(val));
});
}).toArray();
};
return arrEqual(flatten(nested), [1, 7, 9, 3, 1, 8, 5, 2, 2, 3, 4]);
});
test("changing source array", function () {
var arr = ["x", 42, null];
var gen = Generator(arr);
arr.push(helloWorld);
return arrEqual(gen.toArray(), arr);
});
test("evaluated", function () {
var called = 0;
var square = function (x) { called++; return x * x; };
var numbers = Generator([1, 3, 5]);
var squares = numbers.map(square).evaluated();
var doubled = squares.and(squares).toArray()
return arrEqual(doubled, [1, 9, 25, 1, 9, 25]) && called == 3;
});
test("thisObj argument to filter", function () {
var good1, good2;
var obj = { foo: 123 };
var gen = Generator([42]);
gen.filter(function (x) { good1 = this === window; return true; }).toArray();
gen.filter(function (x) { good2 = this === obj; return true; }, obj).toArray();
return good1 && good2;
});
test("thisObj argument to map", function () {
var good1, good2;
var obj = { foo: 123 };
var gen = Generator([42]);
gen.map(function (x) { good1 = this === window; return 43; }).toArray();
gen.map(function (x) { good2 = this === obj; return 43; }, obj).toArray();
return good1 && good2;
});
test("thisObj argument to forEach", function () {
var good1, good2;
var obj = { foo: 123 };
var gen = Generator([42]);
gen.forEach(function (x) { good1 = this === window; });
gen.forEach(function (x) { good2 = this === obj; }, obj);
return good1 && good2;
});
test("array constructor doesn't yield unset/deleted elements", function () {
var arr = [2, undefined, "bla", helloWorld, false];
arr[12] = "late";
delete arr[3];
return arrEqual(Generator(arr).toArray(), [2, undefined, "bla", false, "late"]);
});
test("groupBy only starts on iteration", function () {
var called = false;
var grouper = function (x) { called = true; return typeof x; }
var g = Generator(a).groupBy(grouper);
if (called)
return false;
g.evaluated();
return called;
});
test("groupBy re-groups everytime", function () {
var arr = [1, 2, -1];
var g = Generator(arr).groupBy(function (v) { return v > 0; });
var first = g.map(function (x) { return x.toArray(); }).toArray();
arr[1] = -2;
var second = g.map(function (x) { return x.toArray(); }).toArray();
var good = first.length === 2 && second.length === 2;
good = good && arrEqual(first[0], [1, 2]) && arrEqual(first[1], [-1]);
good = good && arrEqual(second[0], [1]) && arrEqual(second[1], [-2, -1]);
return good;
});
test("late yield throws exception", function () {
var leaked, good = false, seen = [];
var g = Generator(function (Yield) {
leaked = Yield;
Yield(1);
Yield("forty-two");
});
g.forEach(function (val) { seen.push(val); });
if (!(typeof leaked === "function"))
return false;
try {
leaked("boo");
} catch (e) {
if (e instanceof Generator.IterationError && e.message === "yield after end of iteration")
good = true;
else
throw e;
}
return good && arrEqual(seen, [1, "forty-two"]);
});
test("yield after stop throws exception", function () {
var leaked, good = false, seen = [];
var g = Generator(function (Yield) {
leaked = Yield;
Yield(1);
Yield("forty-two");
Yield(3);
});
g.forEach(function (val, index, stop) { seen.push(val); if (typeof val === "string") stop(); });
if (!(typeof leaked === "function"))
return false;
try {
leaked("boo");
} catch (e) {
if (e instanceof Generator.IterationError && e.message === "yield after end of iteration")
good = true;
else
throw e;
}
return good && arrEqual(seen, [1, "forty-two"]);
});
test("yield after exception throws exception", function () {
var leaked, good = false, seen = [];
var g = Generator(function (Yield) {
leaked = Yield;
Yield(1);
Yield("forty-two");
Yield(3);
});
try {
g.forEach(function (val) { seen.push(val); if (typeof val === "string") null(); });
} catch (e) {
if (!e instanceof TypeError)
throw e;
}
if (!(typeof leaked === "function"))
return false;
try {
leaked("boo");
} catch (e) {
if (e instanceof Generator.IterationError && e.message === "yield after end of iteration")
good = true;
else
throw e;
}
return good && arrEqual(seen, [1, "forty-two"]);
});
test("except", function() {
var good = arrEqual(Generator(a).except(helloWorld).toArray(), [13, null, -10.5, "lolcat"]);
good = good && arrEqual(Generator([1, 3, 3, 7]).except(3).toArray(), [1, 7]);
return good;
});
test("sortBy", function () {
return arrEqual(Generator(a).sortBy(function (x) { return typeof x; }).toArray(), [helloWorld, 13, -10.5, null, "lolcat"]);
});
test("sortBy is (probably) stable", function () {
var realSort = Array.prototype.sort;
Array.prototype.sort = function (f) { this.reverse(); realSort.call(this, f); };
var good = arrEqual(Generator(a).sortBy(function (x) { return typeof x; }).toArray(), [helloWorld, 13, -10.5, null, "lolcat"]);
Array.prototype.sort = realSort;
return good;
});
test("sortBy is cranky", function () {
var good = false;
try {
Generator(a).sortBy(function (x) { return NaN; }).toArray();
} catch (e) {
good = e instanceof TypeError && /^cannot compare/.test(e.message);
}
try {
var v = true;
Generator(a).sortBy(function (x) { v = !v; return v ? "0" : 0; }).toArray();
} catch (e) {
good = good && e instanceof TypeError && /^cannot compare/.test(e.message);
}
return good;
});
test("sortBy calls keyFunc exactly once per element", function () {
var n = 0;
Generator(a).sortBy(function (x) { n++; return typeof x; }).toArray();
return n === a.length;
});
test("any with predicate", function () {
var good = Generator(a).any(function (x) { return typeof x === "function" });
good = good && !Generator([-3, -7, 0]).any(function (x) { return x > 0; });
return good;
});
test("all with predicate", function () {
var good = !Generator(a).all(function (x) { return typeof x === "function" });
good = good && Generator([-3, -7, 0]).all(function (x) { return x <= 0; });
return good;
});
test("count", function () {
var good = Generator(a).count() === 5 && Generator([]).count() === 0;
var withHole = [1,2,3,,4];
return good && Generator(withHole).count() === 4;
});
var o0 = {foo: 12, bar: "hello"},
o1 = {foo: -3, bar: false},
o2 = {foo: 0, bar: null},
o3 = {foo: 42, bar: "hello"},
o4 = {foo: -1},
objects = Generator([o0, o1, o2, o3, o4]);
function testSelector(method, expectFoo, expectBar) {
test(method + " with string", function () {
return arrEqual(objects[method]("foo").toArray(), expectFoo) && arrEqual(objects[method]("bar").toArray(), expectBar);
});
}
testSelector("filter", [o0, o1, o3, o4], [o0, o3]);
testSelector("map", [12, -3, 0, 42, -1], ["hello", false, null, "hello", undefined]);
testSelector("takeWhile", [o0, o1], [o0]);
testSelector("skipWhile", [o2, o3, o4], [o1, o2, o3, o4]);
test("all with string", function () {
var good = !objects.all("foo");
good = good && Generator([]).all("foo");
good = good && objects.except(o2).all("foo");
good = good && !objects.all("bar");
good = good && Generator([o0, o3]).all("bar");
good = good && !Generator([o0, o3, o4]).all("bar");
return good;
});
test("any with string", function () {
var good = objects.any("foo");
good = good && !Generator([]).any("foo");
good = good && objects.any("bar");
good = good && !Generator([o1, o2, o4]).any("bar");
good = good && objects.except(o0).any("bar");
return good;
});
test("groupBy with string", function () {
var grouped = objects.groupBy("bar");
var expected_groups = ["hello", false, null, undefined];
var expected_contents = [
[o0, o3],
[o1],
[o2],
[o4]
]
var good = true;
grouped.forEach(function (val) {
var g = expected_groups.shift(),
e = expected_contents.shift();
if (val.key !== g) {
good = false;
return;
}
if (!arrEqual(val.toArray(), e))
good = false;
});
return good;
});
test("sortBy with string", function () {
return arrEqual(objects.sortBy("foo").toArray(), [o1, o4, o2, o0, o3])
});
test("zipWithArray behaves consistently with arrays with holes", function () {
var a = [13,,7];
var b = [4,9,,];
var r1 = Generator(a).zipWithArray(b, function (x, y) { return x + y }).toArray();
var r2 = Generator(b).zipWithArray(a, function (x, y) { return x + y }).toArray();
return arrEqual(r1, r2);
});
</script>
</body>
</html>