-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (119 loc) · 4.73 KB
/
index.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
141
142
143
'use strict';
var glob = require('glob');
var path = require('path');
var assert = require('assert');
var Ajv = require('ajv');
module.exports = jsonScriptTest;
var ajv = Ajv({ allErrors: true, v5: true });
ajv.addSchema(require('jsonscript/schema/schema.json'));
var validate = ajv.compile(require('jsonscript-test-suite/test_suite_schema.json'));
function jsonScriptTest(jsInterpreters, opts) {
skipOrOnly(opts, describe)(opts.description || 'JSONScript tests', function() {
if (opts.timeout) this.timeout(opts.timeout);
for (var suiteName in opts.suites)
addTests(suiteName, opts.suites[suiteName]);
});
function addTests(suiteName, testsPath) {
describe(suiteName, function() {
var files = getTestFiles(testsPath);
files.forEach(function (file) {
var filter = {
skip: getFileFilter(file, 'skip'),
only: getFileFilter(file, 'only')
};
skipOrOnly(filter, describe)(file.name, function() {
var testDir = path.dirname(file.path);
var testSuite = require(file.path);
var valid = validate(testSuite);
if (!valid) console.error('Error validating', file.name, '\nErrors:\n', validate.errors);
assert(valid);
testSuite.forEach(function (testSet) {
skipOrOnly(testSet, describe)(testSet.description, function() {
testSet.tests.forEach(function (test) {
skipOrOnly(test, it)(test.description, function() {
return Array.isArray(jsInterpreters)
? Promise.all(jsInterpreters.map(doTest))
: doTest(jsInterpreters);
});
function doTest(js) {
var data = test.data === undefined ? testSet.data : test.data;
var script = test.script === undefined ? testSet.script : test.script;
return js.evaluate(script, data)
.then(testResult, testException);
function testResult(res) {
if (test.result !== undefined) {
var method;
if (typeof res == 'object' && res !== null) {
method = 'deepStrictEqual';
res = JSON.parse(JSON.stringify(res));
} else {
method = 'strictEqual';
}
try {
assert[method](res, test.result);
suiteHooks(true, res);
} catch(e) {
suiteHooks(false, res);
throw e;
}
} else {
suiteHooks(false, res);
throw new Error('should have failed');
}
}
function testException(err) {
if (test.result !== undefined) {
suiteHooks(false, undefined, err);
throw err;
} else {
try {
if (test.error !== true)
assert.equal(err.message, test.error);
suiteHooks(true, undefined, err);
} catch(e) {
suiteHooks(false, undefined, err);
throw e;
}
}
}
function suiteHooks(passed, result, error) {
var result = {
passed: passed,
jsInterpreter: js,
script: script,
data: data,
test: test,
result: result,
error: error
};
if (opts.afterEach) opts.afterEach(result);
if (opts.afterError && !passed) opts.afterError(result);
}
}
});
});
});
});
});
});
function getFileFilter(file, property) {
var filter = opts[property];
return Array.isArray(filter) && filter.indexOf(file.name) >= 0;
}
}
function skipOrOnly(filter, func) {
return filter.only === true ? func.only : filter.skip === true ? func.skip : func;
}
function getTestFiles(testsPath) {
var files = glob.sync(testsPath, { cwd: opts.cwd });
return files.map(function (file) {
var match = file.match(/(\w+\/)\w+\.json/)
var folder = match ? match[1] : '';
if (opts.hideFolder && folder == opts.hideFolder) folder = '';
return {
path: path.join(opts.cwd, file),
name: folder + path.basename(file, '.json')
};
});
}
}