This repository has been archived by the owner on May 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.ts
109 lines (107 loc) · 2.66 KB
/
mod_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
104
105
106
107
108
109
import { test, runIfMain } from "https://deno.land/[email protected]/testing/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { parseFile } from "./mod.ts";
import { join } from "https://deno.land/[email protected]/fs/path.ts";
const testCases = [
{
name: "simple",
file: "simple.csv",
header: false,
result: [["a", "b", "c"]]
},
{
name: "multiline",
file: "multiline.csv",
header: false,
result: [["a", "b", "c"], ["e", "f", "g"]]
},
{
name: "header mapping boolean",
file: "multiline.csv",
header: true,
result: [{ a: "e", b: "f", c: "g" }]
},
{
name: "header mapping array",
file: "multiline.csv",
header: ["this", "is", "sparta"],
result: [
{ this: "a", is: "b", sparta: "c" },
{ this: "e", is: "f", sparta: "g" }
]
},
{
name: "header mapping object",
file: "multiline.csv",
header: [{ name: "this" }, { name: "is" }, { name: "sparta" }],
result: [
{ this: "a", is: "b", sparta: "c" },
{ this: "e", is: "f", sparta: "g" }
]
},
{
name: "header mapping parse entry",
file: "multiline.csv",
header: [
{
name: "this",
parse: (e: string): string => {
return `b${e}$$`;
}
},
{
name: "is",
parse: (e: string): number => {
return e.length;
}
},
{
name: "sparta",
parse: (e: string): unknown => {
return { bim: `boom-${e}` };
}
}
],
result: [
{ this: "ba$$", is: 1, sparta: { bim: `boom-c` } },
{ this: "be$$", is: 1, sparta: { bim: `boom-g` } }
]
},
{
name: "multiline parse",
file: "multiline.csv",
parse: (e: string[]): unknown => {
return { super: e[0], street: e[1], fighter: e[2] };
},
header: false,
result: [
{ super: "a", street: "b", fighter: "c" },
{ super: "e", street: "f", fighter: "g" }
]
},
{
name: "header mapping object parseline",
file: "multiline.csv",
header: [{ name: "this" }, { name: "is" }, { name: "sparta" }],
parse: (e: Record<string, unknown>): unknown => {
return { super: e.this, street: e.is, fighter: e.sparta };
},
result: [
{ super: "a", street: "b", fighter: "c" },
{ super: "e", street: "f", fighter: "g" }
]
}
];
for (const testCase of testCases) {
test({
name: `[easyCSV] ${testCase.name}`,
async fn(): Promise<void> {
const r = await parseFile(join("test_data", testCase.file), {
header: testCase.header,
parse: testCase.parse
});
assertEquals(r, testCase.result);
}
});
}
runIfMain(import.meta);