-
Notifications
You must be signed in to change notification settings - Fork 16
/
test-readable.js
92 lines (63 loc) · 2.28 KB
/
test-readable.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
'use strict'
const test = require('tape-promise').default(require('tape'))
const Stream = require('stream')
const DelayedStream = require('delayed-stream')
const streamToPromise = require('./')
test('readable/emtpy', async function (t) {
const [readable, promise] = Readable()
readable.emit('end')
const buffer = await promise
t.ok(Buffer.isBuffer(buffer), 'is buffer')
t.equal(buffer.length, 0, 'empty')
})
test('readable/data', async function (t) {
const [readable, promise] = Readable()
readable.emit('data', Buffer.from('foo'))
readable.emit('data', Buffer.from('bar'))
readable.emit('end')
const buffer = await promise
t.ok(Buffer.isBuffer(buffer), 'is buffer')
t.equal(buffer.toString(), 'foobar', 'concatenates data')
})
test('readable/buffers', async function (t) {
const [readable, promise] = Readable()
readable.emit('data', Buffer.from('foo'))
readable.emit('data', 'bar')
readable.emit('end')
const buffer = await promise
t.equal(buffer.toString(), 'foobar', 'concatenates data')
})
test('readable/ended', async function (t) {
const [readable] = Readable()
readable.readable = false
t.equal(await streamToPromise(readable), undefined, 'undefined result')
})
test('readable/paused', async function (t) {
const [readable] = Readable()
const delayed = DelayedStream.create(readable)
readable.emit('data', Buffer.from('foo'))
readable.emit('end')
const buffer = await streamToPromise(delayed)
t.equal(buffer.toString(), 'foo', 'resolves data')
})
test('readable/object', async function (t) {
const objectStream = new Stream.Readable({ objectMode: true })
objectStream._read = function noop () { }
const promise = streamToPromise(objectStream)
objectStream.emit('data', { foo: 'bar' })
objectStream.emit('data', { baz: 'qux' })
objectStream.emit('end')
t.deepEqual(await promise, [{ foo: 'bar' }, { baz: 'qux' }], 'returns array of data')
})
test('readable/error', async function (t) {
const [readable, promise] = Readable()
const err = new Error('oh no')
readable.emit('error', err)
return t.rejects(promise, /oh no/, 'rejects with error')
})
function Readable () {
const readable = new Stream.Readable()
readable._read = function noop () { }
const promise = streamToPromise(readable)
return [readable, promise]
}