|
| 1 | +/*-------------------------------------------------------------------------- |
| 2 | +
|
| 3 | +TypeBox |
| 4 | +
|
| 5 | +The MIT License (MIT) |
| 6 | +
|
| 7 | +Copyright (c) 2017-2026 Haydn Paterson |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in |
| 17 | +all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +THE SOFTWARE. |
| 26 | +
|
| 27 | +---------------------------------------------------------------------------*/ |
| 28 | + |
| 29 | +import type { JSONSchemaTestFile, JSONSchemaTestGroup, JSONSchemaTestSuite } from './types.ts' |
| 30 | + |
| 31 | +export type ProcessCallback = (draft: string, schema: Record<string, unknown> | boolean, value: unknown) => boolean | null |
| 32 | + |
| 33 | +// ------------------------------------------------------------------ |
| 34 | +// CollectJsonFiles |
| 35 | +// ------------------------------------------------------------------ |
| 36 | +function collectJsonFiles(directory: string): string[] { |
| 37 | + const results: string[] = [] |
| 38 | + for (const entry of Deno.readDirSync(directory)) { |
| 39 | + const full = `${directory}/${entry.name}` |
| 40 | + if (entry.isDirectory) results.push(...collectJsonFiles(full)) |
| 41 | + else if (entry.isFile && entry.name.endsWith('.json')) results.push(full) |
| 42 | + } |
| 43 | + return results |
| 44 | +} |
| 45 | +function createAccumulator(input: JSONSchemaTestGroup[]): JSONSchemaTestGroup[] { |
| 46 | + return input.map((group) => ({ ...group, tests: [] })) |
| 47 | +} |
| 48 | +// ------------------------------------------------------------------ |
| 49 | +// RunTest |
| 50 | +// ------------------------------------------------------------------ |
| 51 | +function runTest(callback: ProcessCallback, draft: string, schema: Record<string, unknown> | boolean, data: unknown): boolean | null { |
| 52 | + try { |
| 53 | + return callback(draft, schema, data) |
| 54 | + } catch { |
| 55 | + return null |
| 56 | + } |
| 57 | +} |
| 58 | +// ------------------------------------------------------------------ |
| 59 | +// ResolveDraftAndKeyword |
| 60 | +// ------------------------------------------------------------------ |
| 61 | +function resolveDraftAndKeyword(sourcePath: string, rootDirectory: string): { draft: string; keyword: string } | null { |
| 62 | + const root = rootDirectory.replace(/\\/g, '/').replace(/\/$/, '') |
| 63 | + const normalized = sourcePath.replace(/\\/g, '/') |
| 64 | + if (!normalized.startsWith(root + '/')) return null |
| 65 | + const relative = normalized.slice(root.length + 1) // e.g. "draft7/optional/email.json" |
| 66 | + const slashIndex = relative.indexOf('/') |
| 67 | + if (slashIndex === -1) return null // no subdirectory — not a valid test path |
| 68 | + const draft = relative.slice(0, slashIndex) |
| 69 | + const keyword = relative.slice(slashIndex + 1).replace(/\.json$/, '') |
| 70 | + return { draft, keyword } |
| 71 | +} |
| 72 | +function resolveFailingPath(sourcePath: string): string { |
| 73 | + return sourcePath.replace(/\\/g, '/').split('/').map((s, i, a) => i === a.length - 1 ? '_' + s : s).join('/') |
| 74 | +} |
| 75 | +// ------------------------------------------------------------------ |
| 76 | +// Assert: Verify processed test counts match source, and log counts |
| 77 | +// ------------------------------------------------------------------ |
| 78 | +function assertCounts(suite: JSONSchemaTestSuite): void { |
| 79 | + const stats = Object.values(suite.report) |
| 80 | + const total = stats.reduce((n, s) => n + s.total, 0) |
| 81 | + const passed = stats.reduce((n, s) => n + s.passed, 0) |
| 82 | + const failed = stats.reduce((n, s) => n + s.failed, 0) |
| 83 | + console.log(`spec: total ${total}, passed ${passed}, failed ${failed}`) |
| 84 | + if (passed + failed !== total) { |
| 85 | + throw new Error(`Test count mismatch: source has ${total} tests but processed ${passed + failed}`) |
| 86 | + } |
| 87 | +} |
| 88 | +// ------------------------------------------------------------------ |
| 89 | +// Process: Run all tests and split into passing/failing files |
| 90 | +// ------------------------------------------------------------------ |
| 91 | +export function process(directory: string, callback: ProcessCallback = () => true): JSONSchemaTestSuite { |
| 92 | + const files: JSONSchemaTestFile[] = [] |
| 93 | + const report: JSONSchemaTestSuite['report'] = {} |
| 94 | + for (const sourcePath of collectJsonFiles(directory)) { |
| 95 | + const resolved = resolveDraftAndKeyword(sourcePath, directory) |
| 96 | + if (resolved === null) continue |
| 97 | + const { draft, keyword } = resolved |
| 98 | + const source = JSON.parse(Deno.readTextFileSync(sourcePath)) as JSONSchemaTestGroup[] |
| 99 | + const passed = createAccumulator(source) |
| 100 | + const failed = createAccumulator(source) |
| 101 | + for (let i = 0; i < source.length; i++) { |
| 102 | + const schema = source[i].schema |
| 103 | + for (const test of source[i].tests) { |
| 104 | + const actual = runTest(callback, draft, schema, test.data) |
| 105 | + if (actual !== null && test.valid === actual) { |
| 106 | + passed[i].tests.push(test) |
| 107 | + } else { |
| 108 | + failed[i].tests.push(test) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + const passedGroups = passed.filter((group) => group.tests.length > 0) |
| 113 | + const failedGroups = failed.filter((group) => group.tests.length > 0) |
| 114 | + const passedCount = passedGroups.reduce((n, g) => n + g.tests.length, 0) |
| 115 | + const failedCount = failedGroups.reduce((n, g) => n + g.tests.length, 0) |
| 116 | + const root = directory.replace(/\\/g, '/').replace(/\/$/, '') |
| 117 | + const relativePath = sourcePath.replace(/\\/g, '/').slice(root.length + 1) |
| 118 | + const relativeFailingPath = resolveFailingPath(relativePath) |
| 119 | + if (passedGroups.length > 0) { |
| 120 | + files.push({ path: relativePath, draft, keyword, failing: false, groups: passedGroups }) |
| 121 | + } |
| 122 | + if (failedGroups.length > 0) { |
| 123 | + files.push({ path: relativeFailingPath, draft, keyword, failing: true, groups: failedGroups }) |
| 124 | + } |
| 125 | + report[`${draft}/${keyword}`] = { |
| 126 | + passed: passedCount, |
| 127 | + failed: failedCount, |
| 128 | + total: passedCount + failedCount |
| 129 | + } |
| 130 | + } |
| 131 | + files.sort((a, b) => a.path.localeCompare(b.path)) |
| 132 | + const suite = { files, report } |
| 133 | + assertCounts(suite) |
| 134 | + return suite |
| 135 | +} |
0 commit comments