-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathnormalise-input.spec.js
242 lines (190 loc) · 6.66 KB
/
normalise-input.spec.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'use strict'
/* eslint-env mocha */
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const normalise = require('../../src/files/normalise-input')
const { supportsFileReader } = require('../../src/supports')
const { Buffer } = require('buffer')
const all = require('async-iterator-all')
const pull = require('pull-stream')
const Readable2 = require('readable-stream-2')
const Readable3 = require('readable-stream')
const ReadableNode = require('stream').Readable
const globalThis = require('../../src/globalthis')
chai.use(dirtyChai)
const expect = chai.expect
const STRING = 'hello world'
const BUFFER = Buffer.from(STRING)
const ARRAY = Array.from(BUFFER)
const TYPEDARRAY = Uint8Array.from(ARRAY)
let BLOB
if (supportsFileReader) {
BLOB = new globalThis.Blob([
STRING
])
}
async function verifyNormalisation (input) {
expect(input.length).to.equal(1)
if (!input[0].content[Symbol.asyncIterator] && !input[0].content[Symbol.iterator]) {
chai.assert.fail('Content should have been an iterable or an async iterable')
}
expect(await all(input[0].content)).to.deep.equal([BUFFER])
expect(input[0].path).to.equal('')
}
async function testContent (input) {
const result = await all(normalise(input))
await verifyNormalisation(result)
}
function iterableOf (thing) {
return [thing]
}
function asyncIterableOf (thing) {
return (async function * () { // eslint-disable-line require-await
yield thing
}())
}
function pullStreamOf (thing) {
return pull.values([thing])
}
function readable2Of (thing) {
const stream = new Readable2({
objectMode: true,
read () {
this.push(thing)
this.push(null)
}
})
return stream
}
function readable3Of (thing) {
const stream = new Readable3({
objectMode: true,
read () {
this.push(thing)
this.push(null)
}
})
return stream
}
function readableNodeOf (thing) {
const stream = new ReadableNode({
objectMode: true,
read () {
this.push(thing)
this.push(null)
}
})
return stream
}
describe('normalise-input', function () {
function testInputType (content, name, isBytes) {
it(name, async function () {
await testContent(content)
})
if (isBytes) {
it(`Iterable<${name}>`, async function () {
await testContent(iterableOf(content))
})
it(`AsyncIterable<${name}>`, async function () {
await testContent(asyncIterableOf(content))
})
it(`PullStream<${name}>`, async function () {
await testContent(pullStreamOf(content))
})
it(`Readable2<${name}>`, async function () {
await testContent(readable2Of(content))
})
it(`Readable3<${name}>`, async function () {
await testContent(readable3Of(content))
})
it(`ReadableNode<${name}>`, async function () {
await testContent(readableNodeOf(content))
})
}
it(`{ path: '', content: ${name} }`, async function () {
await testContent({ path: '', content })
})
if (isBytes) {
it(`{ path: '', content: Iterable<${name}> }`, async function () {
await testContent({ path: '', content: iterableOf(content) })
})
it(`{ path: '', content: AsyncIterable<${name}> }`, async function () {
await testContent({ path: '', content: asyncIterableOf(content) })
})
it(`{ path: '', content: PullStream<${name}> }`, async function () {
await testContent({ path: '', content: pullStreamOf(content) })
})
it(`{ path: '', content: Readable2<${name}> }`, async function () {
await testContent({ path: '', content: readable2Of(content) })
})
it(`{ path: '', content: Readable3<${name}> }`, async function () {
await testContent({ path: '', content: readable3Of(content) })
})
it(`{ path: '', content: ReadableNode<${name}> }`, async function () {
await testContent({ path: '', content: readableNodeOf(content) })
})
}
it(`Iterable<{ path: '', content: ${name} }`, async function () {
await testContent(iterableOf({ path: '', content }))
})
it(`AsyncIterable<{ path: '', content: ${name} }`, async function () {
await testContent(asyncIterableOf({ path: '', content }))
})
it(`PullStream<{ path: '', content: ${name} }`, async function () {
await testContent(pullStreamOf({ path: '', content }))
})
it(`Readable2<{ path: '', content: ${name} }`, async function () {
await testContent(readable2Of({ path: '', content }))
})
it(`Readable3<{ path: '', content: ${name} }`, async function () {
await testContent(readable3Of({ path: '', content }))
})
it(`ReadableNode<{ path: '', content: ${name} }`, async function () {
await testContent(readableNodeOf({ path: '', content }))
})
if (isBytes) {
it(`Iterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: iterableOf(content) }))
})
it(`Iterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: asyncIterableOf(content) }))
})
it(`AsyncIterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: iterableOf(content) }))
})
it(`AsyncIterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: asyncIterableOf(content) }))
})
it(`PullStream<{ path: '', content: PullStream<${name}> }>`, async function () {
await testContent(pullStreamOf({ path: '', content: pullStreamOf(content) }))
})
it(`Readable2<{ path: '', content: Readable2<${name}> }>`, async function () {
await testContent(readable2Of({ path: '', content: readable2Of(content) }))
})
it(`Readable3<{ path: '', content: Readable3<${name}> }>`, async function () {
await testContent(readable3Of({ path: '', content: readable3Of(content) }))
})
it(`ReadableNode<{ path: '', content: Readable3<${name}> }>`, async function () {
await testContent(readableNodeOf({ path: '', content: readableNodeOf(content) }))
})
}
}
describe('String', () => {
testInputType(STRING, 'String', false)
})
describe('Buffer', () => {
testInputType(BUFFER, 'Buffer', true)
})
describe('Blob', () => {
if (!supportsFileReader) {
return
}
testInputType(BLOB, 'Blob', false)
})
describe('Iterable<Number>', () => {
testInputType(ARRAY, 'Iterable<Number>', false)
})
describe('TypedArray', () => {
testInputType(TYPEDARRAY, 'TypedArray', true)
})
})