Skip to content

Commit 2431a9a

Browse files
committed
Rewrote jsonTests to eliminate the local log and simplify
1 parent 8129028 commit 2431a9a

File tree

2 files changed

+63
-81
lines changed

2 files changed

+63
-81
lines changed

spec_tests/jsonTests.spec.js

Lines changed: 49 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import chai from 'chai'
22
const assert = chai.assert
33
import { beforeAll, describe, afterAll } from '@jest/globals'
44

5-
import * as hed from '../validator/event'
65
import { BidsHedIssue } from '../bids/types/issues'
76
import { buildSchemas } from '../schema/init'
87
import { SchemaSpec, SchemasSpec } from '../schema/specs'
@@ -11,8 +10,6 @@ import { BidsSidecar, BidsTsvFile } from '../bids'
1110
import { generateIssue, IssueError } from '../common/issues/issues'
1211
const fs = require('fs')
1312

14-
const displayLog = process.env.DISPLAY_LOG === 'true'
15-
1613
const skippedErrors = {
1714
VERSION_DEPRECATED: 'Not handling in the spec tests',
1815
ELEMENT_DEPRECATED: 'Not handling in this round. This is a warning',
@@ -78,11 +75,6 @@ describe('HED validation using JSON tests', () => {
7875
['8.3.0', undefined],
7976
])
8077

81-
const badLog = []
82-
let totalTests = 0
83-
let wrongErrors = 0
84-
let unexpectedErrors = 0
85-
8678
beforeAll(async () => {
8779
const spec2 = new SchemaSpec('', '8.2.0', '', path.join(__dirname, '../tests/data/HED8.2.0.xml'))
8880
const specs2 = new SchemasSpec().addSchemaSpec(spec2)
@@ -94,13 +86,7 @@ describe('HED validation using JSON tests', () => {
9486
schemaMap.set('8.3.0', schemas3)
9587
})
9688

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(() => {})
10490

10591
test('should load testInfo and schemas correctly', () => {
10692
expect(testInfo).toBeDefined()
@@ -113,115 +99,100 @@ describe('HED validation using JSON tests', () => {
11399

114100
describe.each(testInfo)(
115101
'$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 }) => {
117103
let hedSchema
118-
let itemLog
119104
let defs
120-
let hasWarning
105+
let expectedErrors
106+
const noErrors = new Set()
107+
121108
const failedSidecars = stringifyList(tests.sidecar_tests.fails)
122109
const passedSidecars = stringifyList(tests.sidecar_tests.passes)
123110
const failedEvents = tsvListToStrings(tests.event_tests.fails)
124111
const passedEvents = tsvListToStrings(tests.event_tests.passes)
125112
const failedCombos = comboListToStrings(tests.combo_tests.fails)
126113
const passedCombos = comboListToStrings(tests.combo_tests.passes)
127114

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()
133118
for (const issue of issues) {
134119
if (issue instanceof BidsHedIssue) {
135-
errors.push(`${issue.hedIssue.hedCode}`)
120+
errors.add(issue.hedIssue.hedCode)
136121
} else {
137-
errors.push(`${issue.hedCode}`)
122+
errors.add(issue.hedCode)
138123
}
139124
}
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
161128
}
129+
assert.isTrue(
130+
hasIntersection,
131+
`${header} expected one of errors[${[...expectedErrors].join(', ')}] but received [${[...errors].join(', ')}]`,
132+
)
162133
}
163134

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}"`
167138
const mergedSide = getMergedSidecar(side, defs)
168139
let sidecarIssues = []
169140
try {
170141
const bidsSide = new BidsSidecar(`sidecar`, mergedSide, { relativePath: 'combo test sidecar' })
171-
sidecarIssues = bidsSide.validate(schema)
142+
sidecarIssues = bidsSide.validate(hedSchema)
172143
} catch (e) {
173144
sidecarIssues = [convertIssue(e)]
174145
}
175146
let eventsIssues = []
176147
try {
177148
const bidsTsv = new BidsTsvFile(`events`, events, { relativePath: 'combo test tsv' }, [side], mergedSide)
178-
eventsIssues = bidsTsv.validate(schema)
149+
eventsIssues = bidsTsv.validate(hedSchema)
179150
} catch (e) {
180151
eventsIssues = [convertIssue(e)]
181152
}
182153
const allIssues = [...sidecarIssues, ...eventsIssues]
183-
assertErrors(eCode, altCodes, expectError, iLog, header, allIssues)
154+
assertErrors(expectedErrors, allIssues, header)
184155
}
185156

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}"`
189160
let eventsIssues = []
190161
try {
191162
const bidsTsv = new BidsTsvFile(`events`, events, { relativePath: 'events test' }, [], defs)
192-
eventsIssues = bidsTsv.validate(schema)
163+
eventsIssues = bidsTsv.validate(hedSchema)
193164
} catch (e) {
194165
eventsIssues = [convertIssue(e)]
195166
}
196-
assertErrors(eCode, altCodes, expectError, iLog, header, eventsIssues)
167+
assertErrors(expectedErrors, eventsIssues, header)
197168
}
198169

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}"`
202173
const side1 = getMergedSidecar(side, defs)
203174
let sidecarIssues = []
204175
try {
205176
const bidsSide = new BidsSidecar(`sidecar`, side1, { relativePath: 'sidecar test' })
206-
sidecarIssues = bidsSide.validate(schema)
177+
sidecarIssues = bidsSide.validate(hedSchema)
207178
} catch (e) {
208179
sidecarIssues = [convertIssue(e)]
209180
}
210-
assertErrors(eCode, altCodes, expectError, iLog, header, sidecarIssues)
181+
assertErrors(expectedErrors, sidecarIssues, header)
211182
}
212183

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}"`
216187
const hTsv = `HED\n${str}\n`
217188
let stringIssues = []
218189
try {
219190
const bidsTsv = new BidsTsvFile(`events`, hTsv, { relativePath: 'string test tsv' }, [], defs)
220-
stringIssues = bidsTsv.validate(schema)
191+
stringIssues = bidsTsv.validate(hedSchema)
221192
} catch (e) {
222193
stringIssues = [convertIssue(e)]
223194
}
224-
assertErrors(eCode, altCodes, expectError, iLog, header, stringIssues)
195+
assertErrors(expectedErrors, stringIssues, header)
225196
}
226197

