-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.js
129 lines (114 loc) · 2.93 KB
/
test.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
const test = require('tape');
const fromIter = require('./index');
test('it sends array items (iterable) to a puller sink', t => {
t.plan(13);
const source = fromIter([10, 20, 30]);
const downwardsExpectedTypes = [
[0, 'function'],
[1, 'number'],
[1, 'number'],
[1, 'number'],
[2, 'undefined'],
];
const downwardsExpected = [10, 20, 30];
let talkback;
source(0, (type, data) => {
const et = downwardsExpectedTypes.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 0) {
talkback = data;
talkback(1);
return;
}
if (type === 1) {
const e = downwardsExpected.shift();
t.equals(data, e, 'downwards data is expected: ' + e);
talkback(1);
}
});
});
test('it sends array entries (iterator) to a puller sink', t => {
t.plan(13);
const source = fromIter(['a', 'b', 'c'].entries());
const downwardsExpectedTypes = [
[0, 'function'],
[1, 'object'],
[1, 'object'],
[1, 'object'],
[2, 'undefined'],
];
const downwardsExpected = [[0, 'a'], [1, 'b'], [2, 'c']];
let talkback;
source(0, (type, data) => {
const et = downwardsExpectedTypes.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 0) {
talkback = data;
talkback(1);
return;
}
if (type === 1) {
const e = downwardsExpected.shift();
t.deepEquals(data, e, 'downwards data is expected: ' + e);
talkback(1);
}
});
});
test('it does not blow up the stack when iterating something huge', t => {
t.plan(2);
let i = 0;
function* gen() {
while (i < 1000000) {
yield i++;
}
}
const source = fromIter(gen());
let talkback;
let iterated = false;
source(0, (type, data) => {
if (type === 0) {
talkback = data;
talkback(1);
return;
}
if (type === 1) {
talkback(1);
return;
}
if (type === 2) {
t.equals(i, 1000000, '1 million items were iterated');
iterated = true;
return;
}
});
t.equals(iterated, true, 'iteration happened synchronously');
});
test('it stops sending after source completion', t => {
t.plan(5);
const source = fromIter([10, 20, 30]);
const actual = [];
const downwardsExpectedTypes = [
[0, 'function'],
[1, 'number'],
];
let talkback;
source(0, (type, data) => {
const et = downwardsExpectedTypes.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 0) {
talkback = data;
talkback(1);
return;
}
if (type === 1) {
actual.push(data);
talkback(2);
talkback(1);
talkback(1);
}
});
t.deepEquals(actual, [10]);
});