-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
702 lines (653 loc) · 26.4 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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head><title>dysfunctional.JS Test Suite</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<link rel="stylesheet" type="text/css" href="common.css">
<script type="text/javascript" src="dysfunctional.js"></script>
<script type="text/javascript" src="test/test.js"></script>
<script type="text/javascript" src="test/test-output.js"></script>
<script type="text/javascript">
function runTests(lib){
var hint=document.getElementById('run-hint');
hint.parentNode.removeChild(hint);
var lib=dysfunctional; // Shortcut
lib.activatePrototypeExtensions();
// The tests.
function getTests(){
// Some functions used in the tests.
function foo(){
return 'foo';}
function bar(){
return 'bar';}
function xyz(){
return 'xyz';}
function thisFoo(){
return this.foo;}
function thisBar(){
return this.bar;}
function thisXyz(){
return this.xyz;}
// Some objects used in the tests.
var o={foo:'oof',bar:'rab',xyz:'zyx'};
var oFoo={val:'foo'};
var oBar={val:'bar'};
var oXyz={val:'xyz'};
return [
{
'test':function(fw){
fw.assertTruthy(typeof lib.noOp() == 'undefined');
fw.assertEqual('foo', lib.id('foo'));},
'description':'id()',
'functions':['id', 'noOp']},
{
'test':function(fw){
fw.assertEqual('bar', lib.opIfElse(false, 'foo', 'bar'));
fw.assertEqual('bar', lib.opIfElse(false, 'foo', 'bar'));
fw.assertEqual(foo(), lib.ifElse(true, foo, bar));
fw.assertEqual(bar(), lib.ifElse(false, foo, bar));
fw.assertEqual(o.foo, lib.ifElse(true, thisFoo, thisBar, o));
fw.assertEqual(o.bar, lib.ifElse(false, thisFoo, thisBar, o));},
'description':'Functionified conditionals: opIfElse, ifElse',
'functions':['opIfElse','ifElse']},
{
'test':function(fw){
fw.assertTruthy(lib.lt(1,2));
fw.assertTruthy(lib.lt(-Infinity, 100));
fw.assertFalsy(lib.lt(-4, -6));
fw.assertTruthy(lib.lte(-1,1));
fw.assertTruthy(lib.lte(50000, Infinity));
fw.assertTruthy(lib.between(200, 100, 500));
fw.assertTruthy(lib.between(50, 50, 200));
fw.assertTruthy(lib.between(300,75,300));
fw.assertFalsy(lib.between(99, 100, 200));
fw.assertFalsy(lib.between(304, 302, 303));},
'description':'Number comparisons: lt, lte, gt, gte, between',
'functions':['lt','lte','gt','gte', 'between']},
{
'test':function(fw){
fw.assertEqual(15, lib.add(-5, 20));
fw.assertEqual(20, lib.subtract(30, 10));
fw.assertEqual(12, lib.multiply(-3, -4));
fw.assertEqual(11, lib.divide(110, 10));
fw.assertEqual(3, lib.negate(-3));
fw.assertEqual(4, lib.reciprocal(0.25));},
'description':'Number arithmetic: add, subtract, multiply, divide, negate, reciprocal',
'functions':['add','subtract','multiply','divide','negate','reciprocal']},
{
'test':function(fw){
fw.assertEqual(0, lib.sum());
fw.assertEqual(5, lib.sum(1,2,0,3,4,-5));
fw.assertEqual(1, lib.product());
fw.assertEqual(60, lib.product(-3,4,1,-5));
},
'description':'Number arithmetic: sum, product',
'functions':['sum','product']},
{
'test':function(fw){
fw.assertEqual('neg', lib.opIfSgn(-3, 'neg', 'zero', 'pos'));
fw.assertEqual('zero', lib.opIfSgn(0, 'neg', 'zero', 'pos'));
fw.assertEqual('pos', lib.opIfSgn(0.25, 'neg', 'zero', 'pos'));
fw.assertEqual('foo', lib.ifSgn(-1.2, foo, bar, xyz));
fw.assertEqual('bar', lib.ifSgn(0, foo, bar, xyz));
fw.assertEqual('xyz', lib.ifSgn(15, foo, bar, xyz));
fw.assertEqual('oof', lib.ifSgn(-1.2, thisFoo, thisBar, thisXyz, o));
fw.assertEqual('rab', lib.ifSgn(0, thisFoo, thisBar, thisXyz, o));
fw.assertEqual('zyx', lib.ifSgn(35, thisFoo, thisBar, thisXyz, o));},
'description':'opIfSgn, ifSgn',
'functions':['opIfSgn','ifSgn']},
{
'test':function(fw){
fw.assertTruthy(lib.not(false));
fw.assertFalsy(lib.not(true));},
'description':'not',
'functions':['not']},
{
'test':function(fw){
function f(x){
return x*this.y;}
var g=lib.bind(f, {y:20});
var obj={g:g};
var h=lib.hold(f, 2);
fw.assertEqual(60, obj.g(3));
fw.assertEqual(24, h.call({y:12}, 4));},
'description':'bind, hold',
'functions':['bind','hold']},
{
'test':function(fw){
var o1={x:5},o2={x:2,y:3},o3={y:4,z:1};
var merged=lib.merge(o1, o2, o3);
fw.assertEqual(2, merged.x);
fw.assertEqual(4, merged.y);
fw.assertEqual(1, merged.z);
var target={x:8,y:6,z:7};
lib.inject(target, o2, o1);
fw.assertEqual(5, target.x);
fw.assertEqual(3, target.y);
fw.assertEqual(7, target.z);},
'description':'Object functions: inject, merge',
'functions':['merge','inject']},
{
'test':function(fw){
var testArray=[1,2,3,1,2,3,1,2,3];
fw.assertEqual(-1, lib.indexOf(testArray, 4), 'Value not in array.');
fw.assertEqual(-1, lib.indexOf(testArray, 1, 7), 'Start index too high to find the value.');
fw.assertEqual(1, lib.indexOf(testArray, 2));
fw.assertEqual(3, lib.indexOf(testArray, 1, 2));
fw.assertEqual(3, lib.indexOf(testArray, 1, 3));
fw.assertEqual(-1, lib.lastIndexOf(testArray, 4), 'Value not in array.');
fw.assertEqual(-1, lib.lastIndexOf(testArray, 3, 1), 'Start index too low to find the value.');
fw.assertEqual(8, lib.lastIndexOf(testArray, 3));
fw.assertEqual(2, lib.lastIndexOf(testArray, 3, 4));
fw.assertEqual(2, lib.lastIndexOf(testArray, 3, 2));},
'description':'Array functions: indexOf, lastIndexOf',
'functions':['indexOf','lastIndexOf']},
{
'test':function(fw){
var testArray=[5,3,-2,9];
fw.assertEqual(5, lib.first(testArray));
fw.assertEqual(9, lib.last(testArray));},
'description':'Array functions: first, last',
'functions':['first','last']},
{
'test':function(fw){
var testArray=[3, 5, 9, 20];
function testFunc(a, b){
return b-a;}
fw.assertEqual(55, lib.reduce([55], testFunc));
fw.assertEqual(55, lib.reduceRight([55], testFunc));
fw.assertEqual(56, lib.reduce([], testFunc, 56));
fw.assertEqual(56, lib.reduceRight([], testFunc, 56));
fw.assertEqual(13, lib.reduce(testArray, testFunc));
fw.assertEqual(14, lib.reduce(testArray, testFunc, 1));
fw.assertEqual(-13, lib.reduceRight(testArray, testFunc));
fw.assertEqual(-12, lib.reduceRight(testArray, testFunc, 1));
fw.assertException(function(){lib.reduce([], testFunc);}, new TypeError(''));
fw.assertException(function(){lib.reduceRight([], testFunc);}, new TypeError(''));},
'description':'Array functions: reduce, reduceRight',
'functions':['reduce','reduceRight']},
{
'test':function(fw){
fw.assertTruthy(lib.every([], lib.noOp), 'every([], ...) returns true.');
fw.assertTruthy(lib.every([], lib.noOp, {}), 'every([], ...) returns true.');
fw.assertFalsy(lib.some([], lib.noOp));
fw.assertFalsy(lib.some([], lib.noOp, {}));
function even(n,i,a){
return !(n%2);}
function mod(n,i,a){
return !(n%this.div);}
fw.assertTruthy(lib.every([2,4,6,8,10], even), '2,4,6,8,10 are even.');
fw.assertTruthy(lib.every([2,4,6,8,10], mod, {div:2}), '2,4,6,8,10 are even (with context).');
fw.assertFalsy(lib.every([2,4,5,8,10], even));
fw.assertFalsy(lib.every([2,4,6,9,10], mod, {div:2}));
fw.assertTruthy(lib.some([1,3,4,6,9], even), 'Some elements are even.');
fw.assertTruthy(lib.some([2,5,7,8,9], mod, {div:2}), 'Some elements are even (with context).');
fw.assertFalsy(lib.some([1,3,5,7,9], even));
fw.assertFalsy(lib.some([1,3,5,7,9], mod, {div:2}));},
'description':'Array functions: every, some',
'functions':['every','some']},
{
'test':function(fw){
var arr=[2,3,5];
var mapResult1=lib.map(arr, function(n,i,a){return n*n+i;});
var mapResult2=lib.map(arr, function(n,i,a){return (n*n+i)+this.suffix;}, {suffix:'px'});
fw.assertEqual(4, mapResult1[0]);
fw.assertEqual(10, mapResult1[1]);
fw.assertEqual(27, mapResult1[2]);
fw.assertEqual('4px', mapResult2[0]);
fw.assertEqual('10px', mapResult2[1]);
fw.assertEqual('27px', mapResult2[2]);
var forEachResult1='';
var forEachResult2='';
lib.forEach(arr, function(n,i,a){forEachResult1+=n+''+i+''+a.length+'_';});
lib.forEach(arr, function(n,i,a){forEachResult2+=n+''+i+''+a.length+this.suffix;}, {suffix:'-'});
fw.assertEqual('203_313_523_', forEachResult1);
fw.assertEqual('203-313-523-', forEachResult2);},
'description':'Array functions: forEach, map',
'functions':['forEach','map']},
{
'test':function(fw){
fw.assertEqual(0, lib.filter([1,2,3],function(n,i,a){return false;}).length);
fw.assertEqual(0, lib.filter([1,2],function(n,i,a){return !this.value;},{value:true}).length);
function f(n,i,a){
return n+i==a.length;}
function g(n,i,a){
return n+i==a.length+this.bonus;}
var result1=lib.filter([5,2,3,2,6],f);
var result2=lib.filter([3,6,4,4,1],g,{bonus:2});
fw.assertEqual(3, result1.length);
fw.assertEqual(5, result1[0]);
fw.assertEqual(3, result1[1]);
fw.assertEqual(2, result1[2]);
fw.assertEqual(2, result2.length);
fw.assertEqual(6, result2[0]);
fw.assertEqual(4, result2[1]);},
'description':'Array function: filter',
'functions':['filter']},
{
'test':function(fw){
var testArray=[8,5,3,9,1];
fw.assertEqual(8, lib.head(testArray)[0]);
fw.assertEqual(1, lib.tail(testArray)[0]);
var r1=lib.head(testArray, 3);
var r2=lib.tail(testArray, 3);
fw.assertEqual(3, r1.length);
fw.assertEqual(3, r2.length);
fw.assertEqual(8, r1[0]);
fw.assertEqual(5, r1[1]);
fw.assertEqual(3, r1[2]);
fw.assertEqual(3, r2[0]);
fw.assertEqual(9, r2[1]);
fw.assertEqual(1, r2[2]);
var r3=lib.head([1,2],5);
var r4=lib.tail([1,2],5);
fw.assertEqual(2, r3.length);
fw.assertEqual(2, r4.length);
fw.assertEqual(1, r3[0]);
fw.assertEqual(2, r3[1]);
fw.assertEqual(1, r4[0]);
fw.assertEqual(2, r4[1]);},
'description':'Array functions: head, tail',
'functions':['head', 'tail']},
{
'test':function(fw){
var testCs=[true,7,'hello',Math];
for(var i=0,l=testCs.length;i<l;i++){
var value=testCs[i];
var f=lib.constant(value);
for(var j=0;j<10;j++){
fw.assertTruthy(f()===value);}}},
'description':'Function: constant',
'functions':['constant']},
{
'test':function(fw){
function add(s1, s2){return s1+s2;};
function doubleString(s){return s+s;};
function addX(s){return s+'x';};
fw.assertEqual('46', lib.compose(add)('4', '6'));
fw.assertEqual('nmnmxnmnmx', lib.compose(add, doubleString, addX, doubleString)('n','m'));},
'description':'Function: compose',
'functions':['compose']},
{
'test':function(fw){
function reverse(a,b,c,d,e){return [e,d,c,b,a];}
function sum(){
var result=0;
for(var i=0,l=arguments.length;i<l;i++){
result+=arguments[i];}
return result;}
var cr=lib.curry(reverse);
fw.assertEqualArrays(cr(1,2,3,4,5),[5,4,3,2,1],'Called with all arguments.');
fw.assertEqualArrays(cr(1,2,3,4,5),cr(1,2)(3,4,5));
fw.assertEqualArrays(cr(1,2,3,4,5),cr(1)(2)(3)(4)(5));
fw.assertEqualArrays(cr(1,2,3,4,5),cr()(1)(2,3)()(4,5));
var add=lib.curry(sum, 2);
var add5=add(5);
var add_=add();
fw.assertEqual(7, add5(2));
fw.assertEqual(10, add_(6,4));
fw.assertEqual(12, add(10,2));
fw.assertEqual(20, lib.curry(sum, 5)(3)(4,5)()(6)()(2));},
'description':'Function: curry',
'functions':['curry']},
{
'test':function(fw){
function fibonacciStep(n, m){
return [m, n+m];}
var fib=lib.generator(fibonacciStep, [0,1], lib.first);
fw.assertEqualArrays([1,1,2,3,5,8,13],[fib(),fib(),fib(),fib(),fib(),fib(),fib()]);
function inc(n){
return [n+1];}
var nat=lib.generator(inc, [0]);
fw.assertEqualArrays([1],nat());
fw.assertEqualArrays([2],nat());
fw.assertEqualArrays([3],nat());},
'description':'Function: generator',
'functions':['generator']},
{
'test':function(fw){
fw.assertTruthy(lib.and(1,1));
fw.assertFalsy(lib.and(1,0));
fw.assertFalsy(lib.and(0,1));
fw.assertFalsy(lib.and(0,0));
fw.assertTruthy(lib.or(1,1));
fw.assertTruthy(lib.or(1,0));
fw.assertTruthy(lib.or(0,1));
fw.assertFalsy(lib.or(0,0));
fw.assertFalsy(lib.xor(1,1));
fw.assertTruthy(lib.xor(1,0));
fw.assertTruthy(lib.xor(0,1));
fw.assertFalsy(lib.xor(0,0));
fw.assertFalsy(lib.nand(1,1));
fw.assertTruthy(lib.nand(1,0));
fw.assertTruthy(lib.nand(0,1));
fw.assertTruthy(lib.nand(0,0));
fw.assertFalsy(lib.nor(1,1));
fw.assertFalsy(lib.nor(1,0));
fw.assertFalsy(lib.nor(0,1));
fw.assertTruthy(lib.nor(0,0));
fw.assertTruthy(lib.xnor(1,1));
fw.assertFalsy(lib.xnor(1,0));
fw.assertFalsy(lib.xnor(0,1));
fw.assertTruthy(lib.xnor(0,0));},
'description':'Boolean functions.',
'functions':['and','or','xor','nand','nor','xnor']},
{
'test':function(fw){
fw.assertEqual(3,lib.item([0,1,2,3],3));
fw.assertEqual('world',lib.item({'hello':'world'},'hello'));
fw.assertEqualArrays([1,4,7],lib.items([2,1,5,4,7,8],1,3,4));
fw.assertEqualArrays(['foo','bar'],lib.items({'x':'bar','y':'foo','z':'xyz'},'y','x'));
},
'description':'Extraction of items from arrays and objects.',
'functions':['item','items']},
{
'test':function(fw){
var s1='\n foo \t';
fw.assertEqual('foo', lib.trim(s1), 'Trim.');
fw.assertEqual(3, lib.trim(s1).length, 'Trim length.');
fw.assertEqual('\n foo', lib.rtrim(s1), 'Trim right.');
fw.assertEqual(5, lib.rtrim(s1).length, 'Right trim length.');
fw.assertEqual('foo \t', lib.ltrim(s1), 'Trim left.');
fw.assertEqual(5, lib.ltrim(s1).length, 'Left trim length.');},
'description':'Trim whitespace.',
'functions':['trim','ltrim','rtrim']},
{
'test':function(fw){
for(var i=0;i<100;i++){
var r=lib.random(2,8);
fw.assertTruthy(r>=2&&r<=8);}},
'description':'Random numbers.',
'functions':['random']},
{
'test':function(fw){
var data=[1,2,7,8,4,1,9,3]; // Sorted: 1,1,2,3,4,7,8,9
// With standard functions.
var med=lib.median(data);
var arM=lib.arithmeticMean(data);
var geoM=lib.geometricMean(data);
var harM=lib.harmonicMean(data);
fw.assertTruthy(med==3||med==4, 'Median (standard), should be 3 or 4, was '+med+'.');
fw.assertTruthy(arM==4.375, 'Arithmetic mean (standard).');
fw.assertTruthy(geoM>=3.2383 && geoM<=3.239, 'Geometric mean (standard).');
fw.assertTruthy(harM>=2.31 && harM<=2.311, 'Harmonic mean (standard).');
// With custom functions.
var cMed=lib.median(data, Math.sgn);
var cArM=lib.arithmeticMean(data, lib.add, lib.divide);
var cGeoM=lib.geometricMean(data, lib.multiply, Math.pow);
var cHarM=lib.harmonicMean(data, lib.add, lib.divide, 1);
fw.assertTruthy(cMed==3||cMed==4, 'Median (custom functions), should be 3 or 4, was '+med+'.');
fw.assertTruthy(cArM==4.375, 'Arithmetic mean (custom functions).');
fw.assertTruthy(cGeoM>=3.2383 && cGeoM<=3.239, 'Geometric mean (custom functions).');
fw.assertTruthy(cHarM>=2.31 && cHarM<=2.311, 'Harmonic mean (custom functions).');
},
'description':'Test median and means.',
'functions':['median','arithmeticMean','geometricMean','harmonicMean']},
{
'test':function(fw){
fw.assertTruthy((8).gt(4));
fw.assertEqual(7, (-7).abs());
fw.assertFalsy((true).not());
fw.assertFalsy((true).not.hold()());
fw.assertEqual(6, [1,3,2].reduce(function(previous, item, index, collection){
return previous+(collection[index]==item?item:0);}, 0));
fw.assertEqualArrays([6,4,13], (function(n, i, arr){
return n*n+i+arr.length+this.add;}).map([2,1,3],{add:-1}));
},
'description':'Prototypal extensions.',
'functions':['activatePrototypeExtensions']},
{
'test':function(fw){
var obj1={foo:'old'};
lib.addMethod(obj1, 'foo', lib.noOp);
fw.assertEqual(obj1.foo, 'old');
var obj2={foo:'old'};
lib.addMethod(obj2, 'foo', lib.constant('new'), {override:true});
fw.assertEqual('new', obj2.foo());
var obj3={value:5};
lib.addMethod(obj3, 'foo', function(obj, x){
return obj.value+x;});
fw.assertEqual(8, obj3.foo(3));
var obj4={value:8};
lib.addMethod(obj4, 'foo', function(factor, obj, suffix){
return (factor*obj.value)+suffix;}, {thisPosition:1});
fw.assertEqual('24px', obj4.foo(3, 'px'));
},
'description':'Object augmentation - add method.',
'functions':['addMethod']},
{
'test':function(fw){
fw.assertEqual(
55,
lib.loop(
function(){
return this.x<10;},
function(){
this.x++;
this.sum+=this.x;
return this.sum;},
{x:0,sum:0}));
fw.assertEqual(
55,
lib.until(
function(){
return this.x>=10;},
function(){
this.x++;
this.sum+=this.x;
return this.sum;},
{x:0,sum:0}));
},
'description':'Looping - loop and until.',
'functions':['loop','until']},
{
'test':function(fw){
fw.assertTruthy(lib.even(8));
fw.assertFalsy(lib.even(-7));
fw.assertTruthy(lib.odd(-3));
fw.assertFalsy(lib.odd(4));},
'description':'Odd and even.',
'functions':['even','odd']},
{
'test':function(fw){
var simpleObj={
a:'foo',
z:'bar',
b:'boofar'};
fw.assertEqualArrays(['a','b','z'], lib.keys(simpleObj).sort());
fw.assertEqualArrays(['bar','boofar','foo'], lib.values(simpleObj).sort());
function Bar(){}
Bar.prototype={x:'foo'};
var bar=new Bar();
bar.y='bar';
fw.assertEqualArrays(['y'], lib.keys(bar));
fw.assertEqualArrays(['bar'], lib.values(bar));
fw.assertEqualArrays(['x','y'], lib.keys(bar, true).sort());
fw.assertEqualArrays(['bar', 'foo'], lib.values(bar, true).sort());},
'description':'Keys and values.',
'functions':['keys','values']},
{
'test':function(fw){
function Bar(){}
Bar.prototype={x:200};
var bar=new Bar();
bar.y='bar';
bar.z='boofar';
// Every
fw.assertTruthy(lib.everyProperty(bar, function(value, name, obj){
return typeof value == 'string' && name != 'x' && this == obj && obj == bar && obj[name]==value;}, bar));
fw.assertFalsy(lib.everyProperty(bar, function(value, name, obj){
return name.length==3;}));
// Some
fw.assertTruthy(lib.someProperty(bar, function(value, name, obj){
return name=='y' && obj[name]==value && this == obj && obj == bar;}, bar));
fw.assertFalsy(lib.someProperty(bar, function(value, name, obj){
return obj[name] != value || this[name] != value || name == 'x';}, bar));
// Map
var mappedBar=lib.mapProperties(bar, function(value, name, obj){
return (bar==obj && bar==this)?name+'_'+value:'';}, bar);
fw.assertEqual('y_bar', mappedBar.y);
fw.assertEqual('z_boofar', mappedBar.z);
fw.assertFalsy(mappedBar.x);
// Filter
var filteredBar=lib.filterProperties(bar, function(value, name, obj){
return obj[name]==value && obj==bar && this==bar && name=='z';}, bar);
fw.assertEqual('boofar', filteredBar.z);
fw.assertTruthy(typeof filteredBar.y=='undefined');
// For each
var forEachBar={};
lib.forEachProperty(bar, function(value, name, obj){
if (this==obj && this==bar && obj[name]==value){
forEachBar[name]=value;}}, bar);
fw.assertEqual('bar', forEachBar.y);
fw.assertEqual('boofar', forEachBar.z);
fw.assertFalsy(forEachBar.hasOwnProperty('x'));},
'description':'Object functions: every, filter, forEach, map, some',
'functions':['everyProperty', 'someProperty', 'mapProperties', 'filterProperties', 'forEachProperty']},
{
'test':function(fw){
var tryRan=false;
var catchRan=false;
var finallyRan=false;
var elseRan=false;
var tryError=new Error('try');
var catchError=new Error('catch');
function resetRan(){
tryRan=false;
catchRan=false;
finallyRan=false;
elseRan=false;}
function throwInTryBlock(){
tryRan=true;
throw tryError;}
function throwInCatchBlock(){
catchRan=true;
throw catchError;}
function returnTry(){
tryRan=true;
return 'try';}
function returnCatch(){
catchRan=true;
return 'catch';}
function returnFinally(){
finallyRan=true;
return 'finally';}
function runElse(){
elseRan=true;}
function assertRan(ranTry, ranCatch, ranFinally, ranElse){
function s(what, expectedValue){
return expectedValue?
'Expected '+what+' block to run, but it did not.':
'Expected '+what+' block not to run, but it did.';}
ranTry?fw.assertTruthy(tryRan, s('try', true)):fw.assertFalsy(tryRan, s('try', false));
ranCatch?fw.assertTruthy(catchRan, s('catch', true)):fw.assertFalsy(catchRan, s('catch', false));
ranFinally?fw.assertTruthy(finallyRan, s('finally', true)):fw.assertFalsy(finallyRan, s('finally', false));
ranElse?fw.assertTruthy(elseRan, s('else', true)):fw.assertFalsy(elseRan, s('else', false));}
function afterExecution(t, c, f, e){
assertRan(t, c, f,e );
resetRan();}
fw.assertException(function(){lib.fThrow('foobar_exception');}, 'foobar_exception');
fw.assertEqual(lib.tryCatch(returnTry, returnCatch), returnTry());
afterExecution(true, false, false, false);
fw.assertEqual(lib.tryCatch(throwInTryBlock, returnCatch), returnCatch());
afterExecution(true, true, false, false);
fw.assertEqual(lib.tryCatch(returnTry, returnCatch, returnFinally), returnFinally());
afterExecution(true, false, true, false);
fw.assertEqual(lib.tryCatch(throwInTryBlock, returnCatch, returnFinally), returnFinally());
afterExecution(true, true, true, false);
fw.assertException(function(){
lib.tryCatch(throwInTryBlock, throwInCatchBlock);}, catchError);
afterExecution(true, true, false, false);
fw.assertException(function(){
lib.tryCatch(throwInTryBlock, throwInCatchBlock, returnFinally())}, catchError);
afterExecution(true, true, true, false);
fw.assertEqual(lib.tryCatch(returnTry, returnCatch, returnFinally, runElse), returnFinally());
afterExecution(true, false, true, true);
fw.assertEqual(lib.tryCatch(throwInTryBlock, returnCatch, returnFinally, runElse), returnFinally());
afterExecution(true, true, true, false);},
'description':'Functification of exception handling.',
'functions':['fThrow', 'tryCatch']},
{
'test':function(fw){
fw.assertEqualArrays([], lib.range());
fw.assertEqualArrays([0, 1, 2, 3, 4], lib.range(5));
fw.assertEqualArrays([], lib.range(0));
fw.assertEqualArrays([], lib.range(-200));
fw.assertEqualArrays([2, 3, 4], lib.range(2, 5));
fw.assertEqualArrays([], lib.range(4, 1));
fw.assertEqualArrays([], lib.range(2, 2));
fw.assertEqualArrays([3, 5, 7], lib.range(3, 8, 2));
fw.assertEqualArrays([6, 5, 4], lib.range(6, 3, -1));
fw.assertEqualArrays([], lib.range(4, 4, 2), '4, 4, 2');
fw.assertEqualArrays([], lib.range(4, 4, -2), '4, 4, -2');
fw.assertEqualArrays([3], lib.range(3, 5, 3), '3, 5, 3');
fw.assertEqualArrays([5], lib.range(5, 3, -3), '5, 3, -3');},
'description':'Range function.',
'functions':['range']},
{
'test':function(fw){
fw.assertEqual(-1, lib.sgn(-5));
fw.assertEqual(0, lib.sgn(0));
fw.assertEqual(1, lib.sgn(20));},
'description':'Signum.',
'functions':['sgn']},
{
'test':function(fw){
fw.assertEqual(0, lib.modulo(6, 3));
fw.assertEqual(2, lib.modulo(7, 5));
fw.assertEqual(2, lib.modulo(2, 5));
fw.assertEqual(2, lib.modulo(7, -5));
fw.assertEqual(3, lib.modulo(-2, -5), '-2, -5');
fw.assertEqual(3, lib.modulo(-7, -5), '-7, -5');},
'description':'Modulo',
'functions':['modulo']},
{
'test':function(fw){
var obj = {
x: 20,
getMultipliedX: function(factor){
return factor*this.x;}};
var getMX = lib.methodToFunction('getMultipliedX');
fw.assertEqual(60, getMX(obj, 3));},
'description':'Convert method to function.',
'functions':[]}
];}
var tests=getTests();
// Find untested functions.
var fns=[];
for(var prop in lib){
if (lib.hasOwnProperty(prop)){
fns.push(prop);}}
var tested=[];
for(var i=0,l=tests.length;i<l;i++){
var testedFns=tests[i].functions;
for(var j=0,max=testedFns.length;j<max;j++){
tested.push(testedFns[j]);}}
function inside(arr, val){
for(var i=0,l=arr.length;i<l;i++){
if (arr[i]==val){
return true;}}
return false;}
var untested=[];
for(var i=0,l=fns.length;i<l;i++){
if (!inside(tested, fns[i])){
untested.push(fns[i]);}}
if (untested.length){
document.getElementById('results').innerHTML='These functions have not been tested: '+untested.join(', ')+'.';}
else{
document.getElementById('results').innerHTML='Congratulations, all functions have been tested.';}
createOutput(document, document.getElementById('results'))(TestFramework.run(tests));
}
</script>
</head>
<body>
<h1>dysfunctional.JS Test Suite</h1>
<h2>Results</h2>
<p>To see the results, run the tests. Or go back to the <a href=".">dysfunctional.JS overview</a>.</p>
<p id="run-hint">If javascript is activated, you can <input type="button" onclick="runTests(dysfunctional);" value="run the tests!"></p>
<noscript><p>Sadly, without javascript the test suite cannot run.</p></noscript>
<div id="results" class="results"></div>
</body>
</html>