227198
/**
@@ -241,16 +212,13 @@ describe('HED validation using JSON tests', () => {
241212
beforeAll(async () => {
242213
hedSchema = schemaMap.get(schema)
243214
defs = { definitions: { HED: { defList: definitions.join(',') } } }
244-
itemLog = []
245-
hasWarning = warning
215+
expectedErrors = new Set(alt_codes)
216+
expectedErrors.add(error_code)
246217
})
247218

248-
afterAll(() => {
249-
badLog.push(itemLog.join('\n'))
250-
})
219+
afterAll(() => {})
251220

252221
if (error_code in skippedErrors || name in skippedErrors) {
253-
//badLog.push(`${error_code} skipped because ${skippedErrors["error_code"]}`);
254222
test.skip(`Skipping tests ${error_code} skipped because ${skippedErrors['error_code']}`, () => {})
255223
} else {
256224
test('it should have HED schema defined', () => {
@@ -259,49 +227,49 @@ describe('HED validation using JSON tests', () => {
259227

260228
if (tests.string_tests.passes.length > 0) {
261229
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)
263231
})
264232
}
265233

266234
if (tests.string_tests.fails.length > 0) {
267235
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)
269237
})
270238
}
271239

272240
if (passedSidecars.length > 0) {
273241
test.each(passedSidecars)(`Valid sidecar: %s`, (side) => {
274-
sideValidator(error_code, alt_codes, name, side, hedSchema, defs, false, itemLog)
242+
sideValidator(side, noErrors)
275243
})
276244
}
277245

278246
if (failedSidecars.length > 0) {
279247
test.each(failedSidecars)(`Invalid sidecar: %s`, (side) => {
280-
sideValidator(error_code, alt_codes, name, side, hedSchema, defs, true, itemLog)
248+
sideValidator(side, expectedErrors)
281249
})
282250
}
283251

284252
if (passedEvents.length > 0) {
285253
test.each(passedEvents)(`Valid events: %s`, (events) => {
286-
eventsValidator(error_code, alt_codes, name, events, hedSchema, defs, false, itemLog)
254+
eventsValidator(events, noErrors)
287255
})
288256
}
289257

290258
if (failedEvents.length > 0) {
291259
test.each(failedEvents)(`Invalid events: %s`, (events) => {
292-
eventsValidator(error_code, alt_codes, name, events, hedSchema, defs, true, itemLog)
260+
eventsValidator(events, expectedErrors)
293261
})
294262
}
295263

296264
if (passedCombos.length > 0) {
297265
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)
299267
})
300268
}
301269

302270
if (failedCombos.length > 0) {
303271
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)
305273
})
306274
}
307275
}

tests/testData/tagParserTests.data.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ export const parsedHedTagTests = [
131131
takesValue: true,
132132
error: generateIssue('childRequired', { tag: 'Duration' }),
133133
},
134+
{
135+
//TODO: Special tag Event-context is unique and doesn't allow extension although parent does
136+
testname: 'invalid-tag-does-not-allow-extension',
137+
explanation: '"Sensory-event/Blech" should not have a child no recursive-extension allowed.',
138+
schemaVersion: '8.3.0',
139+
fullString: 'Duration',
140+
tagSpec: new TagSpec('Sensory-event/Blech', 0, 19, ''),
141+
tagLong: undefined,
142+
tagShort: undefined,
143+
formattedTag: undefined,
144+
canonicalTag: undefined,
145+
takesValue: true,
146+
error: generateIssue('invalidExtension', { parentTag: 'Sensory-event', tag: 'Blech' }),
147+
},
134148
{
135149
testname: 'invalid-tag-with-blank-in-extension',
136150
explanation: '" Object/blec h " has a blank in the tag extension',

0 commit comments

Comments
 (0)