@@ -2,7 +2,6 @@ import chai from 'chai'
2
2
const assert = chai . assert
3
3
import { beforeAll , describe , afterAll } from '@jest/globals'
4
4
5
- import * as hed from '../validator/event'
6
5
import { BidsHedIssue } from '../bids/types/issues'
7
6
import { buildSchemas } from '../schema/init'
8
7
import { SchemaSpec , SchemasSpec } from '../schema/specs'
@@ -11,8 +10,6 @@ import { BidsSidecar, BidsTsvFile } from '../bids'
11
10
import { generateIssue , IssueError } from '../common/issues/issues'
12
11
const fs = require ( 'fs' )
13
12
14
- const displayLog = process . env . DISPLAY_LOG === 'true'
15
-
16
13
const skippedErrors = {
17
14
VERSION_DEPRECATED : 'Not handling in the spec tests' ,
18
15
ELEMENT_DEPRECATED : 'Not handling in this round. This is a warning' ,
@@ -78,11 +75,6 @@ describe('HED validation using JSON tests', () => {
78
75
[ '8.3.0' , undefined ] ,
79
76
] )
80
77
81
- const badLog = [ ]
82
- let totalTests = 0
83
- let wrongErrors = 0
84
- let unexpectedErrors = 0
85
-
86
78
beforeAll ( async ( ) => {
87
79
const spec2 = new SchemaSpec ( '' , '8.2.0' , '' , path . join ( __dirname , '../tests/data/HED8.2.0.xml' ) )
88
80
const specs2 = new SchemasSpec ( ) . addSchemaSpec ( spec2 )
@@ -94,13 +86,7 @@ describe('HED validation using JSON tests', () => {
94
86
schemaMap . set ( '8.3.0' , schemas3 )
95
87
} )
96
88
97
- afterAll ( ( ) => {
98
- const outBad = path . join ( __dirname , 'runLog.txt' )
99
- const summary = `Total tests:${ totalTests } Wrong error codes:${ wrongErrors } Unexpected errors:${ unexpectedErrors } \n`
100
- if ( displayLog ) {
101
- fs . writeFileSync ( outBad , summary + badLog . join ( '\n' ) , 'utf8' )
102
- }
103
- } )
89
+ afterAll ( ( ) => { } )
104
90
105
91
test ( 'should load testInfo and schemas correctly' , ( ) => {
106
92
expect ( testInfo ) . toBeDefined ( )
@@ -113,115 +99,100 @@ describe('HED validation using JSON tests', () => {
113
99
114
100
describe . each ( testInfo ) (
115
101
'$error_code $name : $description' ,
116
- ( { error_code, alt_codes, name, description , schema, warning, definitions, tests } ) => {
102
+ ( { error_code, alt_codes, name, schema, warning, definitions, tests } ) => {
117
103
let hedSchema
118
- let itemLog
119
104
let defs
120
- let hasWarning
105
+ let expectedErrors
106
+ const noErrors = new Set ( )
107
+
121
108
const failedSidecars = stringifyList ( tests . sidecar_tests . fails )
122
109
const passedSidecars = stringifyList ( tests . sidecar_tests . passes )
123
110
const failedEvents = tsvListToStrings ( tests . event_tests . fails )
124
111
const passedEvents = tsvListToStrings ( tests . event_tests . passes )
125
112
const failedCombos = comboListToStrings ( tests . combo_tests . fails )
126
113
const passedCombos = comboListToStrings ( tests . combo_tests . passes )
127
114
128
- const assertErrors = function ( eCode , altCodes , expectError , iLog , header , issues ) {
129
- const errors = [ ]
130
- const log = [ header ]
131
- totalTests += 1
132
-
115
+ const assertErrors = function ( expectedErrors , issues , header ) {
116
+ // Get the set of actual issues that were encountered.
117
+ const errors = new Set ( )
133
118
for ( const issue of issues ) {
134
119
if ( issue instanceof BidsHedIssue ) {
135
- errors . push ( ` ${ issue . hedIssue . hedCode } ` )
120
+ errors . add ( issue . hedIssue . hedCode )
136
121
} else {
137
- errors . push ( ` ${ issue . hedCode } ` )
122
+ errors . add ( issue . hedCode )
138
123
}
139
124
}
140
- let altErrorString = ''
141
- if ( altCodes . length > 0 ) {
142
- altErrorString = ` or alternative error codes [${ altCodes . join ( ' ,' ) } ] `
143
- }
144
- const errorString = errors . join ( ',' )
145
- if ( errors . length > 0 ) {
146
- log . push ( `---has errors [${ errorString } ]` )
147
- }
148
- const expectedErrors = [ ...[ eCode ] , ...altCodes ]
149
- const wrongError = `---expected ${ eCode } ${ altErrorString } but got errors [${ errorString } ]`
150
- const hasErrors = `---expected no errors but got errors [${ errorString } ]`
151
- if ( expectError && ! expectedErrors . some ( ( substring ) => errorString . includes ( substring ) ) ) {
152
- log . push ( wrongError )
153
- iLog . push ( log . join ( '\n' ) )
154
- wrongErrors += 1
155
- assert ( errorString . includes ( eCode ) , `${ header } ---expected ${ eCode } and got errors [${ errorString } ]` )
156
- } else if ( ! expectError && errorString . length > 0 ) {
157
- log . push ( hasErrors )
158
- iLog . push ( log . join ( '\n' ) )
159
- unexpectedErrors += 1
160
- assert ( errorString . length === 0 , `${ header } ---expected no errors but got errors [${ errorString } ]` )
125
+ let hasIntersection = [ ...expectedErrors ] . some ( ( element ) => errors . has ( element ) )
126
+ if ( expectedErrors . size === 0 && errors . size === 0 ) {
127
+ hasIntersection = true
161
128
}
129
+ assert . isTrue (
130
+ hasIntersection ,
131
+ `${ header } expected one of errors[${ [ ...expectedErrors ] . join ( ', ' ) } ] but received [${ [ ...errors ] . join ( ', ' ) } ]` ,
132
+ )
162
133
}
163
134
164
- const comboValidator = function ( eCode , altCodes , eName , side , events , schema , defs , expectError , iLog ) {
165
- const status = expectError ? 'Expect fail' : 'Expect pass'
166
- const header = `\n[${ eCode } ${ eName } ](${ status } )\tCOMBO\t"${ side } "\n"${ events } "`
135
+ const comboValidator = function ( side , events , expectedErrors ) {
136
+ const status = expectedErrors . size === 0 ? 'Expect fail' : 'Expect pass'
137
+ const header = `\n[${ error_code } ${ name } ](${ status } )\tCOMBO\t"${ side } "\n"${ events } "`
167
138
const mergedSide = getMergedSidecar ( side , defs )
168
139
let sidecarIssues = [ ]
169
140
try {
170
141
const bidsSide = new BidsSidecar ( `sidecar` , mergedSide , { relativePath : 'combo test sidecar' } )
171
- sidecarIssues = bidsSide . validate ( schema )
142
+ sidecarIssues = bidsSide . validate ( hedSchema )
172
143
} catch ( e ) {
173
144
sidecarIssues = [ convertIssue ( e ) ]
174
145
}
175
146
let eventsIssues = [ ]
176
147
try {
177
148
const bidsTsv = new BidsTsvFile ( `events` , events , { relativePath : 'combo test tsv' } , [ side ] , mergedSide )
178
- eventsIssues = bidsTsv . validate ( schema )
149
+ eventsIssues = bidsTsv . validate ( hedSchema )
179
150
} catch ( e ) {
180
151
eventsIssues = [ convertIssue ( e ) ]
181
152
}
182
153
const allIssues = [ ...sidecarIssues , ...eventsIssues ]
183
- assertErrors ( eCode , altCodes , expectError , iLog , header , allIssues )
154
+ assertErrors ( expectedErrors , allIssues , header )
184
155
}
185
156
186
- const eventsValidator = function ( eCode , altCodes , eName , events , schema , defs , expectError , iLog ) {
187
- const status = expectError ? 'Expect fail' : 'Expect pass'
188
- const header = `\n[${ eCode } ${ eName } ](${ status } )\tEvents:\n"${ events } "`
157
+ const eventsValidator = function ( events , expectedErrors ) {
158
+ const status = expectedErrors . size === 0 ? 'Expect fail' : 'Expect pass'
159
+ const header = `\n[${ error_code } ${ name } ](${ status } )\tEvents:\n"${ events } "`
189
160
let eventsIssues = [ ]
190
161
try {
191
162
const bidsTsv = new BidsTsvFile ( `events` , events , { relativePath : 'events test' } , [ ] , defs )
192
- eventsIssues = bidsTsv . validate ( schema )
163
+ eventsIssues = bidsTsv . validate ( hedSchema )
193
164
} catch ( e ) {
194
165
eventsIssues = [ convertIssue ( e ) ]
195
166
}
196
- assertErrors ( eCode , altCodes , expectError , iLog , header , eventsIssues )
167
+ assertErrors ( expectedErrors , eventsIssues , header )
197
168
}
198
169
199
- const sideValidator = function ( eCode , altCodes , eName , side , schema , defs , expectError , iLog ) {
200
- const status = expectError ? 'Expect fail' : 'Expect pass'
201
- const header = `\n[${ eCode } ${ eName } ](${ status } )\tSIDECAR "${ side } "`
170
+ const sideValidator = function ( side , expectedErrors ) {
171
+ const status = expectedErrors . size === 0 ? 'Expect fail' : 'Expect pass'
172
+ const header = `\n[${ error_code } ${ name } ](${ status } )\tSIDECAR "${ side } "`
202
173
const side1 = getMergedSidecar ( side , defs )
203
174
let sidecarIssues = [ ]
204
175
try {
205
176
const bidsSide = new BidsSidecar ( `sidecar` , side1 , { relativePath : 'sidecar test' } )
206
- sidecarIssues = bidsSide . validate ( schema )
177
+ sidecarIssues = bidsSide . validate ( hedSchema )
207
178
} catch ( e ) {
208
179
sidecarIssues = [ convertIssue ( e ) ]
209
180
}
210
- assertErrors ( eCode , altCodes , expectError , iLog , header , sidecarIssues )
181
+ assertErrors ( expectedErrors , sidecarIssues , header )
211
182
}
212
183
213
- const stringValidator = function ( eCode , altCodes , eName , str , schema , defs , expectError , iLog ) {
214
- const status = expectError ? 'Expect fail' : 'Expect pass'
215
- const header = `\n[${ eCode } ${ eName } ](${ status } )\tSTRING: "${ str } "`
184
+ const stringValidator = function ( str , expectedErrors ) {
185
+ const status = expectedErrors . size === 0 ? 'Expect fail' : 'Expect pass'
186
+ const header = `\n[${ error_code } ${ name } ](${ status } )\tSTRING: "${ str } "`
216
187
const hTsv = `HED\n${ str } \n`
217
188
let stringIssues = [ ]
218
189
try {
219
190
const bidsTsv = new BidsTsvFile ( `events` , hTsv , { relativePath : 'string test tsv' } , [ ] , defs )
220
- stringIssues = bidsTsv . validate ( schema )
191
+ stringIssues = bidsTsv . validate ( hedSchema )
221
192
} catch ( e ) {
222
193
stringIssues = [ convertIssue ( e ) ]
223
194
}
224
- assertErrors ( eCode , altCodes , expectError , iLog , header , stringIssues )
195
+ assertErrors ( expectedErrors , stringIssues , header )
225
196
}
226
197
227
198
/**
@@ -241,16 +212,13 @@ describe('HED validation using JSON tests', () => {
241
212
beforeAll ( async ( ) => {
242
213
hedSchema = schemaMap . get ( schema )
243
214
defs = { definitions : { HED : { defList : definitions . join ( ',' ) } } }
244
- itemLog = [ ]
245
- hasWarning = warning
215
+ expectedErrors = new Set ( alt_codes )
216
+ expectedErrors . add ( error_code )
246
217
} )
247
218
248
- afterAll ( ( ) => {
249
- badLog . push ( itemLog . join ( '\n' ) )
250
- } )
219
+ afterAll ( ( ) => { } )
251
220
252
221
if ( error_code in skippedErrors || name in skippedErrors ) {
253
- //badLog.push(`${error_code} skipped because ${skippedErrors["error_code"]}`);
254
222
test . skip ( `Skipping tests ${ error_code } skipped because ${ skippedErrors [ 'error_code' ] } ` , ( ) => { } )
255
223
} else {
256
224
test ( 'it should have HED schema defined' , ( ) => {
@@ -259,49 +227,49 @@ describe('HED validation using JSON tests', () => {
259
227
260
228
if ( tests . string_tests . passes . length > 0 ) {
261
229
test . each ( tests . string_tests . passes ) ( 'Valid string: %s' , ( str ) => {
262
- stringValidator ( error_code , alt_codes , name , str , hedSchema , defs , false , itemLog )
230
+ stringValidator ( str , noErrors )
263
231
} )
264
232
}
265
233
266
234
if ( tests . string_tests . fails . length > 0 ) {
267
235
test . each ( tests . string_tests . fails ) ( 'Invalid string: %s' , ( str ) => {
268
- stringValidator ( error_code , alt_codes , name , str , hedSchema , defs , true , itemLog )
236
+ stringValidator ( str , expectedErrors )
269
237
} )
270
238
}
271
239
272
240
if ( passedSidecars . length > 0 ) {
273
241
test . each ( passedSidecars ) ( `Valid sidecar: %s` , ( side ) => {
274
- sideValidator ( error_code , alt_codes , name , side , hedSchema , defs , false , itemLog )
242
+ sideValidator ( side , noErrors )
275
243
} )
276
244
}
277
245
278
246
if ( failedSidecars . length > 0 ) {
279
247
test . each ( failedSidecars ) ( `Invalid sidecar: %s` , ( side ) => {
280
- sideValidator ( error_code , alt_codes , name , side , hedSchema , defs , true , itemLog )
248
+ sideValidator ( side , expectedErrors )
281
249
} )
282
250
}
283
251
284
252
if ( passedEvents . length > 0 ) {
285
253
test . each ( passedEvents ) ( `Valid events: %s` , ( events ) => {
286
- eventsValidator ( error_code , alt_codes , name , events , hedSchema , defs , false , itemLog )
254
+ eventsValidator ( events , noErrors )
287
255
} )
288
256
}
289
257
290
258
if ( failedEvents . length > 0 ) {
291
259
test . each ( failedEvents ) ( `Invalid events: %s` , ( events ) => {
292
- eventsValidator ( error_code , alt_codes , name , events , hedSchema , defs , true , itemLog )
260
+ eventsValidator ( events , expectedErrors )
293
261
} )
294
262
}
295
263
296
264
if ( passedCombos . length > 0 ) {
297
265
test . each ( passedCombos ) ( `Valid combo: [%s] [%s]` , ( side , events ) => {
298
- comboValidator ( error_code , alt_codes , name , side , events , hedSchema , defs , false , itemLog )
266
+ comboValidator ( side , events , noErrors )
299
267
} )
300
268
}
301
269
302
270
if ( failedCombos . length > 0 ) {
303
271
test . each ( failedCombos ) ( `Invalid combo: [%s] [%s]` , ( side , events ) => {
304
- comboValidator ( error_code , alt_codes , name , side , events , hedSchema , defs , true , itemLog )
272
+ comboValidator ( side , events , expectedErrors )
305
273
} )
306
274
}
307
275
}
0 commit comments