Skip to content

Commit

Permalink
Fix duplicate tag check and port JSON tests to event spec
Browse files Browse the repository at this point in the history
  • Loading branch information
happy5214 committed Sep 13, 2024
1 parent ad5cf11 commit 957f95d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
7 changes: 2 additions & 5 deletions parser/parsedHedGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,12 @@ export class ParsedHedGroup extends ParsedHedSubstring {
* @yields {ParsedHedTag[]} The subgroups of this tag group.
*/
*subGroupArrayIterator() {
const currentGroup = []
for (const innerTag of this.tags) {
if (innerTag instanceof ParsedHedTag) {
currentGroup.push(innerTag)
} else if (innerTag instanceof ParsedHedGroup) {
if (innerTag instanceof ParsedHedGroup) {
yield* innerTag.subGroupArrayIterator()
}
}
yield currentGroup
yield this.tags
}

/**
Expand Down
52 changes: 39 additions & 13 deletions tests/event.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ describe('HED string and event validation', () => {
*
* @param {Object<string, string>} testStrings A mapping of test strings.
* @param {Object<string, Issue[]>} expectedIssues The expected issues for each test string.
* @param {function(HedValidator, ParsedHedTag[]): void} testFunction A test-specific function that executes the required validation check.
* @param {function(HedValidator, ParsedHedSubstring[]): void} testFunction A test-specific function that executes the required validation check.
* @param {Object<string, boolean>?} testOptions Any needed custom options for the validator.
*/
const validatorSyntactic = function (testStrings, expectedIssues, testFunction, testOptions = {}) {
Expand All @@ -279,26 +279,34 @@ describe('HED string and event validation', () => {
testFunction(validator, subGroup)
}
}
testFunction(validator, validator.parsedString.topLevelTags)
testFunction(validator, validator.parsedString.parseTree)
},
testOptions,
)
}

it('should not contain duplicates', () => {
const testStrings = {
//topLevelDuplicate: 'Event/Category/Experimental stimulus,Event/Category/Experimental stimulus',
groupDuplicate:
'Item/Object/Vehicle/Train,(Event/Category/Experimental stimulus,Attribute/Visual/Color/Purple,Event/Category/Experimental stimulus)',
nestedGroupDuplicate:
'Item/Object/Vehicle/Train,(Attribute/Visual/Color/Purple,(Event/Category/Experimental stimulus,Event/Category/Experimental stimulus))',
noDuplicate: 'Event/Category/Experimental stimulus,Item/Object/Vehicle/Train,Attribute/Visual/Color/Purple',
legalDuplicate: 'Item/Object/Vehicle/Train,(Item/Object/Vehicle/Train,Event/Category/Experimental stimulus)',
nestedLegalDuplicate:
'(Item/Object/Vehicle/Train,(Item/Object/Vehicle/Train,Event/Category/Experimental stimulus))',
legalDuplicateDifferentValue: '(Attribute/Language/Unit/Word/Brain,Attribute/Language/Unit/Word/Study)',
twoNonDuplicateGroups: '(Red, Blue, (Green)), (Red, Blue, ((Green)))',
//topLevelDuplicate: 'Event/Category/Experimental stimulus,Event/Category/Experimental stimulus',
groupDuplicate:
'Item/Object/Vehicle/Train,(Event/Category/Experimental stimulus,Attribute/Visual/Color/Purple,Event/Category/Experimental stimulus)',
nestedGroupDuplicate:
'Item/Object/Vehicle/Train,(Attribute/Visual/Color/Purple,(Event/Category/Experimental stimulus,Event/Category/Experimental stimulus))',
twoDuplicateGroups: '(Red, Blue, (Green)), (Red, Blue, (Green))',
repeatedGroup: '(Red, (Blue, Green, (Yellow)), Red, (Blue, Green, (Yellow)))',
}
const expectedIssues = {
noDuplicate: [],
legalDuplicate: [],
nestedLegalDuplicate: [],
legalDuplicateDifferentValue: [],
twoNonDuplicateGroups: [],
topLevelDuplicate: [
generateIssue('duplicateTag', {
tag: 'Event/Category/Experimental stimulus',
Expand All @@ -323,10 +331,28 @@ describe('HED string and event validation', () => {
tag: 'Event/Category/Experimental stimulus',
}),
],
noDuplicate: [],
legalDuplicate: [],
nestedLegalDuplicate: [],
legalDuplicateDifferentValue: [],
twoDuplicateGroups: [
generateIssue('duplicateTag', {
tag: '(Red, Blue, (Green))',
}),
generateIssue('duplicateTag', {
tag: '(Red, Blue, (Green))',
}),
],
repeatedGroup: [
generateIssue('duplicateTag', {
tag: 'Red',
}),
generateIssue('duplicateTag', {
tag: 'Red',
}),
generateIssue('duplicateTag', {
tag: '(Blue, Green, (Yellow))',
}),
generateIssue('duplicateTag', {
tag: '(Blue, Green, (Yellow))',
}),
],
}
validatorSyntactic(testStrings, expectedIssues, (validator, tagLevel) => {
validator.checkForDuplicateTags(tagLevel)
Expand Down Expand Up @@ -593,7 +619,7 @@ describe('HED string and event validation', () => {
*
* @param {Object<string, string>} testStrings A mapping of test strings.
* @param {Object<string, Issue[]>} expectedIssues The expected issues for each test string.
* @param {function(HedValidator, ParsedHedTag[]): void} testFunction A test-specific function that executes the required validation check.
* @param {function(HedValidator, ParsedHedSubstring[]): void} testFunction A test-specific function that executes the required validation check.
* @param {Object<string, boolean>?} testOptions Any needed custom options for the validator.
*/
const validatorSemantic = function (testStrings, expectedIssues, testFunction, testOptions = {}) {
Expand All @@ -606,7 +632,7 @@ describe('HED string and event validation', () => {
testFunction(validator, subGroup)
}
}
testFunction(validator, validator.parsedString.topLevelTags)
testFunction(validator, validator.parsedString.parseTree)
},
testOptions,
)
Expand Down
6 changes: 4 additions & 2 deletions validator/event/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class HedValidator {
validateStringLevel() {
this.options.isEventLevel = false
this.validateIndividualHedTags()
this.validateHedTagLevels()
this.validateHedTagGroups()
}

Expand Down Expand Up @@ -99,7 +100,7 @@ export class HedValidator {
this.validateHedTagLevel(subGroup)
}
}
this.validateHedTagLevel(this.parsedString.topLevelTags)
this.validateHedTagLevel(this.parsedString.parseTree)
}

/**
Expand Down Expand Up @@ -174,8 +175,9 @@ export class HedValidator {
* Check for multiple instances of a unique tag.
*/
checkForMultipleUniqueTags(tagList) {
const actualTagList = tagList.filter((tagOrGroup) => tagOrGroup instanceof ParsedHedTag)
this._checkForTagAttribute(uniqueType, (uniqueTagPrefix) => {
if (tagList.filter((tag) => tag.formattedTag.startsWith(uniqueTagPrefix)).length > 1) {
if (actualTagList.filter((tag) => tag.formattedTag.startsWith(uniqueTagPrefix)).length > 1) {
this.pushIssue('multipleUniqueTags', {
tag: uniqueTagPrefix,
})
Expand Down

0 comments on commit 957f95d

Please sign in to comment.