forked from nodejs/readable-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-stream-asIndexedPairs.mjs
82 lines (75 loc) · 1.98 KB
/
test-stream-asIndexedPairs.mjs
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
import '../common/index.mjs'
import { Readable } from '../../lib/ours/index.js'
import { deepStrictEqual, rejects, throws } from 'assert'
import tap from 'tap'
{
// asIndexedPairs with a synchronous stream
const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray()
deepStrictEqual(pairs, [
[0, 1],
[1, 2],
[2, 3]
])
const empty = await Readable.from([]).asIndexedPairs().toArray()
deepStrictEqual(empty, [])
}
{
// asIndexedPairs works an asynchronous streams
const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x)
const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray()
deepStrictEqual(pairs, [
[0, 1],
[1, 2],
[2, 3]
])
const empty = await asyncFrom([]).asIndexedPairs().toArray()
deepStrictEqual(empty, [])
}
{
// Does not enumerate an infinite stream
const infinite = () =>
Readable.from(
(async function* () {
while (true) yield 1
})()
)
const pairs = await infinite().asIndexedPairs().take(3).toArray()
deepStrictEqual(pairs, [
[0, 1],
[1, 1],
[2, 1]
])
const empty = await infinite().asIndexedPairs().take(0).toArray()
deepStrictEqual(empty, [])
}
{
// AbortSignal
await rejects(
async () => {
const ac = new AbortController()
const { signal } = ac
const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray()
ac.abort()
await p
},
{ name: 'AbortError' }
)
await rejects(async () => {
const signal = AbortSignal.abort()
await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray()
}, /AbortError/)
}
{
// Error cases
throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/)
throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/)
}
/* replacement start */
process.on('beforeExit', (code) => {
if (code === 0) {
tap.pass('test succeeded')
} else {
tap.fail(`test failed - exited code ${code}`)
}
})
/* replacement end */