-
Notifications
You must be signed in to change notification settings - Fork 5
/
writer_test.ts
103 lines (88 loc) · 2.58 KB
/
writer_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { assertEquals } from "@std/assert/assert-equals";
import { Buffer } from "@std/io/buffer";
import { CSVWriter, writeCSV, writeCSVObjects } from "./writer.ts";
Deno.test({
name: "CSVWriter writes simple file",
async fn() {
const buf = new Buffer();
const writer = new CSVWriter(buf);
await writer.writeCell("a");
await writer.writeCell("b");
await writer.writeCell("c");
await writer.nextLine();
await writer.writeCell("1");
await writer.writeCell("2");
await writer.writeCell("3");
assertEquals(new TextDecoder().decode(buf.bytes()), "a,b,c\n1,2,3");
},
});
Deno.test({
name: "CSVWriter detects quotes",
async fn() {
const buf = new Buffer();
const writer = new CSVWriter(buf);
await writer.writeCell("a");
await writer.writeCell("b");
await writer.writeCell("c");
await writer.nextLine();
await writer.writeCell('1"2');
await writer.writeCell("2,3");
await writer.writeCell("3\n4");
assertEquals(
new TextDecoder().decode(buf.bytes()),
`a,b,c\n"1""2","2,3","3\n4"`,
);
},
});
Deno.test({
name: "CSVWriter works with async iterable",
async fn() {
const buf = new Buffer();
const writer = new CSVWriter(buf);
const asyncCell = async function* () {
const enc = new TextEncoder();
yield enc.encode("1");
yield enc.encode("\n");
yield enc.encode('"');
yield enc.encode(",");
yield enc.encode("2");
};
await writer.writeCell("a");
await writer.nextLine();
await writer.writeCell(asyncCell());
assertEquals(new TextDecoder().decode(buf.bytes()), `a\n"1\n"",2"`);
},
});
Deno.test({
name: "writeCSV works with different input",
async fn() {
const buf = new Buffer();
const enc = new TextEncoder();
const asyncCell = async function* (str: string) {
yield enc.encode(str);
};
const asyncRow = async function* () {
yield "1";
yield "2";
yield asyncCell("3");
};
const asyncRows = async function* () {
yield ["a", "b", asyncCell("c")];
yield asyncRow();
};
await writeCSV(buf, asyncRows());
assertEquals(new TextDecoder().decode(buf.bytes()), `a,b,"c"\n1,2,"3"`);
},
});
Deno.test({
name: "writeCSVObjects works objects",
async fn() {
const buf = new Buffer();
const asyncRows = async function* () {
yield { a: "1", b: "2", c: "3" };
yield { a: "4", b: "5", c: "6" };
};
await writeCSVObjects(buf, asyncRows(), { header: ["a", "b", "c"] });
assertEquals(new TextDecoder().decode(buf.bytes()), `a,b,c\n1,2,3\n4,5,6`);
},
});