-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathiterLinesFromReadable.test.ts
More file actions
85 lines (74 loc) · 2.26 KB
/
iterLinesFromReadable.test.ts
File metadata and controls
85 lines (74 loc) · 2.26 KB
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
import { Readable } from 'stream';
import { iterlinesFromReadable } from './iterLinesFromReadable';
import chalk from 'chalk';
async function toLines(input: Iterable<string>): Promise<string[]> {
const readable = Readable.from(input);
const lines = [];
for await (const line of iterlinesFromReadable(readable)) {
lines.push(line);
}
return lines;
}
test('single line', async () => {
expect(await toLines('')).toEqual(['']);
expect(await toLines('one')).toEqual(['one']);
});
test('unix newlines', async () => {
expect(await toLines('one\ntwo')).toEqual(['one', 'two']);
});
test('windows newlines', async () => {
expect(await toLines('one\r\ntwo')).toEqual(['one', 'two']);
});
test('leading and trailing newlines', async () => {
expect(await toLines('\none\n')).toEqual(['', 'one', '']);
});
test('line separated across yields', async () => {
function* generateLines() {
yield 'one\ntw';
yield 'o\nthr';
yield 'ee';
}
expect(await toLines(generateLines())).toEqual(['one', 'two', 'three']);
});
test('mixed', async () => {
expect(await toLines(['0\n', '1'])).toEqual(['0', '1']);
});
test('lines with ascii colors', async () => {
expect(await toLines(chalk.red('one\r\ntwo'))).toEqual([
chalk.red('one'),
chalk.red('two'),
]);
expect(await toLines(chalk.red('one\r') + '\ntwo')).toEqual([
chalk.red('one'),
'two',
]);
expect(await toLines(chalk.red('one\ntwo'))).toEqual([
chalk.red('one'),
chalk.red('two'),
]);
expect(await toLines(chalk.red('one') + '\ntwo')).toEqual([
chalk.red('one'),
'two',
]);
});
test('fuzz', async () => {
const chunks = [];
const lines = [];
let currentLine = '';
for (let i = 0; i < 5; i++) {
let chunk = '';
for (let j = 0; j < 5; j++) {
const character = Math.random() < 0.25 ? '\n' : i.toString();
if (character === '\n') {
lines.push(currentLine);
currentLine = '';
} else {
currentLine += character;
}
chunk += character;
}
chunks.push(chunk);
}
lines.push(currentLine);
expect(await toLines(chunks)).toEqual(lines);
});