forked from SheetJS/js-word
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
69 lines (64 loc) · 2.66 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
/* vim: set ts=2: */
var XLS;
var fs = require('fs'), assert = require('assert');
describe('source',function(){ it('should load', function(){ XLS = require('./'); });});
var files = (fs.existsSync('tests.lst') ? fs.readFileSync('tests.lst', 'utf-8').split("\n") : fs.readdirSync('test_files')).filter(function(x){return x.substr(-4)==".xls" || x.substr(-8)==".xls.b64";});
/* Excel enforces 31 character sheet limit, although technical file limit is 255 */
function fixsheetname(x) { return x.substr(0,31); }
function normalizecsv(x) { return x.replace(/\t/g,",").replace(/#{255}/g,"").replace(/"/g,"").replace(/[\n\r]+/g,"\n").replace(/\n*$/,""); }
function parsetest(x, wb) {
describe(x + ' should have all bits', function() {
var sname = './test_files/2011/' + x + '.sheetnames';
it('should have all sheets', function() {
wb.SheetNames.forEach(function(y) { assert(wb.Sheets[y], 'bad sheet ' + y); });
});
it('should have the right sheet names', fs.existsSync(sname) ? function() {
var file = fs.readFileSync(sname, 'utf-8');
var names = wb.SheetNames.map(fixsheetname).join("\n") + "\n";
assert.equal(names, file);
} : null);
});
describe(x + ' should generate CSV', function() {
wb.SheetNames.forEach(function(ws, i) {
it('#' + i + ' (' + ws + ')', function() {
if(wb.SSF) XLS.SSF.load_table(wb.SSF);
var csv = XLS.utils.make_csv(wb.Sheets[ws]);
});
});
});
describe(x + ' should generate JSON', function() {
wb.SheetNames.forEach(function(ws, i) {
it('#' + i + ' (' + ws + ')', function() {
if(wb.SSF) XLS.SSF.load_table(wb.SSF);
var json = XLS.utils.sheet_to_row_object_array(wb.Sheets[ws]);
});
});
});
describe(x + ' should generate formulae', function() {
wb.SheetNames.forEach(function(ws, i) {
it('#' + i + ' (' + ws + ')', function() {
if(wb.SSF) XLS.SSF.load_table(wb.SSF);
var json = XLS.utils.get_formulae(wb.Sheets[ws]);
});
});
});
describe(x + ' should generate correct output', function() {
wb.SheetNames.forEach(function(ws, i) {
var name = ('./test_files/' + x + '.' + i + '.csv');
it('#' + i + ' (' + ws + ')', fs.existsSync(name) ? function() {
var file = fs.readFileSync(name, 'utf-8');
if(wb.SSF) XLS.SSF.load_table(wb.SSF);
var csv = XLS.utils.make_csv(wb.Sheets[ws]);
assert.equal(normalizecsv(csv), normalizecsv(file), "CSV badness");
} : null);
});
});
}
describe('should parse test files', function() {
files.forEach(function(x) {
it(x, x.substr(-8) == ".pending" ? null : function() {
var wb = x.substr(-4) == ".b64" ? XLS.read(fs.readFileSync('./test_files/' + x, 'utf8'), {type: 'base64'}) : XLS.readFile('./test_files/' + x);
parsetest(x, wb);
});
});
});