-
Notifications
You must be signed in to change notification settings - Fork 0
/
specs.js
109 lines (92 loc) · 2.69 KB
/
specs.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
var assert = require('assert');
var zo = require('./zo').zo;
require('../cupoftea/cupoftea');
var assertCorrectResult = function (result) {
assert.deepEqual(result, [1, 2, 3, 4]);
};
spec('each', function () {
var list = [1, 2, 3, 4];
spec('sync', function () {
var resultItems = [];
zo([1, 2, 3, 4]).each(function (item, done) {
resultItems.push(item);
done();
}).results(shouldCall(function (items) {
assertCorrectResult(resultItems);
}));
});
spec('async', function () {
var resultItems = [];
zo([1, 2, 3, 4]).each(function (item, done) {
process.nextTick(function () {
resultItems.push(item);
done();
})
}).results(shouldCall(function (items) {
assertCorrectResult(resultItems);
}));
});
spec('limits outstanding functions', function () {
var resultItems = [];
var outstanding = 0;
zo([1, 2, 3, 4]).each(function (item, done) {
outstanding++;
assert.ok(outstanding <= 2, 'expected maximum of 2 outstanding functions, got ' + outstanding);
process.nextTick(function () {
resultItems.push(item);
outstanding--;
done();
})
}, {limit: 2}).results(shouldCall(function (items) {
assertCorrectResult(resultItems);
}));
});
});
spec('reduce left', function () {
spec('async', function () {
zo([1, 2, 3, 4]).reduce([], function(memo, item, into) {
process.nextTick(function() {
memo.push(item);
into(memo);
});
}).results(shouldCall(assertCorrectResult));
});
spec('sync', function () {
zo([1, 2, 3, 4]).reduce([], function(memo, item, into) {
memo.push(item);
into(memo);
}).results(shouldCall(assertCorrectResult));
});
});
spec('reduce right', function () {
var assertCorrectResult = function (result) {
assert.deepEqual(result, [4, 3, 2, 1]);
};
spec('async', function () {
zo([1, 2, 3, 4]).reduceRight([], function(memo, item, into) {
process.nextTick(function() {
memo.push(item);
into(memo);
});
}).results(shouldCall(assertCorrectResult));
});
spec('sync', function () {
zo([1, 2, 3, 4]).reduceRight([], function(memo, item, into) {
memo.push(item);
into(memo);
}).results(shouldCall(assertCorrectResult));
});
});
spec('callstack', function () {
var items = [];
var numberOfItems = 10000000;
for (var n = 0; n < numberOfItems; n++) {
items.push(n);
}
console.log('items');
zo(items).reduce(0, function (memo, item, into) {
into(memo + 1);
}).results(shouldCall(function (res) {
assert.equal(res, numberOfItems);
}));
});