-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
64 lines (56 loc) · 1.34 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
const { Readable, Writable } = require('stream');
const marinate = require('./index');
const outputs = [`
hello
`,
'123', `
`,
'456', `
`,
'789', `
`,
'Hello world', `
`,
'YOLO',`
`,
];
const stringToReadableStream = str => {
const file$ = new Readable({
read(size) {
if (!this.content) this.push(null)
else {
this.push(this.content.slice(0, size))
this.content = this.content.slice(size)
}
}
});
file$.push(Buffer.from(str, 'ascii'));
return file$;
}
const createRes = () => {
let outputPointer = 0;
return new Writable({
decodeStrings: false,
write(actual, encoding, next) {
const expected = outputs[outputPointer++]
if (actual instanceof Buffer) actual = actual.toString()
try {
console.assert(actual === expected)
} catch (err) {
console.log('Test failed.', { actual, expected })
}
next()
}
})
}
const template = marinate`
hello
${'123'}
${() => '456'}
${() => new Promise(r => setTimeout(r, 2000, '789'))}
${() => stringToReadableStream('Hello world')}
${() => new Promise(r => setTimeout(r, 2000, stringToReadableStream('YOLO')))}
`
template(createRes(), { debug: 'off' })
template(createRes(), { debug: 'off', stringToBufferThreshold: 0 })
template(createRes(), { debug: 'off', stringToBufferThreshold: 5 })