-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.js
199 lines (165 loc) · 5.86 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
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
import {Buffer} from 'node:buffer';
import test from 'ava';
import getStream from 'get-stream';
import {pEvent} from 'p-event';
import intoStream from './index.js';
const fixture = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.';
function generatorFrom(array) {
return function * () {
let index = 0;
while (index < array.length) {
yield array[index++];
}
};
}
function iterableFrom(array) {
return {
[Symbol.iterator]: generatorFrom(array),
};
}
function asyncGeneratorFrom(array) {
return async function * () {
let index = 0;
while (index < array.length) {
// eslint-disable-next-line no-await-in-loop
await Promise.resolve();
yield array[index++];
}
};
}
function asyncIterableFrom(array) {
return {
[Symbol.asyncIterator]: asyncGeneratorFrom(array),
};
}
test('string', async t => {
t.is(await getStream(intoStream(fixture)), fixture);
});
test('buffer', async t => {
const f = Buffer.from(fixture);
const result = await getStream.buffer(intoStream(f));
t.true(result.equals(f));
});
test('ArrayBuffer', async t => {
const f = Buffer.from(fixture);
const view = new Uint8Array(f.length);
for (const [index, element] of f.entries()) {
view[index] = element;
}
const result = await getStream.buffer(intoStream(view.buffer));
t.true(result.equals(f));
});
test('ArrayBuffer view', async t => {
const f = Buffer.from(fixture);
const view = new Uint8Array(f.length);
for (const [index, element] of f.entries()) {
view[index] = element;
}
const result = await getStream.buffer(intoStream(view));
t.true(result.equals(f));
});
test('array', async t => {
const result = await intoStream([...fixture]).toArray();
t.is(result.join(''), fixture);
});
test('iterable', async t => {
const iterable = iterableFrom([...fixture]);
t.is(await getStream(intoStream(iterable)), fixture);
});
test('generator', async t => {
const generator = generatorFrom([...fixture]);
t.is(await getStream(intoStream(generator())), fixture);
});
test('async iterable', async t => {
const iterable = asyncIterableFrom([...fixture]);
t.is(await getStream(intoStream(iterable)), fixture);
});
test.failing('async iterable - Uint8Array', async t => {
const fixture = new Uint8Array([21, 31]);
const iterable = asyncIterableFrom(fixture);
t.is(await getStream(intoStream(iterable)), fixture);
});
test('async generator', async t => {
const generator = asyncGeneratorFrom([...fixture]);
t.is(await getStream(intoStream(generator())), fixture);
});
test('promise', async t => {
const promise = new Promise(resolve => {
setImmediate(resolve.bind(null, fixture));
});
t.is(await getStream(intoStream(promise)), fixture);
});
test('promise resolving to iterable', async t => {
const promise = new Promise(resolve => {
setImmediate(resolve.bind(null, iterableFrom([...fixture])));
});
t.is(await getStream(intoStream(promise)), fixture);
});
test('promise resolving to async iterable', async t => {
const promise = new Promise(resolve => {
setImmediate(resolve.bind(null, asyncIterableFrom([...fixture])));
});
t.is(await getStream(intoStream(promise)), fixture);
});
test('stream errors when promise rejects', async t => {
const promise = new Promise((resolve, reject) => {
setImmediate(reject.bind(null, new Error('test error')));
});
await t.throwsAsync(getStream(intoStream(promise)), {message: 'test error'});
});
test('object mode', async t => {
const f = {foo: true};
t.deepEqual(await getStream.array(intoStream.object(f)), [f]);
const f2 = [{foo: true}, {bar: true}];
t.deepEqual(await getStream.array(intoStream.object(f2)), f2);
});
test('object mode from iterable', async t => {
const values = [{foo: true}, {bar: true}];
const iterable = iterableFrom(values);
t.deepEqual(await getStream.array(intoStream.object(iterable)), values);
});
test('object mode from async iterable', async t => {
const values = [{foo: true}, {bar: true}];
const iterable = asyncIterableFrom(values);
t.deepEqual(await getStream.array(intoStream.object(iterable)), values);
});
test('object mode from promise', async t => {
const f = {foo: true};
const promise = new Promise(resolve => {
setImmediate(resolve.bind(null, f));
});
t.deepEqual(await getStream.array(intoStream.object(promise)), [f]);
const f2 = [{foo: true}, {bar: true}];
const promise2 = new Promise(resolve => {
setImmediate(resolve.bind(null, f2));
});
t.deepEqual(await getStream.array(intoStream.object(promise2)), f2);
});
test('object mode from promise resolving to iterable', async t => {
const values = [{foo: true}, {bar: true}];
const promise = new Promise(resolve => {
setImmediate(resolve.bind(null, iterableFrom([{foo: true}, {bar: true}])));
});
t.deepEqual(await getStream.array(intoStream.object(promise)), values);
});
test('object mode from promise resolving to async iterable', async t => {
const values = [{foo: true}, {bar: true}];
const promise = new Promise(resolve => {
setImmediate(resolve.bind(null, asyncIterableFrom([{foo: true}, {bar: true}])));
});
t.deepEqual(await getStream.array(intoStream.object(promise)), values);
});
test('object mode errors when promise rejects', async t => {
const promise = new Promise((resolve, reject) => {
setImmediate(reject.bind(null, new Error('test error')));
});
await t.throwsAsync(getStream.array(intoStream.object(promise)), {message: 'test error'});
});
test('pushes chunk on next tick', async t => {
let flag = false;
(async () => {
flag = true;
})();
await pEvent(intoStream(Buffer.from(fixture)), 'data');
t.true(flag);
});