-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcross-test-python.js
More file actions
81 lines (69 loc) · 2.71 KB
/
Copy pathcross-test-python.js
File metadata and controls
81 lines (69 loc) · 2.71 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
const nsv = require('./index');
const { execSync } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
// Test cases
const tests = [
{ name: 'simple table', input: 'a\nb\n\nc\nd\n' },
{ name: 'four newlines', input: '\n\n\n\n' },
{ name: 'double newline then first', input: '\n\nfirst\n' },
{ name: 'first, triple newline, second', input: 'first\n\n\n\nsecond\n' },
{ name: 'empty cell token', input: '\\\n' },
{ name: 'text with dangling backslash', input: 'text\\\n' },
{ name: 'unknown escape sequence', input: 'test\\x41\n' },
{ name: 'spec example', input: 'Tab\\tseparated\\tvalues\\n(would be left as-is normally)\n' },
{ name: 'mixed escapes', input: 'Roses are red\\nViolets are blue\\nThis may be pain\\nBut CSV would be, too\n' },
{ name: 'empty rows', input: '\n\na\n\n\n' },
{ name: 'trailing newlines', input: 'a\n\n\n' },
{ name: 'complex example', input: 'first\nrow\n\nsecond\nrow\n\nmissing ->\n\\\n<- missing\n\nRoses are red\\nViolets are blue\\nThis may be pain\\nBut CSV would be, too\nTab\\tseparated\\tvalues\\n(would be left as-is normally)\nNot a newline: \\\\n\n' },
];
console.log('Cross-testing JS implementation against Python (PyPI 0.2.3)\n');
console.log('='.repeat(60) + '\n');
let passCount = 0;
let failCount = 0;
// Create temp file for input
const tmpFile = path.join(os.tmpdir(), 'nsv-test-input.txt');
for (const test of tests) {
// Write test input to temp file
fs.writeFileSync(tmpFile, test.input);
// Run Python implementation from PyPI
let pythonResult;
try {
const pythonCode = `import sys; import json; import nsv; f = open(sys.argv[1]); result = nsv.load(f); f.close(); print(json.dumps(result))`;
const output = execSync(`python3 -c ${JSON.stringify(pythonCode)} ${JSON.stringify(tmpFile)}`, {
encoding: 'utf8'
});
pythonResult = JSON.parse(output.trim());
} catch (e) {
console.error(`✗ ${test.name}`);
console.error(` Error: ${e.message}`);
failCount++;
continue;
}
// Run JS implementation
const jsResult = nsv.parse(test.input);
// Compare
const match = JSON.stringify(jsResult) === JSON.stringify(pythonResult);
if (match) {
console.log(`✓ ${test.name}`);
passCount++;
} else {
console.log(`✗ ${test.name}`);
console.log(` Input: ${JSON.stringify(test.input)}`);
console.log(` Python: ${JSON.stringify(pythonResult)}`);
console.log(` JS: ${JSON.stringify(jsResult)}`);
failCount++;
}
}
// Cleanup
try {
fs.unlinkSync(tmpFile);
} catch (e) {
// Ignore cleanup errors
}
console.log('\n' + '='.repeat(60));
console.log(`Results: ${passCount} passed, ${failCount} failed`);
if (failCount > 0) {
process.exit(1);
}