Skip to content

Commit

Permalink
Replace remaining possible uses of promises with async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
happy5214 committed Jul 29, 2024
1 parent 91033e1 commit 22729a5
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 197 deletions.
59 changes: 28 additions & 31 deletions tests/bids.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ describe('BIDS datasets', () => {
*/
const validator = (testDatasets, expectedIssues, versionSpec) => {
return Promise.all(
Object.entries(testDatasets).map(([datasetName, dataset]) => {
Object.entries(testDatasets).map(async ([datasetName, dataset]) => {
assert.property(expectedIssues, datasetName, datasetName + ' is not in expectedIssues')
return validateBidsDataset(dataset, versionSpec).then((issues) => {
assert.sameDeepMembers(issues, expectedIssues[datasetName], datasetName)
})
const issues = await validateBidsDataset(dataset, versionSpec)
assert.sameDeepMembers(issues, expectedIssues[datasetName], datasetName)
}),
)
}
Expand All @@ -55,16 +54,15 @@ describe('BIDS datasets', () => {
*/
const validatorWithSpecs = (testDatasets, expectedIssues, versionSpecs) => {
return Promise.all(
Object.entries(testDatasets).map(([datasetName, dataset]) => {
Object.entries(testDatasets).map(async ([datasetName, dataset]) => {
assert.property(expectedIssues, datasetName, datasetName + ' is not in expectedIssues')
let specs = versionSpecs
if (versionSpecs) {
assert.property(versionSpecs, datasetName, datasetName + ' is not in versionSpecs')
specs = versionSpecs[datasetName]
}
return validateBidsDataset(dataset, specs).then((issues) => {
assert.sameDeepMembers(issues, expectedIssues[datasetName], datasetName)
})
const issues = await validateBidsDataset(dataset, specs)
assert.sameDeepMembers(issues, expectedIssues[datasetName], datasetName)
}),
)
}
Expand Down Expand Up @@ -673,7 +671,7 @@ describe('BIDS datasets', () => {
return validator(testDatasets, expectedIssues, specs)
}, 10000)

it('should splice strings by replacing placeholders and deleting "n/a" values', () => {
it('should splice strings by replacing placeholders and deleting "n/a" values', async () => {
const tsvFiles = bidsTsvFiles[10]
const expectedStrings = [
'Label/1, (Def/Acc/3.5 m-per-s^2), (Item-count/2, Label/1)',
Expand All @@ -684,28 +682,27 @@ describe('BIDS datasets', () => {
'(Red, Blue), (Green, (Yellow))',
]
const dataset = new BidsDataset(tsvFiles, [])
return buildBidsSchemas(dataset, specs).then((hedSchemas) => {
const parsedExpectedStrings = []
for (const expectedString of expectedStrings) {
const [parsedExpectedString, parsingIssues] = parseHedString(expectedString, hedSchemas)
assert.isEmpty(Object.values(parsingIssues).flat(), `String "${expectedString}" failed to parse`)
parsedExpectedStrings.push(parsedExpectedString)
}
const tsvHedStrings = []
for (const tsvFile of tsvFiles) {
tsvFile.mergedSidecar.parseHedStrings(hedSchemas)
const tsvValidator = new BidsHedTsvParser(tsvFile, hedSchemas)
const tsvHed = tsvValidator.parse()
assert.isEmpty(tsvValidator.issues, 'TSV file failed to parse')
tsvHedStrings.push(...tsvHed)
}
const formatMap = (hedString) => hedString.format()
assert.deepStrictEqual(
tsvHedStrings.map(formatMap),
parsedExpectedStrings.map(formatMap),
'Mismatch in parsed strings',
)
})
const hedSchemas = await buildBidsSchemas(dataset, specs)
const parsedExpectedStrings = []
for (const expectedString of expectedStrings) {
const [parsedExpectedString, parsingIssues] = parseHedString(expectedString, hedSchemas)
assert.isEmpty(Object.values(parsingIssues).flat(), `String "${expectedString}" failed to parse`)
parsedExpectedStrings.push(parsedExpectedString)
}
const tsvHedStrings = []
for (const tsvFile of tsvFiles) {
tsvFile.mergedSidecar.parseHedStrings(hedSchemas)
const tsvValidator = new BidsHedTsvParser(tsvFile, hedSchemas)
const tsvHed = tsvValidator.parse()
assert.isEmpty(tsvValidator.issues, 'TSV file failed to parse')
tsvHedStrings.push(...tsvHed)
}
const formatMap = (hedString) => hedString.format()
assert.deepStrictEqual(
tsvHedStrings.map(formatMap),
parsedExpectedStrings.map(formatMap),
'Mismatch in parsed strings',
)
}, 10000)
})

Expand Down
9 changes: 3 additions & 6 deletions tests/dataset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { SchemaSpec, SchemasSpec } from '../common/schema/types'
describe('HED dataset validation', () => {
const hedSchemaFile = 'tests/data/HED8.2.0.xml'
const hedLibrarySchemaFile = 'tests/data/HED_testlib_2.0.0.xml'
let hedSchemaPromise
let hedSchemas

beforeAll(() => {
beforeAll(async () => {
const spec1 = new SchemaSpec('', '8.2.0', '', hedSchemaFile)
const spec2 = new SchemaSpec('testlib', '2.0.0', 'testlib', hedLibrarySchemaFile)
const specs = new SchemasSpec().addSchemaSpec(spec1).addSchemaSpec(spec2)
hedSchemaPromise = buildSchemas(specs)
hedSchemas = await buildSchemas(specs)
})

describe('Basic HED string lists', () => {
Expand All @@ -27,7 +27,6 @@ describe('HED dataset validation', () => {
* @param {Object<string, Issue[]>} expectedIssues The expected issues.
*/
const validator = async function (testDatasets, expectedIssues) {
const hedSchemas = await hedSchemaPromise
for (const [testDatasetKey, testDataset] of Object.entries(testDatasets)) {
assert.property(expectedIssues, testDatasetKey, testDatasetKey + ' is not in expectedIssues')
const [, testIssues] = hed.validateHedEvents(testDataset, hedSchemas, null, true)
Expand Down Expand Up @@ -81,7 +80,6 @@ describe('HED dataset validation', () => {
* @param {Object<string, Issue[]>} expectedIssues The expected issues.
*/
const validator = async function (testDatasets, expectedIssues) {
const hedSchemas = await hedSchemaPromise
for (const [testDatasetKey, testDataset] of Object.entries(testDatasets)) {
assert.property(expectedIssues, testDatasetKey, testDatasetKey + ' is not in expectedIssues')
const [, testIssues] = hed.validateHedDataset(testDataset, hedSchemas, true)
Expand Down Expand Up @@ -218,7 +216,6 @@ describe('HED dataset validation', () => {
* @param {Object<string, Issue[]>} expectedIssues The expected issues.
*/
const validator = async function (testDatasets, testContext, expectedIssues) {
const hedSchemas = await hedSchemaPromise
for (const [testDatasetKey, testDataset] of Object.entries(testDatasets)) {
assert.property(expectedIssues, testDatasetKey, testDatasetKey + ' is not in expectedIssues')
const [, testIssues] = hed.validateHedDatasetWithContext(testDataset, testContext, hedSchemas, true)
Expand Down
66 changes: 26 additions & 40 deletions tests/event.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ describe('HED string and event validation', () => {
describe('HED-2G validation', () => {
describe('Later HED-2G schemas', () => {
const hedSchemaFile = 'tests/data/HED7.1.1.xml'
let hedSchemaPromise
let hedSchemas

beforeAll(() => {
beforeAll(async () => {
const spec1 = new SchemaSpec('', '7.1.1', '', hedSchemaFile)
const specs = new SchemasSpec().addSchemaSpec(spec1)
hedSchemaPromise = buildSchemas(specs)
hedSchemas = await buildSchemas(specs)
})

/**
Expand All @@ -357,9 +357,7 @@ describe('HED string and event validation', () => {
* @param {Object<string, boolean>?} testOptions Any needed custom options for the validator.
*/
const validatorSemanticBase = function (testStrings, expectedIssues, testFunction, testOptions = {}) {
return hedSchemaPromise.then((hedSchemas) => {
validatorBase(hedSchemas, Hed2Validator, testStrings, expectedIssues, testFunction, testOptions)
})
validatorBase(hedSchemas, Hed2Validator, testStrings, expectedIssues, testFunction, testOptions)
}

describe('Full HED Strings', () => {
Expand Down Expand Up @@ -686,13 +684,11 @@ describe('HED string and event validation', () => {

describe('HED Strings', () => {
const validator = function (testStrings, expectedIssues, expectValuePlaceholderString = false) {
return hedSchemaPromise.then((hedSchemas) => {
for (const [testStringKey, testString] of Object.entries(testStrings)) {
assert.property(expectedIssues, testStringKey, testStringKey + ' is not in expectedIssues')
const [, testIssues] = hed.validateHedString(testString, hedSchemas, true, expectValuePlaceholderString)
assert.sameDeepMembers(testIssues, expectedIssues[testStringKey], testString)
}
})
for (const [testStringKey, testString] of Object.entries(testStrings)) {
assert.property(expectedIssues, testStringKey, testStringKey + ' is not in expectedIssues')
const [, testIssues] = hed.validateHedString(testString, hedSchemas, true, expectValuePlaceholderString)
assert.sameDeepMembers(testIssues, expectedIssues[testStringKey], testString)
}
}

it('should skip tag group-level checks', () => {
Expand All @@ -711,12 +707,12 @@ describe('HED string and event validation', () => {

describe('Pre-v7.1.0 HED schemas', () => {
const hedSchemaFile = 'tests/data/HED7.0.4.xml'
let hedSchemaPromise
let hedSchemas

beforeAll(() => {
beforeAll(async () => {
const spec2 = new SchemaSpec('', '7.0.4', '', hedSchemaFile)
const specs = new SchemasSpec().addSchemaSpec(spec2)
hedSchemaPromise = buildSchemas(specs)
hedSchemas = await buildSchemas(specs)
})

/**
Expand All @@ -730,9 +726,7 @@ describe('HED string and event validation', () => {
* @param {Object<string, boolean>?} testOptions Any needed custom options for the validator.
*/
const validatorSemanticBase = function (testStrings, expectedIssues, testFunction, testOptions = {}) {
return hedSchemaPromise.then((hedSchemas) => {
validatorBase(hedSchemas, Hed2Validator, testStrings, expectedIssues, testFunction, testOptions)
})
validatorBase(hedSchemas, Hed2Validator, testStrings, expectedIssues, testFunction, testOptions)
}

describe('Individual HED Tags', () => {
Expand Down Expand Up @@ -850,12 +844,12 @@ describe('HED string and event validation', () => {

describe('HED-3G validation', () => {
const hedSchemaFile = 'tests/data/HED8.2.0.xml'
let hedSchemaPromise
let hedSchemas

beforeAll(() => {
beforeAll(async () => {
const spec3 = new SchemaSpec('', '8.2.0', '', hedSchemaFile)
const specs = new SchemasSpec().addSchemaSpec(spec3)
hedSchemaPromise = buildSchemas(specs)
hedSchemas = await buildSchemas(specs)
})

/**
Expand Down Expand Up @@ -894,9 +888,7 @@ describe('HED string and event validation', () => {
* @param {Object<string, boolean>?} testOptions Any needed custom options for the validator.
*/
const validatorSemanticBase = function (testStrings, expectedIssues, testFunction, testOptions = {}) {
return hedSchemaPromise.then((hedSchemas) => {
validatorBase(hedSchemas, testStrings, expectedIssues, testFunction, testOptions)
})
validatorBase(hedSchemas, testStrings, expectedIssues, testFunction, testOptions)
}

describe('Full HED Strings', () => {
Expand Down Expand Up @@ -1762,28 +1754,26 @@ describe('HED string and event validation', () => {
generateIssue('invalidPlaceholder', { tag: 'Time-value/#' }),
],
}
return Promise.all([
validatorSemantic(expectedPlaceholdersTestStrings, expectedPlaceholdersIssues, true),
validatorSemantic(noExpectedPlaceholdersTestStrings, noExpectedPlaceholdersIssues, false),
])
validatorSemantic(expectedPlaceholdersTestStrings, expectedPlaceholdersIssues, true)
validatorSemantic(noExpectedPlaceholdersTestStrings, noExpectedPlaceholdersIssues, false)
})
})
})

describe('HED-3G library and partnered schema validation', () => {
const hedLibrary2SchemaFile = 'tests/data/HED_testlib_2.0.0.xml'
const hedLibrary3SchemaFile = 'tests/data/HED_testlib_3.0.0.xml'
let hedSchemaPromise, hedSchemaPromise2
let hedSchemas, hedSchemas2

beforeAll(() => {
beforeAll(async () => {
const spec4 = new SchemaSpec('testlib', '2.0.0', 'testlib', hedLibrary2SchemaFile)
const spec5 = new SchemaSpec('testlib', '3.0.0', 'testlib', hedLibrary3SchemaFile)
const spec6 = new SchemaSpec('', '2.0.0', 'testlib', hedLibrary2SchemaFile)
const spec7 = new SchemaSpec('', '3.0.0', 'testlib', hedLibrary3SchemaFile)
const specs = new SchemasSpec().addSchemaSpec(spec4).addSchemaSpec(spec5)
const specs2 = new SchemasSpec().addSchemaSpec(spec6).addSchemaSpec(spec7)
hedSchemaPromise = buildSchemas(specs)
hedSchemaPromise2 = buildSchemas(specs2)
hedSchemas = await buildSchemas(specs)
hedSchemas2 = await buildSchemas(specs2)
})

/**
Expand Down Expand Up @@ -1822,16 +1812,14 @@ describe('HED string and event validation', () => {
* @param {Object<string, boolean>?} testOptions Any needed custom options for the validator.
*/
const validatorSemanticBase = function (testStrings, expectedIssues, testFunction, testOptions = {}) {
return hedSchemaPromise.then((hedSchemas) => {
validatorBase(hedSchemas, testStrings, expectedIssues, testFunction, testOptions)
})
validatorBase(hedSchemas, testStrings, expectedIssues, testFunction, testOptions)
}

describe('Full HED Strings', () => {
const validatorSemantic = validatorSemanticBase

/**
* HED 3 semantic validation function using the alternate schema Promise object.
* HED 3 semantic validation function using the alternative schema collection.
*
* This base function uses the HED 3-specific {@link Hed3Validator} validator class.
*
Expand All @@ -1841,9 +1829,7 @@ describe('HED string and event validation', () => {
* @param {Object<string, boolean>?} testOptions Any needed custom options for the validator.
*/
const validatorSemantic2 = function (testStrings, expectedIssues, testFunction, testOptions = {}) {
return hedSchemaPromise2.then((hedSchemas) => {
validatorBase(hedSchemas, testStrings, expectedIssues, testFunction, testOptions)
})
validatorBase(hedSchemas2, testStrings, expectedIssues, testFunction, testOptions)
}

it('should allow combining tags from multiple partnered schemas', () => {
Expand Down
Loading

0 comments on commit 22729a5

Please sign in to comment.