use glob in test
parent
d784d95959
commit
dcf20e93dc
|
@ -1,3 +1,3 @@
|
|||
[submodule "JSON-Schema-Test-Suite"]
|
||||
path = JSON-Schema-Test-Suite
|
||||
path = spec/JSON-Schema-Test-Suite
|
||||
url = https://github.com/epoberezkin/JSON-Schema-Test-Suite
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
function (data, dataPath) {
|
||||
'use strict';
|
||||
|
||||
{{? it.resolveRef(it.schema) }}
|
||||
var result = validateRef('{{= it.schema }}', data, dataPath);
|
||||
if (!result.valid) validate.errors.push.apply(validate.errors, result.errors);
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
function (data, dataPath) {
|
||||
'use strict';
|
||||
|
||||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
||||
|
||||
{{~ it.schema:$schema:$i }}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
function (data, dataPath) {
|
||||
'use strict';
|
||||
|
||||
var errs = validate.errors.length;
|
||||
|
||||
{{~ it.schema:$schema:$i }}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
function (data, dataPath) {
|
||||
'use strict';
|
||||
|
||||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
||||
|
||||
{{
|
||||
|
|
|
@ -1,18 +1,11 @@
|
|||
function (data, dataPath) {
|
||||
'use strict';
|
||||
/* TODO change to inline */
|
||||
{{ var $enum_itemsHash = it.toHash(it.schema.enum, it.stableStringify); }}
|
||||
|
||||
{{ var $itemsHash = it.toHash(it.schema, it.stableStringify); }}
|
||||
var itemsHash = {{= JSON.stringify($enum_itemsHash) }};
|
||||
var valid = itemsHash[stableStringify(data)] || false;
|
||||
|
||||
var itemsHash = {{= JSON.stringify($itemsHash) }};
|
||||
var valid = itemsHash[stableStringify(data)];
|
||||
|
||||
if (!valid) validate.errors.push({
|
||||
keyword: 'enum',
|
||||
dataPath: dataPath,
|
||||
message: 'should be equal to one of values'
|
||||
{{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath }}, data: data{{?}}
|
||||
});
|
||||
|
||||
return valid || false;
|
||||
}
|
||||
if (!valid) validate.errors.push({
|
||||
keyword: 'enum',
|
||||
dataPath: dataPath,
|
||||
message: 'should be equal to one of values'
|
||||
{{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath + '.enum' }}, data: data{{?}}
|
||||
});
|
||||
|
|
|
@ -15,8 +15,8 @@ var RULES = module.exports = [
|
|||
{ type: 'object',
|
||||
inline: [ 'maxProperties', 'minProperties', 'required' ],
|
||||
func: [ 'dependencies', 'properties' ] },
|
||||
{ inline: [ 'type' ],
|
||||
func: [ '$ref', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] }
|
||||
{ inline: [ 'type', 'enum' ],
|
||||
func: [ '$ref', 'not', 'anyOf', 'oneOf', 'allOf' ] }
|
||||
];
|
||||
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
function (data, dataPath) {
|
||||
'use strict';
|
||||
|
||||
var errs = validate.errors.length;
|
||||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
||||
|
||||
{{
|
||||
var $propertyKeys = Object.keys(it.schema || {})
|
||||
|
|
|
@ -3,7 +3,7 @@ function (data, dataPath) {
|
|||
|
||||
/* TODO change to inline ??? with break in the loop */
|
||||
|
||||
var errs = validate.errors.length;
|
||||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
||||
|
||||
{{? it.schema && it.opts.uniqueItems !== false }}
|
||||
if (data.length <= 1) return true;
|
||||
|
|
|
@ -12,12 +12,12 @@
|
|||
module.exports = function (it) {
|
||||
if (it.isRoot) {
|
||||
it.isRoot = false;
|
||||
var out = '\nfunction (data) { \n "use strict"; \n';
|
||||
var out = '\nfunction (data) { \n';
|
||||
out += 'var dataPath = ""; \n';
|
||||
out += 'var errs = validate.errors.length = 0; \n';
|
||||
out += (it.opts.allErrors ? 'var errs = ' : '') + 'validate.errors.length = 0; \n';
|
||||
} else {
|
||||
var out = '\nfunction (data, dataPath) { \n "use strict"; \n';
|
||||
out += 'var errs = validate.errors.length; \n';
|
||||
var out = '\nfunction (data, dataPath) { \n';
|
||||
if (it.opts.allErrors) out += 'var errs = validate.errors.length; \n';
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"JSON-Schema-Test-Suite": "git+ssh://git@github.com/json-schema/JSON-Schema-Test-Suite.git",
|
||||
"glob": "^5.0.10",
|
||||
"mocha": "^2.2.5"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
'use strict';
|
||||
|
||||
var fs = require('fs')
|
||||
var glob = require('glob')
|
||||
, path = require('path')
|
||||
, assert = require('assert')
|
||||
, TESTS_PATH = 'JSON-Schema-Test-Suite/tests/draft4/';
|
||||
, assert = require('assert');
|
||||
|
||||
var ONLY_RULES, SKIP_RULES;
|
||||
// ONLY_RULES = [
|
||||
|
@ -20,8 +19,8 @@ var ONLY_RULES, SKIP_RULES;
|
|||
// ];
|
||||
|
||||
SKIP_RULES = [
|
||||
// 'refRemote',
|
||||
// 'optional/zeroTerminatedFloats'
|
||||
'refRemote',
|
||||
'optional/zeroTerminatedFloats'
|
||||
];
|
||||
|
||||
|
||||
|
@ -30,9 +29,9 @@ var Ajv = require('../lib/ajv')
|
|||
, fullAjv = Ajv({ allErrors: true, verbose: true });
|
||||
|
||||
var remoteRefs = {
|
||||
'http://localhost:1234/integer.json': require('../JSON-Schema-Test-Suite/remotes/integer.json'),
|
||||
'http://localhost:1234/subSchemas.json': require('../JSON-Schema-Test-Suite/remotes/subSchemas.json'),
|
||||
'http://localhost:1234/folder/folderInteger.json': require('../JSON-Schema-Test-Suite/remotes/folder/folderInteger.json')
|
||||
'http://localhost:1234/integer.json': require('./JSON-Schema-Test-Suite/remotes/integer.json'),
|
||||
'http://localhost:1234/subSchemas.json': require('./JSON-Schema-Test-Suite/remotes/subSchemas.json'),
|
||||
'http://localhost:1234/folder/folderInteger.json': require('./JSON-Schema-Test-Suite/remotes/folder/folderInteger.json')
|
||||
};
|
||||
|
||||
for (var id in remoteRefs) {
|
||||
|
@ -42,58 +41,54 @@ for (var id in remoteRefs) {
|
|||
|
||||
|
||||
describe('JSON-Schema tests', function () {
|
||||
var testsPath = path.join(__dirname, '..', TESTS_PATH);
|
||||
var files = getTestFilesRecursive(testsPath);
|
||||
addTests('draft4: ', './json-schema-test-suite/tests/draft4/{**/,}*.json');
|
||||
|
||||
files.forEach(function (file) {
|
||||
if (ONLY_RULES && ONLY_RULES.indexOf(file.name) == -1) return;
|
||||
if (SKIP_RULES && SKIP_RULES.indexOf(file.name) >= 0) return;
|
||||
function addTests(description, testsPath) {
|
||||
describe(description, function() {
|
||||
var files = getTestFiles(testsPath);
|
||||
|
||||
describe(file.name, function() {
|
||||
var testSets = require(file.path);
|
||||
testSets.forEach(function (testSet) {
|
||||
// if (testSet.description != 'multiple types can be specified in an array') return;
|
||||
describe(testSet.description, function() {
|
||||
// it(testSet.description, function() {
|
||||
var validate = ajv.compile(testSet.schema);
|
||||
var fullValidate = fullAjv.compile(testSet.schema);
|
||||
files.forEach(function (file) {
|
||||
if (ONLY_RULES && ONLY_RULES.indexOf(file.name) == -1) return;
|
||||
if (SKIP_RULES && SKIP_RULES.indexOf(file.name) >= 0) return;
|
||||
|
||||
testSet.tests.forEach(function (test) {
|
||||
// if (test.description != 'an integer is valid') return;
|
||||
it(test.description, function() {
|
||||
var valid = validate(test.data);
|
||||
// console.log('result', result);
|
||||
assert.equal(valid, test.valid);
|
||||
if (valid) assert(validate.errors.length == 0);
|
||||
else assert(validate.errors.length > 0);
|
||||
describe(file.name, function() {
|
||||
var testSets = require(file.path);
|
||||
testSets.forEach(function (testSet) {
|
||||
// if (testSet.description != 'multiple types can be specified in an array') return;
|
||||
describe(testSet.description, function() {
|
||||
// it(testSet.description, function() {
|
||||
var validate = ajv.compile(testSet.schema);
|
||||
var fullValidate = fullAjv.compile(testSet.schema);
|
||||
|
||||
var valid = fullValidate(test.data);
|
||||
// console.log('full result', result);
|
||||
assert.equal(valid, test.valid);
|
||||
if (valid) assert(validate.errors.length == 0);
|
||||
else assert(validate.errors.length > 0);
|
||||
testSet.tests.forEach(function (test) {
|
||||
// if (test.description != 'an integer is valid') return;
|
||||
it(test.description, function() {
|
||||
var valid = validate(test.data);
|
||||
// console.log('result', result);
|
||||
assert.equal(valid, test.valid);
|
||||
if (valid) assert(validate.errors.length == 0);
|
||||
else assert(validate.errors.length > 0);
|
||||
|
||||
var valid = fullValidate(test.data);
|
||||
// console.log('full result', result);
|
||||
assert.equal(valid, test.valid);
|
||||
if (valid) assert(validate.errors.length == 0);
|
||||
else assert(validate.errors.length > 0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function getTestFilesRecursive(rootPath) {
|
||||
var list = fs.readdirSync(rootPath);
|
||||
var files = [];
|
||||
list.forEach(function (item) {
|
||||
var itemPath = path.join(rootPath, item);
|
||||
var stat = fs.statSync(itemPath);
|
||||
if (stat.isFile()) files.push({ name: path.basename(item, '.json'), path: itemPath });
|
||||
else if (stat.isDirectory()) {
|
||||
var _files = getTestFilesRecursive(itemPath);
|
||||
_files.forEach(function (f) {
|
||||
files.push({ name: path.join(item, f.name), path: f.path })
|
||||
});
|
||||
}
|
||||
function getTestFiles(testsPath) {
|
||||
var files = glob.sync(testsPath, { cwd: __dirname });
|
||||
return files.map(function (file) {
|
||||
var optional = /optional\/\w+\.json/.test(file) ? 'optional/' : '';
|
||||
return { path: file, name: optional + path.basename(file, '.json') };
|
||||
});
|
||||
return files;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue