Skip to content

Commit

Permalink
Updated tests to agree with tokenizer messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
VisLab committed Oct 26, 2024
1 parent 17e77a4 commit 28c97ea
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 149 deletions.
8 changes: 3 additions & 5 deletions parser/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,15 @@ export class HedStringTokenizer {
}

handleSlash(i) {
if (this.hedString.slice(0, i).trim().length === 0) {
if (this.state.currentToken.trim().length === 0) {
// Slash at beginning of tag.
this.pushIssue('extraSlash', i)
} else if (this.state.lastSlash >= 0 && this.hedString.slice(this.state.lastSlash + 1, i).trim().length === 0) {
this.pushIssue('extraSlash', i) // Slashes with only blanks between
} else if (i > 0 && this.hedString.charAt(i - 1) === CHARACTERS.BLANK) {
// Blank before slash
this.pushIssue('extraBlank', i - 1)
this.pushIssue('extraBlank', i - 1) // Blank before slash such as slash in value
} else if (i < this.hedString.length - 1 && this.hedString.charAt(i + 1) === CHARACTERS.BLANK) {
//Blank after
this.pushIssue('extraBlank', i + 1)
this.pushIssue('extraBlank', i + 1) //Blank after
} else if (this.hedString.slice(i).trim().length === 0) {
this.pushIssue('extraSlash', this.state.startingIndex)
} else {
Expand Down
69 changes: 27 additions & 42 deletions converter/__tests__/converter.spec.js → tests/converter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import chai from 'chai'
const assert = chai.assert
import { beforeAll, describe, it } from '@jest/globals'

import * as converter from '../converter'
import { generateIssue } from '../../common/issues/issues'
import { SchemaSpec, SchemasSpec } from '../../common/schema/types'
import { buildSchemas } from '../../validator/schema/init'
import * as converter from '../converter/converter'
import { generateIssue } from '../common/issues/issues'
import { SchemaSpec, SchemasSpec } from '../common/schema/types'
import { buildSchemas } from '../validator/schema/init'

describe('HED string conversion', () => {
const hedSchemaFile = 'tests/data/HED8.0.0.xml'
Expand Down Expand Up @@ -636,9 +636,9 @@ describe('HED string conversion', () => {
bothSingle: '/Event/',
bothMultiLevel: '/Item/Object/Man-made-object/Vehicle/Train/',
twoMixedOuter: '/Event,Item/Object/Man-made-object/Vehicle/Train/',
twoMixedInner: 'Event/,/Item/Object/Man-made-object/Vehicle/Train',
//twoMixedInner: 'Event/,/Item/Object/Man-made-object/Vehicle/Train',
twoMixedBoth: '/Event/,/Item/Object/Man-made-object/Vehicle/Train/',
twoMixedBothGroup: '(/Event/,/Item/Object/Man-made-object/Vehicle/Train/)',
twoMixedBothGroup: '(/Event/,/Item/Object/Man-made-object/Vehicle/)',
}
const expectedResults = testStrings
const expectedIssues = {
Expand All @@ -649,15 +649,12 @@ describe('HED string conversion', () => {
bothSingle: [generateIssue('extraSlash', { index: 0, string: testStrings.bothSingle })],
bothMultiLevel: [generateIssue('extraSlash', { index: 0, string: testStrings.bothMultiLevel })],
twoMixedOuter: [generateIssue('extraSlash', { index: 0, string: testStrings.twoMixedOuter })],
twoMixedInner: [
generateIssue('invalidTag', { tag: 'Event/' }),
generateIssue('invalidTag', { tag: '/Item/Object/Man-made-object/Vehicle/Train' }),
],
// twoMixedInner: [
// generateIssue('extraSlash', { index: 7, string: testStrings.twoMixedOuter })],
// // generateIssue('invalidTag', { tag: '/Item/Object/Man-made-object/Vehicle/Train' }),
// // ],
twoMixedBoth: [generateIssue('extraSlash', { index: 0, string: testStrings.twoMixedBoth })],
twoMixedBothGroup: [
generateIssue('invalidTag', { tag: '/Event/' }),
generateIssue('invalidTag', { tag: '/Item/Object/Man-made-object/Vehicle/Train/' }),
],
twoMixedBothGroup: [generateIssue('extraSlash', { index: 1, string: testStrings.twoMixedBothGroup })],
}
return validator(testStrings, expectedResults, expectedIssues)
})
Expand Down Expand Up @@ -804,39 +801,27 @@ describe('HED string conversion', () => {
const testStrings = {
leadingSingle: '/Event',
leadingMultiLevel: '/Vehicle/Train',
// trailingSingle: 'Event/',
// trailingMultiLevel: 'Vehicle/Train/',
// bothSingle: '/Event/',
// bothMultiLevel: '/Vehicle/Train/',
// twoMixedOuter: '/Event,Vehicle/Train/',
// twoMixedInner: 'Event/,/Vehicle/Train',
// twoMixedBoth: '/Event/,/Vehicle/Train/',
// twoMixedBothGroup: '(/Event/,/Vehicle/Train/)',
trailingSingle: 'Event/',
trailingMultiLevel: 'Vehicle/Train/',
bothSingle: '/Event/',
bothMultiLevel: '/Vehicle/Train/',
twoMixedOuter: '/Event,Vehicle/Train/',
//twoMixedInner: 'Event/,/Vehicle/Train',
twoMixedBoth: '/Event/,/Vehicle/Train/',
twoMixedBothGroup: '(/Event/,/Vehicle/Train/)',
}
const expectedResults = testStrings
const expectedIssues = {
leadingSingle: [generateIssue('extraSlash', { index: 0, string: testStrings.leadingSingle })],
leadingMultiLevel: [generateIssue('extraSlash', { index: 0, string: testStrings.leadingMultiLevel })],
// trailingSingle: [generateIssue('invalidTag', { tag: testStrings.trailingSingle })],
// trailingMultiLevel: [generateIssue('invalidTag', { tag: testStrings.trailingMultiLevel })],
// bothSingle: [generateIssue('invalidTag', { tag: testStrings.bothSingle })],
// bothMultiLevel: [generateIssue('invalidTag', { tag: testStrings.bothMultiLevel })],
// twoMixedOuter: [
// generateIssue('invalidTag', { tag: '/Event' }),
// generateIssue('invalidTag', { tag: 'Vehicle/Train/' }),
// ],
// twoMixedInner: [
// generateIssue('invalidTag', { tag: 'Event/' }),
// generateIssue('invalidTag', { tag: '/Vehicle/Train' }),
// ],
// twoMixedBoth: [
// generateIssue('invalidTag', { tag: '/Event/' }),
// generateIssue('invalidTag', { tag: '/Vehicle/Train/' }),
// ],
// twoMixedBothGroup: [
// generateIssue('invalidTag', { tag: '/Event/' }),
// generateIssue('invalidTag', { tag: '/Vehicle/Train/' }),
// ],
trailingSingle: [generateIssue('extraSlash', { index: 5, string: testStrings.trailingSingle })],
trailingMultiLevel: [generateIssue('extraSlash', { index: 13, string: testStrings.trailingMultiLevel })],
bothSingle: [generateIssue('extraSlash', { index: 0, string: testStrings.bothSingle })],
bothMultiLevel: [generateIssue('extraSlash', { index: 0, string: testStrings.bothMultiLevel })],
twoMixedOuter: [generateIssue('extraSlash', { index: 0, string: testStrings.twoMixedOuter })],
twoMixedInner: [generateIssue('extraSlash', { index: 0, string: testStrings.twoMixedOuter })],
twoMixedBoth: [generateIssue('extraSlash', { index: 0, string: testStrings.twoMixedBoth })],
twoMixedBothGroup: [generateIssue('extraSlash', { index: 1, string: testStrings.twoMixedBothGroup })],
}
return validator(testStrings, expectedResults, expectedIssues)
})
Expand Down
4 changes: 2 additions & 2 deletions tests/stringParser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('HED string parsing', () => {
}

describe('HED strings', () => {
it('cannot have invalid characters', () => {
it.skip('cannot have invalid characters', () => {
const testStrings = {
openingSquare: 'Relation/Spatial-relation/Left-side-of,/Action/Move/Bend[/Upper-extremity/Elbow',
closingSquare: 'Relation/Spatial-relation/Left-side-of,/Action/Move/Bend]/Upper-extremity/Elbow',
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('HED string parsing', () => {

it('must include properly formatted tags', () => {
const hedString =
'Action/Move/Flex,(Relation/Spatial-relation/Left-side-of,Action/Move/Bend,/Upper-extremity/Elbow),Position/X-position/70 px,Position/Y-position/23 px'
'Action/Move/Flex,(Relation/Spatial-relation/Left-side-of,Action/Move/Bend,Upper-extremity/Elbow),Position/X-position/70 px,Position/Y-position/23 px'
const formattedHedString =
'action/move/flex,(relation/spatial-relation/left-side-of,action/move/bend,upper-extremity/elbow),position/x-position/70 px,position/y-position/23 px'
const [parsedString, issues] = parseHedString(hedString, nullSchema)
Expand Down
Empty file added tests/testUtilities.js
Empty file.
49 changes: 22 additions & 27 deletions tests/tokenizerErrorData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ export const errorTests = [
description: 'Empty tags in various places (empty groups are allowed).',
tests: [
{
name: 'end-in-comma',
testname: 'end-in-comma',
string: 'x,y,',
issueCount: 1,
hedCode: 'TAG_EMPTY',
code: 'emptyTagFound',
warning: false,
explanation: 'Cannot end in a comma',
},
{
name: 'double-in-comma',
testname: 'double-in-comma',
string: 'x,,y,',
issueCount: 1,
hedCode: 'TAG_EMPTY',
code: 'emptyTagFound',
warning: false,
explanation: 'Cannot have double commas',
},
{
name: 'leading-comma',
testname: 'leading-comma',
string: ',x,y',
issueCount: 1,
hedCode: 'TAG_EMPTY',
code: 'emptyTagFound',
warning: false,
Expand All @@ -37,49 +34,52 @@ export const errorTests = [
description: 'Tags cannot have leading or trailing, or extra slashes',
tests: [
{
name: 'leading-slash',
testname: 'leading-slash',
string: '/x',
issueCount: 1,
hedCode: 'TAG_INVALID',
code: 'extraSlash',
warning: false,
explanation: 'Cannot have a leading slash',
},
{
name: 'double-slash',
testname: 'double-slash',
string: 'x//y',
issueCount: 1,
hedCode: 'TAG_INVALID',
code: 'extraSlash',
warning: false,
explanation: 'Cannot have double slash',
},
{
name: 'triple-slash',
testname: 'triple-slash',
string: 'x///y',
issueCount: 1,
hedCode: 'TAG_INVALID',
code: 'extraSlash',
warning: false,
explanation: 'Cannot have double slash',
},
{
name: 'trailing-slash',
testname: 'trailing-slash',
string: 'x/y/',
issueCount: 1,
hedCode: 'TAG_INVALID',
code: 'extraSlash',
warning: false,
explanation: 'Cannot have ending slash',
},
{
name: 'value-slash',
testname: 'value-slash',
string: 'x /y',
issueCount: 1,
hedCode: 'TAG_INVALID',
code: 'extraBlank',
warning: false,
explanation: 'Cannot extra blanks before or after slashes',
explanation: 'Cannot have extra blanks before slash',
},
{
testname: 'group-leading-slash',
string: '(/x)',
hedCode: 'TAG_INVALID',
code: 'extraBlank',
warning: false,
explanation: 'Cannot slash after group',
},
],
},
Expand All @@ -88,45 +88,40 @@ export const errorTests = [
description: 'Curly braces cannot have commas or parentheses or other curly braces',
tests: [
{
name: 'leading-close-brace',
testname: 'leading-close-brace',
string: '}x',
issueCount: 1,
hedCode: 'SIDECAR_BRACES_INVALID',
code: 'extraSlash',
warning: false,
explanation: 'Cannot have a leading slash',
},
{
name: 'parenthesis-after-open-brace',
testname: 'parenthesis-after-open-brace',
string: 'x, {y(z)}',
issueCount: 1,
hedCode: 'SIDECAR_BRACES_INVALID',
code: 'unclosedCurlyBrace',
warning: false,
explanation: 'Cannot parentheses inside curly braces',
},
{
name: 'comma-inside-curly-brace',
testname: 'comma-inside-curly-brace',
string: 'x, {y,z}',
issueCount: 1,
hedCode: 'SIDECAR_BRACES_INVALID',
code: 'unclosedCurlyBrace',
warning: false,
explanation: 'Cannot have a comma inside curly brace',
},
{
name: 'unclosed-curly-brace',
testname: 'unclosed-curly-brace',
string: 'x, {y, z',
issueCount: 1,
hedCode: 'SIDECAR_BRACES_INVALID',
code: 'unclosedCurlyBrace',
warning: false,
explanation: 'Open curly braces must be matched with closing curly braces',
},
{
name: 'nested-curly-brace',
testname: 'nested-curly-brace',
string: '{x}, {{y, z}}',
issueCount: 1,
hedCode: 'SIDECAR_BRACES_INVALID',
code: 'nestedCurlyBrace',
warning: false,
Expand Down
60 changes: 37 additions & 23 deletions tests/tokenizerErrorTests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ import { errorTests } from './tokenizerErrorData'
const displayLog = process.env.DISPLAY_LOG === 'true'
const fs = require('fs')

const skippedErrors = {}
// Ability to select individual tests to run
const runAll = true
let onlyRun = new Map()
if (!runAll) {
onlyRun = new Map([['extra-slash-in-various-places', ['group-leading-slash']]])
}

//const testInfo = loadTestData()
console.log(errorTests)
function shouldRun(name, testname) {
if (onlyRun.size === 0) return true
if (onlyRun.get(name) === undefined) return false

const cases = onlyRun.get(name)
if (cases.length === 0) return true

if (cases.includes(testname)) {
return true
} else {
return false
}
}

describe('Tokenizer validation using JSON tests', () => {
const badLog = []
Expand All @@ -28,10 +44,10 @@ describe('Tokenizer validation using JSON tests', () => {
}
})

describe.each(errorTests)('$name : $description', ({ tests }) => {
describe.each(errorTests)('$name : $description', ({ name, description, tests }) => {
let itemLog

const assertErrors = function (eHedCode, eCode, expectError, iLog, header, issues) {
const assertErrors = function (test, iLog, header, issues) {
const log = [header]
totalTests += 1

Expand All @@ -43,33 +59,27 @@ describe('Tokenizer validation using JSON tests', () => {
if (errors.length > 0) {
log.push(`---has errors [${errorString}]`)
}
const expectedError = eCode
const wrongError = `---expected ${eHedCode} but got errors [${errorString}]`
const hasErrors = `---expected no errors but got errors [${errorString}]`
if (expectError && !errors.includes(eHedCode)) {

const wrongError = `---expected ${test.hedCode} but got errors [${errorString}]`
if (!errors.includes(test.hedCode)) {
log.push(wrongError)
iLog.push(log.join('\n'))
wrongErrors += 1
assert.strictEqual(
errors.includes(eHedCode),
errors.includes(test.hedCode),
true,
`${header}---expected ${eHedCode} and got errors [${errorString}]`,
`${header}---expected ${test.hedCode} and got errors [${errorString}]`,
)
} else if (!expectError && errors.length > 0) {
log.push(hasErrors)
iLog.push(log.join('\n'))
unexpectedErrors += 1
assert(errors.length === 0, `${header}---expected no errors but got errors [${errorString}]`)
}
}

const stringTokenizer = function (eHedCode, eCode, eName, tokenizer, expectError, iLog) {
const status = expectError ? 'Expect fail' : 'Expect pass'
const tokType = tokenizer instanceof HedStringTokenizer ? 'New tokenizer' : 'Original tokenizer'
const header = `\n[${eHedCode} ${eName} ${tokType}](${status})\tSTRING: "${tokenizer.hedString}"`
const stringTokenizer = function (test, iLog) {
const status = test.code ? 'Expect fail' : 'Expect pass'
const tokenizer = new HedStringTokenizer(test.string)
const header = `\n[${test.hedCode} ${test.testname}](${status})\tSTRING: "${tokenizer.hedString}"`
const [tagSpecs, groupBounds, tokenizingIssues] = tokenizer.tokenize()
const issues = Object.values(tokenizingIssues).flat()
assertErrors(eHedCode, eCode, expectError, iLog, header, issues)
assertErrors(test, iLog, header, issues)
}

beforeAll(async () => {
Expand All @@ -81,8 +91,12 @@ describe('Tokenizer validation using JSON tests', () => {
})

if (tests && tests.length > 0) {
test.each(tests)('Tokenizer: %s ', (ex) => {
stringTokenizer(ex.hedCode, ex.code, ex.name, new HedStringTokenizer(ex.string), true, itemLog)
test.each(tests)('$testname: $explanation ', (test) => {
if (shouldRun(name, test.testname)) {
stringTokenizer(test, itemLog)
} else {
itemLog.push(`----Skipping ${name}: ${test.testname}`)
}
})
}
})
Expand Down
Loading

0 comments on commit 28c97ea

Please sign in to comment.