-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
55 lines (46 loc) · 1.72 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
import test from 'ava';
import stripFinalNewline from './index.js';
const invalidType = async (t, input) => {
t.throws(() => {
stripFinalNewline(input);
}, {
message: /Input must be/,
});
};
test('Invalid type - boolean', invalidType, true);
test('Invalid type - DataView', invalidType, new DataView(new ArrayBuffer(0)));
test('Invalid type - Uint16Array', invalidType, new Uint16Array(new ArrayBuffer(0)));
const assertStrip = async (t, convert, input, output) => {
t.deepEqual(stripFinalNewline(convert(input)), convert(output));
};
const inputs = [
'foo\n',
'foo\nbar\n',
'foo\n\n\n',
'foo\r\n',
'foo\r',
'foo\n\r\n',
];
const outputs = [
'foo',
'foo\nbar',
'foo\n\n',
'foo',
'foo\r',
'foo\n',
];
const identity = input => input;
test('string - LF', assertStrip, identity, inputs[0], outputs[0]);
test('string - LF text LF', assertStrip, identity, inputs[1], outputs[1]);
test('string - LF LF LF', assertStrip, identity, inputs[2], outputs[2]);
test('string - CR LF', assertStrip, identity, inputs[3], outputs[3]);
test('string - CR', assertStrip, identity, inputs[4], outputs[4]);
test('string - LF CR LF', assertStrip, identity, inputs[5], outputs[5]);
const textEncoder = new TextEncoder();
const toUint8Array = textEncoder.encode.bind(textEncoder);
test('Uint8Array - LF', assertStrip, toUint8Array, inputs[0], outputs[0]);
test('Uint8Array - LF text LF', assertStrip, toUint8Array, inputs[1], outputs[1]);
test('Uint8Array - LF LF LF', assertStrip, toUint8Array, inputs[2], outputs[2]);
test('Uint8Array - CR LF', assertStrip, toUint8Array, inputs[3], outputs[3]);
test('Uint8Array - CR', assertStrip, toUint8Array, inputs[4], outputs[4]);
test('Uint8Array - LF CR LF', assertStrip, toUint8Array, inputs[5], outputs[5]);