fatsify核心功能示例测试!!!

This commit is contained in:
2025-09-21 14:50:41 +08:00
commit 9145aea047
1958 changed files with 230098 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
# JSON-Schema-Test-Suite
You can find all test cases [here](https://github.com/json-schema-org/JSON-Schema-Test-Suite).
It contains a set of JSON objects that implementors of JSON Schema validation libraries can use to test their validators.
# How to add another test case?
1. Navigate to [JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite/tree/master/tests)
2. Choose a draft `draft4`, `draft6` or `draft7`
3. Copy & paste the `test-case.json` to the project and add a test like in the `draft4.test.js`

View File

@@ -0,0 +1,12 @@
'use strict'
const { test } = require('node:test')
const { counTests, runTests } = require('./util')
const requiredTestSuite = require('./draft4/required.json')
test('required', async (t) => {
const skippedTests = ['ignores arrays', 'ignores strings', 'ignores other non-objects']
t.plan(counTests(requiredTestSuite, skippedTests))
await runTests(t, requiredTestSuite, skippedTests)
})

View File

@@ -0,0 +1,54 @@
[
{
"description": "required validation",
"schema": {
"properties": {
"foo": {},
"bar": {}
},
"required": ["foo"]
},
"tests": [
{
"description": "present required property is valid",
"data": {"foo": 1},
"valid": true
},
{
"description": "non-present required property is invalid",
"data": {"bar": 1},
"valid": false
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores strings",
"data": "",
"valid": true
},
{
"description": "ignores other non-objects",
"data": 12,
"valid": true
}
]
},
{
"description": "required default validation",
"schema": {
"properties": {
"foo": {}
}
},
"tests": [
{
"description": "not required by default",
"data": {},
"valid": true
}
]
}
]

View File

@@ -0,0 +1,12 @@
'use strict'
const { test } = require('node:test')
const { counTests, runTests } = require('./util')
const requiredTestSuite = require('./draft6/required.json')
test('required', async (t) => {
const skippedTests = ['ignores arrays', 'ignores strings', 'ignores other non-objects']
t.plan(counTests(requiredTestSuite, skippedTests))
await runTests(t, requiredTestSuite, skippedTests)
})

View File

@@ -0,0 +1,70 @@
[
{
"description": "required validation",
"schema": {
"properties": {
"foo": {},
"bar": {}
},
"required": ["foo"]
},
"tests": [
{
"description": "present required property is valid",
"data": {"foo": 1},
"valid": true
},
{
"description": "non-present required property is invalid",
"data": {"bar": 1},
"valid": false
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores strings",
"data": "",
"valid": true
},
{
"description": "ignores other non-objects",
"data": 12,
"valid": true
}
]
},
{
"description": "required default validation",
"schema": {
"properties": {
"foo": {}
}
},
"tests": [
{
"description": "not required by default",
"data": {},
"valid": true
}
]
},
{
"description": "required with empty array",
"schema": {
"properties": {
"foo": {}
},
"required": []
},
"tests": [
{
"description": "property not required",
"data": {},
"valid": true
}
]
}
]

View File

@@ -0,0 +1,12 @@
'use strict'
const { test } = require('node:test')
const { counTests, runTests } = require('./util')
const requiredTestSuite = require('./draft7/required.json')
test('required', async (t) => {
const skippedTests = ['ignores arrays', 'ignores strings', 'ignores other non-objects']
t.plan(counTests(requiredTestSuite, skippedTests))
await runTests(t, requiredTestSuite, skippedTests)
})

View File

@@ -0,0 +1,70 @@
[
{
"description": "required validation",
"schema": {
"properties": {
"foo": {},
"bar": {}
},
"required": ["foo"]
},
"tests": [
{
"description": "present required property is valid",
"data": {"foo": 1},
"valid": true
},
{
"description": "non-present required property is invalid",
"data": {"bar": 1},
"valid": false
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores strings",
"data": "",
"valid": true
},
{
"description": "ignores other non-objects",
"data": 12,
"valid": true
}
]
},
{
"description": "required default validation",
"schema": {
"properties": {
"foo": {}
}
},
"tests": [
{
"description": "not required by default",
"data": {},
"valid": true
}
]
},
{
"description": "required with empty array",
"schema": {
"properties": {
"foo": {}
},
"required": []
},
"tests": [
{
"description": "property not required",
"data": {},
"valid": true
}
]
}
]

View File

@@ -0,0 +1,31 @@
'use strict'
const build = require('../..')
async function runTests (t, testsuite, skippedTests) {
for (const scenario of testsuite) {
const stringify = build(scenario.schema)
for (const test of scenario.tests) {
if (skippedTests.indexOf(test.description) !== -1) {
console.log(`skip ${test.description}`)
continue
}
await t.test(test.description, (t) => {
t.plan(1)
try {
const output = stringify(test.data)
t.assert.equal(output, JSON.stringify(test.data), 'compare payloads')
} catch (err) {
t.assert.ok(test.valid === false, 'payload should be valid: ' + err.message)
}
})
}
}
}
function counTests (ts, skippedTests) {
return ts.reduce((a, b) => a + b.tests.length, 0) - skippedTests.length
}
module.exports = { runTests, counTests }