This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (142 loc) · 5.85 KB
/
index.js
File metadata and controls
166 lines (142 loc) · 5.85 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
if (typeof window != 'object') {
var glob = require('' + 'glob');
var path = require('' + 'path');
}
module.exports = jsonSchemaTest;
function jsonSchemaTest(validators, opts) {
var assert = opts.assert || require('' + 'assert');
var _Promise;
if (opts.async) {
_Promise = opts.Promise || Promise;
if (!_Promise) throw new Error('async mode requires Promise support');
}
skipOrOnly(opts, describe)(opts.description || 'JSON schema tests', function() {
if (opts.timeout) this.timeout(opts.timeout);
for (var suiteName in opts.suites)
addTests(suiteName, opts.suites[suiteName]);
});
function addTests(suiteName, filesOrPath) {
describe(suiteName, function() {
var files = Array.isArray(filesOrPath)
? filesOrPath
: getTestFiles(filesOrPath);
files.forEach(function (file) {
var filter = {
skip: getFileFilter(file, 'skip'),
only: getFileFilter(file, 'only')
}
skipOrOnly(filter, describe)(file.name, function() {
if (file.test) {
var testSets = file.test;
} else if (file.path) {
var testPath = file.path
, testDir = path.dirname(testPath);
var testSets = require(testPath);
}
testSets.forEach(function (testSet) {
skipOrOnly(testSet, describe)(testSet.description, function() {
if (Array.isArray(testSet.schemas))
testSet.schemas.forEach(function (schema, i) {
var descr = schema.description || schema.id || schema.$ref || ('#' + i);
describe('schema ' + descr, function() {
testSchema(schema);
});
});
else
testSchema(testSet.schema);
function testSchema(schema) {
testSet.tests.forEach(function (test) {
skipOrOnly(test, it)(test.description, function() {
if (Array.isArray(validators)) {
if (opts.async) return _Promise.all(validators.map(doTest));
else validators.forEach(doTest);
} else {
return doTest(validators)
}
});
function doTest(validator) {
var data;
if (test.dataFile) {
var dataFile = path.resolve(testDir || '', test.dataFile);
data = require(dataFile);
} else {
data = test.data;
}
var valid = validator.validate(schema, data);
if (opts.async && typeof valid == 'object' && typeof valid.then == 'function') {
return valid.then(
function(_valid) { testResults(_valid, null) },
function(err) {
if (err.errors) testResults(false, err.errors);
else testException(err);
}
);
} else {
testResults(valid, validator.errors);
}
function testResults(valid, errors) {
if (opts.asyncValid == 'data' && test.valid === true)
valid = valid === data;
var passed = valid === test.valid;
if (!passed && opts.log !== false)
console.log('result:', valid, '\nexpected: ', test.valid, '\nerrors:', validator.errors);
if (valid) assert(!errors || errors.length == 0);
else assert(errors.length > 0);
suiteHooks(passed, valid, errors);
assert.equal(valid, test.valid);
}
function testException(err) {
var passed = err.message == test.error;
if (!passed && opts.log !== false)
console.log('error:', err.message,
'\nexpected: ',
test.valid ? 'valid'
: test.valid === false ? 'invalid'
: 'error ' + test.error);
suiteHooks(passed);
assert.equal(err.message, test.error);
}
function suiteHooks(passed, valid, errors) {
var result = {
passed: passed,
validator: validator,
schema: schema,
data: data,
valid: valid,
expected: test.valid,
expectedError: test.error,
errors: errors
};
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')
};
});
}
}