Skip to content

Commit 5b53fc7

Browse files
committed
test: implement run-test-file to run single json tests
1 parent daf461d commit 5b53fc7

File tree

3 files changed

+72
-54
lines changed

3 files changed

+72
-54
lines changed

test/json-schema.js

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

3-
const tape = require('tape')
43
const fs = require('fs')
54
const path = require('path')
6-
const { validator, parser } = require('../')
75
const schemas = require('./util/schemas')
6+
const { processTest } = require('./util/json-schema-test')
87

98
// these tests require lax mode
109
const unsafe = new Set([
@@ -89,7 +88,7 @@ function processTestDir(schemaDir, main, subdir = '') {
8988
const sub = path.join(subdir, file) // relative to schemaDir
9089
if (shouldIngore(sub)) continue
9190
if (file.endsWith('.json')) {
92-
const content = fs.readFileSync(path.join(dir, file))
91+
const content = fs.readFileSync(path.join(dir, file), 'utf-8')
9392
processTest(main, sub, JSON.parse(content), shouldIngore, requiresLax)
9493
} else {
9594
// assume it's a dir and let it fail otherwise
@@ -98,57 +97,6 @@ function processTestDir(schemaDir, main, subdir = '') {
9897
}
9998
}
10099

101-
const schemaVersions = new Map(
102-
Object.entries({
103-
'draft2019-09': 'http://json-schema.org/draft/2019-09/schema#',
104-
draft7: 'http://json-schema.org/draft-07/schema#',
105-
draft6: 'http://json-schema.org/draft-06/schema#',
106-
draft4: 'http://json-schema.org/draft-04/schema#',
107-
draft3: 'http://json-schema.org/draft-03/schema#',
108-
})
109-
)
110-
111-
function processTest(main, id, file, shouldIngore, requiresLax) {
112-
for (const block of file) {
113-
if (shouldIngore(`${id}/${block.description}`)) continue
114-
tape(`json-schema-test-suite ${main}/${id}/${block.description}`, (t) => {
115-
try {
116-
const mode = requiresLax(`${id}/${block.description}`) ? 'lax' : 'default'
117-
const $schemaDefault = schemaVersions.get(main)
118-
const extraFormats = main === 'draft3' // needs old formats
119-
const blockSchemas = [
120-
...(Object.hasOwnProperty.call(block, 'schema') ? [block.schema] : []),
121-
...(block.schemas || []),
122-
]
123-
for (const schema of blockSchemas) {
124-
for (const [includeErrors, allErrors] of [[false, false], [true, false], [true, true]]) {
125-
// ajv sometimes specifies just the schema id as "schema"
126-
const wrapped = typeof schema === 'string' ? { $ref: schema } : schema
127-
const opts = { schemas, mode, $schemaDefault, extraFormats, includeErrors, allErrors }
128-
const validate = validator(wrapped, opts)
129-
const parse = parser(wrapped, opts)
130-
for (const test of block.tests) {
131-
if (shouldIngore(`${id}/${block.description}/${test.description}`)) continue
132-
t.same(validate(test.data), test.valid, test.description)
133-
t.same(parse(JSON.stringify(test.data)).valid, test.valid, test.description)
134-
}
135-
if (mode === 'lax') {
136-
t.throws(
137-
() => validator(wrapped, { ...opts, mode: 'default' }),
138-
'Throws without lax mode'
139-
)
140-
}
141-
}
142-
}
143-
} catch (e) {
144-
t.fail(e)
145-
} finally {
146-
t.end()
147-
}
148-
})
149-
}
150-
}
151-
152100
/** JSON Schema Test Suite tests **/
153101
const testsDir = 'JSON-Schema-Test-Suite/tests'
154102
processTestDir(testsDir, 'draft4')

test/util/json-schema-test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict'
2+
3+
const tape = require('tape')
4+
const { validator, parser } = require('../../')
5+
const schemas = require('./schemas')
6+
7+
const schemaVersions = new Map(
8+
Object.entries({
9+
'draft2019-09': 'http://json-schema.org/draft/2019-09/schema#',
10+
draft7: 'http://json-schema.org/draft-07/schema#',
11+
draft6: 'http://json-schema.org/draft-06/schema#',
12+
draft4: 'http://json-schema.org/draft-04/schema#',
13+
draft3: 'http://json-schema.org/draft-03/schema#',
14+
})
15+
)
16+
17+
function processTest(main, id, file, shouldIngore, requiresLax) {
18+
for (const block of file) {
19+
if (shouldIngore(`${id}/${block.description}`)) continue
20+
tape(`json-schema-test-suite ${main}/${id}/${block.description}`, (t) => {
21+
try {
22+
const mode = requiresLax(`${id}/${block.description}`) ? 'lax' : 'default'
23+
const $schemaDefault = schemaVersions.get(main)
24+
const extraFormats = main === 'draft3' // needs old formats
25+
const blockSchemas = [
26+
...(Object.hasOwnProperty.call(block, 'schema') ? [block.schema] : []),
27+
...(block.schemas || []),
28+
]
29+
for (const schema of blockSchemas) {
30+
for (const [includeErrors, allErrors] of [[false, false], [true, false], [true, true]]) {
31+
// ajv sometimes specifies just the schema id as "schema"
32+
const wrapped = typeof schema === 'string' ? { $ref: schema } : schema
33+
const opts = { schemas, mode, $schemaDefault, extraFormats, includeErrors, allErrors }
34+
const validate = validator(wrapped, opts)
35+
const parse = parser(wrapped, opts)
36+
for (const test of block.tests) {
37+
if (shouldIngore(`${id}/${block.description}/${test.description}`)) continue
38+
t.same(validate(test.data), test.valid, test.description)
39+
t.same(parse(JSON.stringify(test.data)).valid, test.valid, test.description)
40+
}
41+
if (mode === 'lax') {
42+
t.throws(
43+
() => validator(wrapped, { ...opts, mode: 'default' }),
44+
'Throws without lax mode'
45+
)
46+
}
47+
}
48+
}
49+
} catch (e) {
50+
t.fail(e)
51+
} finally {
52+
t.end()
53+
}
54+
})
55+
}
56+
}
57+
58+
module.exports = { processTest }

test/util/run-test-file.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const { processTest } = require('./json-schema-test')
6+
7+
const files = process.argv.slice(2)
8+
for (const file of files) {
9+
if (!file.endsWith('.json')) throw new Error(`Not a JSON: ${file}`)
10+
const content = fs.readFileSync(path.resolve(file), 'utf-8')
11+
processTest('', file, JSON.parse(content), () => false, () => false)
12+
}

0 commit comments

Comments
 (0)