-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
100 lines (88 loc) · 3.91 KB
/
Copy pathtest.js
File metadata and controls
100 lines (88 loc) · 3.91 KB
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
/* jshint esversion: 6 */
const { greenBright, red, redBright, yellow } = require("chalk");
const { escapeSpecialCharacters, stringOrNumberToString, makeCsvList } = require("./index");
let testNb = 0;
let testShot = 0;
let testFailures = 0;
function tr(str) {
return str.replace(/\r/g, "\\r").replace(/\n/g, "\\n");
}
function testEscapeSpecialCharacters(inStr, outStr, shouldEnclose, separator) {
testNb++;
console.log(`test step ${testNb} with: ${tr(inStr)}/${tr(outStr)}/${shouldEnclose}/${separator}`);
const result = escapeSpecialCharacters(inStr, separator);
if (result.result !== outStr || result.shouldEnclose !== shouldEnclose) {
console.log(red(` failure: got unexpected {result: ${tr(result.result)}, shouldEnclose: ${result.shouldEnclose}}`));
testFailures++;
}
}
console.log(yellow(`*** SHOT #${++testShot}: test "escapeSpecialCharacters"`));
testEscapeSpecialCharacters("hello", "hello", false);
testEscapeSpecialCharacters('"hello"', '""hello""', true);
testEscapeSpecialCharacters("hello, world", "hello, world", true);
testEscapeSpecialCharacters("hello, world", "hello, world", false, ";");
testEscapeSpecialCharacters("hello; world", "hello; world", true, ";");
testEscapeSpecialCharacters("hello; world", "hello; world", true, ";+");
testEscapeSpecialCharacters("hello; world", "hello; world", false, "+;");
testEscapeSpecialCharacters("hello; world", "hello; world", false, "+;");
testEscapeSpecialCharacters("hello world\r", "hello world\r", true);
testEscapeSpecialCharacters("hello world\r\n", "hello world\r\n", true);
function testStringOrNumberToString(input, expectedOutput, radix) {
testNb++;
console.log(`test step ${testNb} with "${input}", radix "${radix}"`);
const result = stringOrNumberToString(input, radix);
if (result !== expectedOutput) {
console.log(red(` failure: expected "${expectedOutput}", got "${result}"`));
testFailures++;
}
}
console.log(yellow(`*** SHOT #${++testShot}: test "stringOrNumberToString"`));
testStringOrNumberToString("hello", "hello");
testStringOrNumberToString(2, "2");
testStringOrNumberToString(NaN, "NaN");
testStringOrNumberToString(22, "16", 16);
testStringOrNumberToString({}, null);
testStringOrNumberToString(null, null);
testStringOrNumberToString(undefined, null);
function compareArrayLists(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
let outcome = true;
arr1.forEach((line, index) => {
if (arr2[index] !== line) {
outcome = false;
}
});
return outcome;
}
let headers = ["a", "b", "c"];
let objects = [
{ a: "A1", b: "B1", c: "C1" },
{ a: "A2", b: "B2", c: "C2" },
{ a: "A3, Alice", b: "B3, Bob", c: "C3, Cathy" },
];
console.log(yellow(`*** SHOT #${++testShot}: test "makeCsvList"`));
testNb++;
let expectedListOfStrings = ["a,b,c", "A1,B1,C1", "A2,B2,C2", '"A3, Alice","B3, Bob","C3, Cathy"'];
console.log(`test step ${testNb} call with no options`);
if (!compareArrayLists(makeCsvList(headers, objects), expectedListOfStrings)) {
console.log(red(" failure expecting:"), expectedListOfStrings);
}
testNb++;
expectedListOfStrings = ['"a","b","c"', '"A1","B1","C1"', '"A2","B2","C2"', '"A3, Alice","B3, Bob","C3, Cathy"'];
console.log(`test step ${testNb} call with fullDQuote`);
if (!compareArrayLists(makeCsvList(headers, objects, { fullDQuote: true }), expectedListOfStrings)) {
console.log(red(" failure expecting:"), expectedListOfStrings);
}
testNb++;
expectedListOfStrings = ["a;b;c", "A1;B1;C1", "A2;B2;C2", "A3, Alice;B3, Bob;C3, Cathy"];
console.log(`test step ${testNb} call with different separator`);
if (!compareArrayLists(makeCsvList(headers, objects, { separator: ";+" }), expectedListOfStrings)) {
console.log(red(" failure expecting:"), expectedListOfStrings);
console.log(makeCsvList(headers, objects, { separator: ";+" }));
}
console.log(
"End of tests. Status:",
testFailures > 0 ? redBright(`FAIL with ${testFailures} error${testFailures > 2 ? "s" : ""}`) : greenBright(`PASS`)
);