forked from CyrusNuevoDia/CSV.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
140 lines (132 loc) · 4.87 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var CSV = require("./csv"),
assert = require("assert"),
deepEqual = require("deep-equal"),
fs = require("fs"),
sets = ["marriage_census", "worldbank"],
data = {
marriage_census: {},
worldbank: {}
};
sets.forEach(function(set) {
fs.readFile("./datasets/csv/" + set + ".csv", "utf8", function(err, res) {
data[set].csv = res;
});
fs.readFile("./datasets/json/" + set + ".json", "utf8", function(err, res) {
data[set].json = JSON.parse(res);
});
});
describe("CSV", function() {
describe("#parse()", function() {
it("should return nothing if no data", function() {
var expected = [],
actual = "";
assert.deepEqual(expected, new CSV(actual).parse());
});
it("should parse edge cases", function() {
var expected = [
[[1, 2, "3,4"]],
[[1, 2, "\"3,4\""]],
[[1, 2, "3\n4"]]
],
actual = [
'1,2,"3,4"',
'1,2,"""3,4"""',
'1,2,"3\n4"'
];
expected.map(function(result, index) {
assert.deepEqual(result, new CSV(actual[index]).parse());
});
});
it("should parse with no headers", function() {
var expected = [[1, 2, 3, 4], [5, 6, 7, 8]],
actual = '1,2,3,4\r\n5,6,7,8\r\n';
assert.deepEqual(expected, new CSV(actual).parse());
});
it("should parse with headers", function() {
var expected = [{ name: "Will", age: 32 }],
actual = "name,age\r\nWill,32\r\n";
assert.deepEqual(expected, new CSV(actual, { header: true }).parse());
});
it("should parse files", function() {
sets.forEach(function(set) {
assert.deepEqual(data[set].json, new CSV(data[set].csv, { header: true }).parse());
});
});
it("should parse with headers and cast", function() {
var expected = [{ name: "Will", age: 32, tel: "1009999" }],
actual = "name,age,tel\r\nWill,32,1009999\r\n";
assert.ok(deepEqual(expected, new CSV(actual, { header: true, cast: ["String", "Number", "String"] }).parse(), {strict: true}));
});
it("should parse with cast", function() {
var expected = [["123", 456], ["null", 0]],
actual = "123,456\r\n,\r\n";
assert.deepEqual(expected, new CSV(actual, { cast: ["String", "Number"] }).parse());
});
it("should parse with custom cast", function() {
var customFunc = function(val) { return val === null ? '' : String(val); },
expected = [["123", "456"], ["", "null"]],
actual = "123,456\r\n,\r\n";
assert.deepEqual(expected, new CSV(actual, { cast: [customFunc, "String"] }).parse());
});
it("should expose saved position", function () {
var csv = new CSV("1,2\r\n3,\"4"); // incomplete last line
csv.parse();
assert.equal(5, csv.linePosition);
});
});
describe("#encode()", function() {
it("should return an empty string if no data", function() {
var expected = "",
actual = [];
assert.deepEqual(expected, new CSV(actual).encode());
});
it("should encode edge cases", function() {
var expected = [
'1,2,"3,4"',
'1,2,"""3,4"""',
'1,2,"3\n4"',
'1,2,"3\n4"',
'1,2,"3\n4"'
],
actual = [
[[1, 2, "3,4"]],
[[1, 2, "\"3,4\""]],
[[1, 2, "3\n4"]],
[[1, 2, "3\n4"]],
[[1, 2, "3\n4"]]
];
expected.map(function(result, index) {
assert.deepEqual(result, new CSV(actual[index], { line: "\n" }).encode());
});
});
it("should encode with no headers", function() {
var expected = '1,2,3,4\r\n5,6,7,8',
actual = [[1, 2, 3, 4], [5, 6, 7, 8]];
assert.deepEqual(expected, new CSV(actual).encode());
});
it("should encode with headers", function() {
var expected = "\"name\",\"age\"\r\n\"Will\",32",
actual = [{ name: "Will", age: 32 }];
assert.deepEqual(expected, new CSV(actual, { header: true }).encode());
});
it("should encode files", function() {
var options = { header: true, lineDelimiter: "\n" };
sets.forEach(function(set) {
assert.deepEqual(data[set].csv, new CSV(data[set].json, options).encode());
});
});
it("should encode with cast", function() {
var options = { cast: ["String", "Primitive"] },
expected = "\"123\",\r\n\"null\",456",
actual = [["123", null], [null, "456"]];
assert.deepEqual(expected, new CSV(actual, options).encode());
});
it("should encode with custom cast", function() {
var customFunc = function(val) { return val === null ? '' : this.string(val); },
options = { cast: [customFunc, customFunc] },
expected = "\"123\",\r\n,\"456\"",
actual = [["123", null], [null, "456"]];
assert.deepEqual(expected, new CSV(actual, options).encode());
});
});
});