Skip to content

Commit 8b27fdb

Browse files
committed
validate test suite, add support for test hooks
1 parent 5be989c commit 8b27fdb

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

index.js

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
var glob = require('glob');
44
var path = require('path');
55
var assert = require('assert');
6+
var Ajv = require('ajv');
67

78
module.exports = jsonScriptTest;
89

10+
var ajv = Ajv({ allErrors: true });
11+
ajv.addSchema(require('jsonscript/schema/schema.json'));
12+
var validate = ajv.compile(require('jsonscript-test-suite/test_suite_schema.json'));
13+
914

1015
function jsonScriptTest(jsInterpreters, opts) {
11-
skipOrOnly(opts, describe)(opts.description || 'JSON schema tests', function() {
16+
skipOrOnly(opts, describe)(opts.description || 'JSONScript tests', function() {
1217
if (opts.timeout) this.timeout(opts.timeout);
1318
for (var suiteName in opts.suites)
1419
addTests(suiteName, opts.suites[suiteName]);
@@ -23,12 +28,17 @@ function jsonScriptTest(jsInterpreters, opts) {
2328
var filter = {
2429
skip: getFileFilter(file, 'skip'),
2530
only: getFileFilter(file, 'only')
26-
}
31+
};
2732

2833
skipOrOnly(filter, describe)(file.name, function() {
2934
var testDir = path.dirname(file.path);
3035
var testSuite = require(file.path);
3136

37+
var valid = validate(testSuite);
38+
if (!valid) console.error('Error validating', file.name, '\nErrors:\n', validate.errors);
39+
assert(valid);
40+
41+
3242
testSuite.forEach(function (testSet) {
3343
skipOrOnly(testSet, describe)(testSet.description, function() {
3444
testSet.tests.forEach(function (test) {
@@ -48,53 +58,44 @@ function jsonScriptTest(jsInterpreters, opts) {
4858
function testResult(res) {
4959
if (test.result) {
5060
var method = typeof res == 'object' ? 'deepStrictEqual' : 'strictEqual';
51-
try { assert[method](res, test.result); }
52-
catch(e) { throw e; }
61+
try {
62+
assert[method](res, test.result);
63+
suiteHooks(true, res);
64+
} catch(e) {
65+
suiteHooks(false, res);
66+
throw e;
67+
}
5368
} else {
69+
suiteHooks(false, res);
5470
throw new Error('should have failed');
5571
}
56-
57-
58-
// var passed = valid === test.valid;
59-
// if (!passed && opts.log !== false)
60-
// console.log('result:', valid, '\nexpected: ', test.valid, '\nerrors:', validator.errors);
61-
// if (valid) assert(!errors || errors.length == 0);
62-
// else assert(errors.length > 0);
63-
64-
// suiteHooks(passed, valid, errors);
65-
// assert.equal(valid, test.valid);
6672
}
6773

6874
function testException(err) {
6975
if (test.result) {
76+
suiteHooks(false, undefined, err);
7077
throw err;
7178
} else {
72-
try { assert.equal(err.message, test.error); }
73-
catch(e) { throw e; }
79+
try {
80+
if (test.error !== true)
81+
assert.equal(err.message, test.error);
82+
suiteHooks(true, undefined, err);
83+
} catch(e) {
84+
suiteHooks(false, undefined, err);
85+
throw e;
86+
}
7487
}
75-
76-
// var passed = err.message == test.error;
77-
// if (!passed && opts.log !== false)
78-
// console.log('error:', err.message,
79-
// '\nexpected: ',
80-
// test.valid ? 'valid'
81-
// : test.valid === false ? 'invalid'
82-
// : 'error ' + test.error);
83-
84-
// suiteHooks(passed);
85-
// assert.equal(err.message, test.error);
8688
}
8789

88-
function suiteHooks(passed, valid, errors) {
90+
function suiteHooks(passed, result, error) {
8991
var result = {
9092
passed: passed,
91-
validator: validator,
92-
schema: schema,
93+
jsInterpreter: js,
94+
script: script,
9395
data: data,
94-
valid: valid,
95-
expected: test.valid,
96-
expectedError: test.error,
97-
errors: errors
96+
test: test,
97+
result: result,
98+
error: error
9899
};
99100

100101
if (opts.afterEach) opts.afterEach(result);

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"homepage": "https://github.com/JSONScript/jsonscript-test#readme",
2323
"dependencies": {
2424
"ajv": "^3.7.0",
25+
"glob": "^7.0.0",
26+
"jsonscript": "^0.1.2",
2527
"jsonscript-test-suite": "^0.1.0"
2628
}
2729
}

0 commit comments

Comments
 (0)