1
1
import fs from "node:fs" ;
2
- import path from "node:path" ;
3
- import { fileURLToPath } from "node:url" ;
4
- import { describe , it , expect , beforeEach , beforeAll , afterAll } from "vitest" ;
2
+ import { describe , test , expect , beforeEach , beforeAll , afterAll } from "vitest" ;
3
+ import { isCompatible } from "./test-utils.js" ;
5
4
import { toAbsoluteIri } from "@hyperjump/uri" ;
6
5
import { annotate } from "./index.js" ;
7
6
import { registerSchema , unregisterSchema } from "../lib/index.js" ;
8
7
import "../stable/index.js" ;
9
8
import "../draft-2020-12/index.js" ;
9
+ import "../draft-2019-09/index.js" ;
10
10
import "../draft-07/index.js" ;
11
11
import "../draft-06/index.js" ;
12
12
import "../draft-04/index.js" ;
@@ -19,12 +19,18 @@ import type { Json } from "@hyperjump/json-pointer";
19
19
20
20
21
21
type Suite = {
22
- title : string ;
22
+ description : string ;
23
+ suite : TestCase [ ] ;
24
+ } ;
25
+
26
+ type TestCase = {
27
+ description : string ;
28
+ compatibility : string ;
23
29
schema : SchemaObject ;
24
- subjects : Subject [ ] ;
30
+ tests : Test [ ] ;
25
31
} ;
26
32
27
- type Subject = {
33
+ type Test = {
28
34
instance : Json ;
29
35
assertions : Assertion [ ] ;
30
36
} ;
@@ -35,57 +41,68 @@ type Assertion = {
35
41
expected : unknown [ ] ;
36
42
} ;
37
43
38
- const __filename = fileURLToPath ( import . meta. url ) ;
39
- const __dirname = path . dirname ( __filename ) ;
40
-
41
- const dialectId = "https://json-schema.org/validation" ;
42
44
const host = "https://annotations.json-schema.hyperjump.io" ;
43
45
44
- const testSuiteFilePath = ` ${ __dirname } / tests` ;
46
+ const testSuiteFilePath = "./node_modules/json-schema-test-suite/annotations/ tests" ;
45
47
46
- describe ( "Annotations" , ( ) => {
47
- fs . readdirSync ( testSuiteFilePath , { withFileTypes : true } )
48
- . filter ( ( entry ) => entry . isFile ( ) && entry . name . endsWith ( ".json" ) )
49
- . forEach ( ( entry ) => {
50
- const file = `${ testSuiteFilePath } /${ entry . name } ` ;
51
- const suites = JSON . parse ( fs . readFileSync ( file , "utf8" ) ) as Suite [ ] ;
48
+ const testRunner = ( version : number , dialect : string ) => {
49
+ describe ( dialect , ( ) => {
50
+ fs . readdirSync ( testSuiteFilePath , { withFileTypes : true } )
51
+ . filter ( ( entry ) => entry . isFile ( ) && entry . name . endsWith ( ".json" ) )
52
+ . forEach ( ( entry ) => {
53
+ const file = `${ testSuiteFilePath } /${ entry . name } ` ;
54
+ const suite = JSON . parse ( fs . readFileSync ( file , "utf8" ) ) as Suite ;
52
55
53
- suites . forEach ( ( suite ) => {
54
- describe ( suite . title + "\n" + JSON . stringify ( suite . schema , null , " " ) , ( ) => {
55
- let annotator : Annotator ;
56
- let id : string ;
56
+ for ( const testCase of suite . suite ) {
57
+ if ( ! isCompatible ( testCase . compatibility , version ) ) {
58
+ continue ;
59
+ }
57
60
58
- beforeAll ( async ( ) => {
59
- id = ` ${ host } / ${ encodeURIComponent ( suite . title ) } ` ;
60
- registerSchema ( suite . schema , id , dialectId ) ;
61
+ describe ( testCase . description + "\n" + JSON . stringify ( testCase . schema , null , " " ) , ( ) => {
62
+ let annotator : Annotator ;
63
+ let id : string ;
61
64
62
- annotator = await annotate ( id ) ;
63
- } ) ;
65
+ beforeAll ( async ( ) => {
66
+ id = `${ host } /${ encodeURIComponent ( testCase . description ) } ` ;
67
+ registerSchema ( testCase . schema , id , dialect ) ;
64
68
65
- afterAll ( ( ) => {
66
- unregisterSchema ( id ) ;
67
- } ) ;
69
+ annotator = await annotate ( id ) ;
70
+ } ) ;
68
71
69
- suite . subjects . forEach ( ( subject ) => {
70
- describe ( "Instance: " + JSON . stringify ( subject . instance ) , ( ) => {
71
- let instance : JsonNode ;
72
+ afterAll ( ( ) => {
73
+ unregisterSchema ( id ) ;
74
+ } ) ;
72
75
73
- beforeEach ( ( ) => {
74
- // TODO: What's wrong with the type?
75
- instance = annotator ( subject . instance ) ; // eslint-disable-line @typescript-eslint/no-unsafe-assignment
76
- } ) ;
76
+ for ( const subject of testCase . tests ) {
77
+ describe ( "Instance: " + JSON . stringify ( subject . instance ) , ( ) => {
78
+ let instance : JsonNode ;
77
79
78
- subject . assertions . forEach ( ( assertion ) => {
79
- it ( `${ assertion . keyword } annotations at '${ assertion . location } ' should be ${ JSON . stringify ( assertion . expected ) } ` , ( ) => {
80
- const dialect : string | undefined = suite . schema . $schema ? toAbsoluteIri ( suite . schema . $schema as string ) : undefined ;
81
- const subject = Instance . get ( assertion . location , instance ) ;
82
- const annotations = subject ? Instance . annotation ( subject , assertion . keyword , dialect ) : [ ] ;
83
- expect ( annotations ) . to . eql ( assertion . expected ) ;
80
+ beforeEach ( ( ) => {
81
+ // TODO: What's wrong with the type?
82
+ instance = annotator ( subject . instance ) ; // eslint-disable-line @typescript-eslint/no-unsafe-assignment
84
83
} ) ;
84
+
85
+ for ( const assertion of subject . assertions ) {
86
+ test ( `${ assertion . keyword } annotations at '${ assertion . location } ' should be ${ JSON . stringify ( assertion . expected ) } ` , ( ) => {
87
+ const dialect : string | undefined = testCase . schema . $schema ? toAbsoluteIri ( testCase . schema . $schema as string ) : undefined ;
88
+ const subject = Instance . get ( `#${ assertion . location } ` , instance ) ;
89
+ const annotations = subject ? Instance . annotation ( subject , assertion . keyword , dialect ) : [ ] ;
90
+ expect ( annotations ) . to . eql ( Object . values ( assertion . expected ) ) ;
91
+ } ) ;
92
+ }
85
93
} ) ;
86
- } ) ;
94
+ }
87
95
} ) ;
88
- } ) ;
96
+ }
89
97
} ) ;
90
- } ) ;
98
+ } ) ;
99
+ } ;
100
+
101
+ describe ( "annotations" , ( ) => {
102
+ testRunner ( 9999 , "https://json-schema.org/validation" ) ;
103
+ testRunner ( 2020 , "https://json-schema.org/draft/2020-12/schema" ) ;
104
+ testRunner ( 2019 , "https://json-schema.org/draft/2019-09/schema" ) ;
105
+ testRunner ( 7 , "http://json-schema.org/draft-07/schema" ) ;
106
+ testRunner ( 6 , "http://json-schema.org/draft-06/schema" ) ;
107
+ testRunner ( 4 , "http://json-schema.org/draft-04/schema" ) ;
91
108
} ) ;
0 commit comments