|
| 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 | +// JSONTestSource: A loaded test file before segmentation |
| 35 | +// ------------------------------------------------------------------ |
| 36 | +interface JSONTestSource { |
| 37 | + sourcePath: string |
| 38 | + draft: string |
| 39 | + keyword: string |
| 40 | + groups: JSONSchemaTestGroup[] |
| 41 | +} |
| 42 | + |
| 43 | +// ------------------------------------------------------------------ |
| 44 | +// CollectJsonPaths: Load all test files from disk into memory |
| 45 | +// ------------------------------------------------------------------ |
| 46 | +function collectJsonPaths(directory: string): string[] { |
| 47 | + const results: string[] = [] |
| 48 | + for (const entry of Deno.readDirSync(directory)) { |
| 49 | + const full = `${directory}/${entry.name}` |
| 50 | + if (entry.isDirectory) results.push(...collectJsonPaths(full)) |
| 51 | + else if (entry.isFile && entry.name.endsWith('.json')) results.push(full) |
| 52 | + } |
| 53 | + return results |
| 54 | +} |
| 55 | + |
| 56 | +function resolveDraftAndKeyword(sourcePath: string, rootDirectory: string): { draft: string; keyword: string } | null { |
| 57 | + const root = rootDirectory.replace(/\\/g, '/').replace(/\/$/, '') |
| 58 | + const normalized = sourcePath.replace(/\\/g, '/') |
| 59 | + if (!normalized.startsWith(root + '/')) return null |
| 60 | + const relative = normalized.slice(root.length + 1) |
| 61 | + const slashIndex = relative.indexOf('/') |
| 62 | + if (slashIndex === -1) return null |
| 63 | + const draft = relative.slice(0, slashIndex) |
| 64 | + const keyword = relative.slice(slashIndex + 1).replace(/\.json$/, '') |
| 65 | + return { draft, keyword } |
| 66 | +} |
| 67 | + |
| 68 | +function collectJSONTests(directory: string): JSONTestSource[] { |
| 69 | + const results: JSONTestSource[] = [] |
| 70 | + for (const sourcePath of collectJsonPaths(directory)) { |
| 71 | + const resolved = resolveDraftAndKeyword(sourcePath, directory) |
| 72 | + if (resolved === null) continue |
| 73 | + const { draft, keyword } = resolved |
| 74 | + const groups = JSON.parse(Deno.readTextFileSync(sourcePath)) as JSONSchemaTestGroup[] |
| 75 | + results.push({ sourcePath, draft, keyword, groups }) |
| 76 | + } |
| 77 | + return results |
| 78 | +} |
| 79 | + |
| 80 | +// ------------------------------------------------------------------ |
| 81 | +// RunTest |
| 82 | +// ------------------------------------------------------------------ |
| 83 | +function runTest(callback: ProcessCallback, draft: string, schema: Record<string, unknown> | boolean, data: unknown): boolean | null { |
| 84 | + try { |
| 85 | + return callback(draft, schema, data) |
| 86 | + } catch { |
| 87 | + return null |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +function createAccumulator(input: JSONSchemaTestGroup[]): JSONSchemaTestGroup[] { |
| 92 | + return input.map((group) => ({ ...group, tests: [] })) |
| 93 | +} |
| 94 | + |
| 95 | +function resolveFailingPath(relativePath: string): string { |
| 96 | + return relativePath.replace(/\\/g, '/').split('/').map((s, i, a) => i === a.length - 1 ? '_' + s : s).join('/') |
| 97 | +} |
| 98 | + |
| 99 | +// ------------------------------------------------------------------ |
| 100 | +// AssertCounts |
| 101 | +// ------------------------------------------------------------------ |
| 102 | +function assertCounts(sources: JSONTestSource[], suite: JSONSchemaTestSuite): void { |
| 103 | + const sourceTotal = sources.reduce((n, s) => n + s.groups.reduce((m, g) => m + g.tests.length, 0), 0) |
| 104 | + const segmentedTotal = Object.values(suite.report).reduce((n, s) => n + s.total, 0) |
| 105 | + const passed = Object.values(suite.report).reduce((n, s) => n + s.passed, 0) |
| 106 | + const failed = Object.values(suite.report).reduce((n, s) => n + s.failed, 0) |
| 107 | + console.log(`spec: loaded ${sourceTotal}, passed ${passed}, failed ${failed}, total ${segmentedTotal}`) |
| 108 | + if (sourceTotal !== segmentedTotal) { |
| 109 | + throw new Error(`Test count mismatch: loaded ${sourceTotal} tests from disk but segmented ${segmentedTotal}`) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// ------------------------------------------------------------------ |
| 114 | +// Process: Run all tests and split into passing/failing files |
| 115 | +// ------------------------------------------------------------------ |
| 116 | +export function process(directory: string, callback: ProcessCallback = () => true): JSONSchemaTestSuite { |
| 117 | + const sources = collectJSONTests(directory) |
| 118 | + const files: JSONSchemaTestFile[] = [] |
| 119 | + const report: JSONSchemaTestSuite['report'] = {} |
| 120 | + const root = directory.replace(/\\/g, '/').replace(/\/$/, '') |
| 121 | + for (const { sourcePath, draft, keyword, groups } of sources) { |
| 122 | + const passed = createAccumulator(groups) |
| 123 | + const failed = createAccumulator(groups) |
| 124 | + for (let i = 0; i < groups.length; i++) { |
| 125 | + const schema = groups[i].schema |
| 126 | + for (const test of groups[i].tests) { |
| 127 | + const actual = runTest(callback, draft, schema, test.data) |
| 128 | + if (actual !== null && test.valid === actual) { |
| 129 | + passed[i].tests.push(test) |
| 130 | + } else { |
| 131 | + failed[i].tests.push(test) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + const passedGroups = passed.filter((group) => group.tests.length > 0) |
| 136 | + const failedGroups = failed.filter((group) => group.tests.length > 0) |
| 137 | + const passedCount = passedGroups.reduce((n, g) => n + g.tests.length, 0) |
| 138 | + const failedCount = failedGroups.reduce((n, g) => n + g.tests.length, 0) |
| 139 | + const relativePath = sourcePath.replace(/\\/g, '/').slice(root.length + 1) |
| 140 | + if (passedGroups.length > 0) { |
| 141 | + files.push({ path: relativePath, draft, keyword, failing: false, groups: passedGroups }) |
| 142 | + } |
| 143 | + if (failedGroups.length > 0) { |
| 144 | + files.push({ path: resolveFailingPath(relativePath), draft, keyword, failing: true, groups: failedGroups }) |
| 145 | + } |
| 146 | + |
| 147 | + report[`${draft}/${keyword}`] = { |
| 148 | + passed: passedCount, |
| 149 | + failed: failedCount, |
| 150 | + total: passedCount + failedCount |
| 151 | + } |
| 152 | + } |
| 153 | + files.sort((a, b) => a.path.localeCompare(b.path)) |
| 154 | + const suite = { files, report } |
| 155 | + assertCounts(sources, suite) |
| 156 | + return suite |
| 157 | +} |
0 commit comments