Skip to content

Commit 388daf8

Browse files
committed
test(csv-parse): api ts conversion
1 parent e85beb3 commit 388daf8

10 files changed

Lines changed: 193 additions & 169 deletions
Lines changed: 1 addition & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,8 @@
11
import "should";
2-
import { generate } from "csv-generate";
3-
import { parse, Parser } from "../lib/index.js";
2+
import { parse } from "../lib/index.js";
43

54
describe("API arguments", function () {
6-
it("exports Parser class", function () {
7-
Parser.should.be.a.Function;
8-
});
9-
10-
describe("0 arg", function () {
11-
it("no arguments", function (next) {
12-
const records = [];
13-
const parser = parse();
14-
parser.on("readable", function () {
15-
let d;
16-
while ((d = this.read())) {
17-
records.push(d);
18-
}
19-
});
20-
parser.on("err", (err) => {
21-
next(err);
22-
});
23-
parser.on("end", () => {
24-
records.should.eql([
25-
["field_1", "field_2"],
26-
["value 1", "value 2"],
27-
]);
28-
next();
29-
});
30-
parser.write("field_1,field_2\nvalue 1,value 2");
31-
parser.end();
32-
});
33-
});
34-
35-
describe("1 arg", function () {
36-
it("callback:function; pipe data and get result in callback", function (next) {
37-
generate({ length: 2, seed: 1, columns: 2, fixed_size: true }).pipe(
38-
parse((err, records) => {
39-
records.should.eql([
40-
["OMH", "ONKCHhJmjadoA"],
41-
["D", "GeACHiN"],
42-
]);
43-
next(err);
44-
}),
45-
);
46-
});
47-
48-
it("options:object; write data and read stream", function (next) {
49-
const records = [];
50-
const parser = parse({ columns: true });
51-
parser.on("readable", function () {
52-
let d;
53-
while ((d = parser.read())) {
54-
records.push(d);
55-
}
56-
});
57-
parser.on("err", (err) => {
58-
next(err);
59-
});
60-
parser.on("end", () => {
61-
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
62-
next();
63-
});
64-
parser.write("field_1,field_2\nvalue 1,value 2");
65-
parser.end();
66-
});
67-
});
68-
695
describe("2 args", function () {
70-
it("data:string, options:object; read stream", function (next) {
71-
const records = [];
72-
const parser = parse("field_1,field_2\nvalue 1,value 2", {
73-
columns: true,
74-
});
75-
parser.on("readable", function () {
76-
let d;
77-
while ((d = parser.read())) {
78-
records.push(d);
79-
}
80-
});
81-
parser.on("err", (err) => {
82-
next(err);
83-
});
84-
parser.on("end", () => {
85-
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
86-
next();
87-
});
88-
});
89-
90-
it("options:object, callback:function; write data and get result in callback", function (next) {
91-
const parser = parse({ columns: true }, (err, records) => {
92-
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
93-
next(err);
94-
});
95-
parser.write("field_1,field_2\nvalue 1,value 2");
96-
parser.end();
97-
});
98-
99-
it("data:string, callback:function", function (next) {
100-
parse("value a,value b\nvalue 1,value 2", (err, records) => {
101-
records.should.eql([
102-
["value a", "value b"],
103-
["value 1", "value 2"],
104-
]);
105-
next(err);
106-
});
107-
});
108-
109-
it("data:buffer, callback:function", function (next) {
110-
parse(Buffer.from("value a,value b\nvalue 1,value 2"), (err, records) => {
111-
records.should.eql([
112-
["value a", "value b"],
113-
["value 1", "value 2"],
114-
]);
115-
next(err);
116-
});
117-
});
118-
1196
it("data:undefined, options:object", function () {
1207
(() => {
1218
parse(undefined, {});
@@ -155,28 +42,6 @@ describe("API arguments", function () {
15542
});
15643

15744
describe("3 args", function () {
158-
it("data:string, options:object, callback:function", function (next) {
159-
parse(
160-
"field_1,field_2\nvalue 1,value 2",
161-
{ columns: true },
162-
(err, records) => {
163-
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
164-
next(err);
165-
},
166-
);
167-
});
168-
169-
it("data:buffer, options:object, callback:function", function (next) {
170-
parse(
171-
Buffer.from("field_1,field_2\nvalue 1,value 2"),
172-
{ columns: true },
173-
(err, records) => {
174-
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
175-
next(err);
176-
},
177-
);
178-
});
179-
18045
it("data:undefined, options:object, callback:function", function () {
18146
(() => {
18247
parse(undefined, { columns: true }, () => {});
@@ -185,14 +50,5 @@ describe("API arguments", function () {
18550
code: "CSV_INVALID_ARGUMENT",
18651
});
18752
});
188-
189-
it("data:string, options:object, callback:undefined", function () {
190-
(() => {
191-
parse("field_1,field_2\nvalue 1,value 2", { columns: true }, undefined);
192-
}).should.throw({
193-
message: "Invalid argument: got undefined at index 2",
194-
code: "CSV_INVALID_ARGUMENT",
195-
});
196-
});
19753
});
19854
});
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import "should";
2+
import { generate } from "csv-generate";
3+
import { parse, Parser } from "../lib/index.js";
4+
5+
describe("API arguments", function () {
6+
it("exports Parser class", function () {
7+
Parser.should.be.a.Function;
8+
});
9+
10+
describe("0 arg", function () {
11+
it("no arguments", function (next) {
12+
const records: string[] = [];
13+
const parser = parse();
14+
parser.on("readable", function () {
15+
let d;
16+
while ((d = parser.read())) {
17+
records.push(d);
18+
}
19+
});
20+
parser.on("err", (err) => {
21+
next(err);
22+
});
23+
parser.on("end", () => {
24+
records.should.eql([
25+
["field_1", "field_2"],
26+
["value 1", "value 2"],
27+
]);
28+
next();
29+
});
30+
parser.write("field_1,field_2\nvalue 1,value 2");
31+
parser.end();
32+
});
33+
});
34+
35+
describe("1 arg", function () {
36+
it("callback:function; pipe data and get result in callback", function (next) {
37+
generate({ length: 2, seed: 1, columns: 2, fixed_size: true }).pipe(
38+
parse((err, records) => {
39+
records.should.eql([
40+
["OMH", "ONKCHhJmjadoA"],
41+
["D", "GeACHiN"],
42+
]);
43+
next(err);
44+
}),
45+
);
46+
});
47+
48+
it("options:object; write data and read stream", function (next) {
49+
const records: string[] = [];
50+
const parser = parse({ columns: true });
51+
parser.on("readable", function () {
52+
let d;
53+
while ((d = parser.read())) {
54+
records.push(d);
55+
}
56+
});
57+
parser.on("err", (err) => {
58+
next(err);
59+
});
60+
parser.on("end", () => {
61+
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
62+
next();
63+
});
64+
parser.write("field_1,field_2\nvalue 1,value 2");
65+
parser.end();
66+
});
67+
});
68+
69+
describe("2 args", function () {
70+
it("data:string, options:object; read stream", function (next) {
71+
const records: string[] = [];
72+
const parser = parse("field_1,field_2\nvalue 1,value 2", {
73+
columns: true,
74+
});
75+
parser.on("readable", function () {
76+
let d;
77+
while ((d = parser.read())) {
78+
records.push(d);
79+
}
80+
});
81+
parser.on("err", (err) => {
82+
next(err);
83+
});
84+
parser.on("end", () => {
85+
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
86+
next();
87+
});
88+
});
89+
90+
it("options:object, callback:function; write data and get result in callback", function (next) {
91+
const parser = parse({ columns: true }, (err, records) => {
92+
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
93+
next(err);
94+
});
95+
parser.write("field_1,field_2\nvalue 1,value 2");
96+
parser.end();
97+
});
98+
99+
it("data:string, callback:function", function (next) {
100+
parse("value a,value b\nvalue 1,value 2", (err, records) => {
101+
records.should.eql([
102+
["value a", "value b"],
103+
["value 1", "value 2"],
104+
]);
105+
next(err);
106+
});
107+
});
108+
109+
it("data:buffer, callback:function", function (next) {
110+
parse(Buffer.from("value a,value b\nvalue 1,value 2"), (err, records) => {
111+
records.should.eql([
112+
["value a", "value b"],
113+
["value 1", "value 2"],
114+
]);
115+
next(err);
116+
});
117+
});
118+
});
119+
120+
describe("3 args", function () {
121+
it("data:string, options:object, callback:function", function (next) {
122+
parse(
123+
"field_1,field_2\nvalue 1,value 2",
124+
{ columns: true },
125+
(err, records) => {
126+
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
127+
next(err);
128+
},
129+
);
130+
});
131+
132+
it("data:buffer, options:object, callback:function", function (next) {
133+
parse(
134+
Buffer.from("field_1,field_2\nvalue 1,value 2"),
135+
{ columns: true },
136+
(err, records) => {
137+
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
138+
next(err);
139+
},
140+
);
141+
});
142+
143+
it("data:string, options:object, callback:undefined", function () {
144+
(() => {
145+
parse("field_1,field_2\nvalue 1,value 2", { columns: true }, undefined);
146+
}).should.throw({
147+
message: "Invalid argument: got undefined at index 2",
148+
code: "CSV_INVALID_ARGUMENT",
149+
});
150+
});
151+
});
152+
});
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import "should";
2-
import { parse, CsvError } from "../lib/index.js";
2+
import { parse, CsvError, normalize_options } from "../lib/index.js";
33
import { assert_error } from "./api.assert_error.js";
44

55
describe("API error", function () {
66
it("set code", function () {
7-
const err = new CsvError("MY_CODE", ["a", "b", "c"]);
8-
err.code.should.eql("MY_CODE");
7+
const err = new CsvError("CSV_UNKNOWN_ERROR", ["a", "b", "c"]);
8+
err.code.should.eql("CSV_UNKNOWN_ERROR");
99
});
1010

1111
it("convert array message to string", function () {
12-
const err = new CsvError("MY_CODE", ["a", "b", "c"]);
12+
const err = new CsvError("CSV_UNKNOWN_ERROR", ["a", "b", "c"]);
1313
err.message.should.eql("a b c");
1414
});
1515

1616
it("set additional context information", function () {
17-
const err = new CsvError("MY_CODE", "msg", {}, { a: 1, b: 2 });
17+
const options = normalize_options({});
18+
const err = new CsvError("CSV_UNKNOWN_ERROR", "msg", options, {
19+
a: 1,
20+
b: 2,
21+
});
1822
err.a.should.eql(1);
1923
err.b.should.eql(2);
2024
});
2125

2226
it("errors are enriched by context", function () {
2327
parse('a"b', (err) => {
28+
if (!err) throw Error("Invalid assertion");
2429
assert_error(err, {
2530
message: /Invalid Opening Quote/,
2631
code: "INVALID_OPENING_QUOTE",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { parse } from "../lib/index.js";
44
describe("API info", function () {
55
it("is exported in the callback on error", function (next) {
66
parse("1,2,3\na,b,", (err, records, info) => {
7+
if (!info) return next(Error("Invalid assessment"));
78
info.should.eql({
89
bytes: 10,
910
columns: false,
@@ -19,6 +20,7 @@ describe("API info", function () {
1920

2021
it("is exported in the callback on success", function (next) {
2122
parse("1,2,3\na,b,c", (err, records, info) => {
23+
if (!info) return next(Error("Invalid assessment"));
2224
info.should.eql({
2325
bytes: 11,
2426
columns: false,
@@ -34,6 +36,7 @@ describe("API info", function () {
3436

3537
it("discovered columns are included", function (next) {
3638
parse("a,b,c\n1,2,3", { columns: true }, (err, records, info) => {
39+
if (!info) return next(Error("Invalid assessment"));
3740
info.should.eql({
3841
bytes: 11,
3942
comment_lines: 0,
@@ -49,6 +52,7 @@ describe("API info", function () {
4952

5053
it("with multiline records", function (next) {
5154
parse('a,b,c\nd,"e\n",f\ng,h,i', (err, records, info) => {
55+
if (!info) return next(Error("Invalid assessment"));
5256
info.should.eql({
5357
bytes: 20,
5458
columns: false,

0 commit comments

Comments
 (0)