-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
83 lines (72 loc) · 1.9 KB
/
test.ts
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
import { assertEquals } from "https://deno.land/std@0.83.0/testing/asserts.ts";
import { readline } from "./mod.ts";
async function testFileReader(): Promise<Deno.Reader> {
const buf = new Deno.Buffer();
const enc = new TextEncoder();
await buf.write(enc.encode("1\n2"));
await buf.write(enc.encode("\n3"));
await buf.write(enc.encode("\n4\n5\n6"));
return buf;
}
Deno.test({
name: "read lines from reader",
async fn() {
const lines = [];
for await (const line of readline(await testFileReader())) {
lines.push(line);
}
assertEquals(lines, [
new Uint8Array([49]),
new Uint8Array([50]),
new Uint8Array([51]),
new Uint8Array([52]),
new Uint8Array([53]),
new Uint8Array([54]),
]);
},
});
Deno.test({
name: "read lines from reader with custom separator",
async fn() {
const lines = [];
for await (const line of readline(await testFileReader(), {
separator: new Uint8Array([10, 51, 10]),
})) {
lines.push(line);
}
assertEquals(lines, [
new Uint8Array([49, 10, 50]),
new Uint8Array([52, 10, 53, 10, 54]),
]);
},
});
Deno.test({
name: "read lines from reader where separator is the first character",
async fn() {
const lines = [];
for await (const line of readline(await testFileReader(), {
separator: new Uint8Array([49]),
})) {
lines.push(line);
}
assertEquals(lines, [
new Uint8Array([]),
new Uint8Array([10, 50, 10, 51, 10, 52, 10, 53, 10, 54]),
]);
},
});
Deno.test({
name: "read lines from reader where separator is the last character",
async fn() {
const lines = [];
for await (const line of readline(await testFileReader(), {
separator: new Uint8Array([54]),
})) {
lines.push(line);
}
assertEquals(lines, [
new Uint8Array([49, 10, 50, 10, 51, 10, 52, 10, 53, 10]),
new Uint8Array([]),
]);
},
});