Skip to content

Commit

Permalink
Merge pull request #1826 from effigies/fix/expressions
Browse files Browse the repository at this point in the history
FIX: Failing expression tests
  • Loading branch information
rwblair authored Oct 19, 2023
2 parents 53c6add + 2d6288c commit 27141af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
23 changes: 17 additions & 6 deletions bids-validator/src/schema/expressionLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function exists(list: string[], rule: string = 'dataset'): number {
if (list == null) {
return 0
}

const prefix: string[] = []

// Stimuli and subject-relative paths get prefixes
Expand Down Expand Up @@ -47,16 +51,20 @@ export const expressionFunctions = {
if (Array.isArray(operand)) {
return 'array'
}
if (typeof operand === 'undefined') {
if (typeof operand === 'undefined' || operand === null) {
return 'null'
}
return typeof operand
},
min: (list: number[]): number => {
return Math.min(...list)
min: (list: number[]): number | null => {
return list != null
? Math.min(...list.filter((x) => typeof x === 'number'))
: null
},
max: (list: number[]): number => {
return Math.max(...list)
max: (list: number[]): number | null => {
return list != null
? Math.max(...list.filter((x) => typeof x === 'number'))
: null
},
length: <T>(list: T[]): number | null => {
if (Array.isArray(list) || typeof list == 'string') {
Expand All @@ -68,7 +76,10 @@ export const expressionFunctions = {
return list.filter((x) => x === val).length
},
exists: exists,
substr: (arg: string, start: number, end: number): string => {
substr: (arg: string, start: number, end: number): string | null => {
if (arg == null || start == null || end == null) {
return null
}
return arg.substr(start, end - start)
},
sorted: <T>(list: T[]): T[] => {
Expand Down
14 changes: 12 additions & 2 deletions bids-validator/src/tests/schema-expression-language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ import { colors } from '../deps/fmt.ts'
import { BIDSContext } from '../schema/context.ts'
import { assert, assertEquals } from '../deps/asserts.ts'
import { evalCheck } from '../schema/applyRules.ts'
import { expressionFunctions } from '../schema/expressionLanguage.ts'

const schema = await loadSchema()
const pretty_null = (x: string | null): string => (x === null ? 'null' : x)

const equal = <T>(a: T, b: T): boolean => {
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((val, idx) => val === b[idx])
}
return a === b
}

Deno.test('validate schema expression tests', async (t) => {
const results: string[][] = []
const header = ['expression', 'desired', 'actual', 'result'].map((x) =>
colors.magenta(x),
)
for (const test of schema.meta.expression_tests) {
await t.step(`${test.expression} evals to ${test.result}`, () => {
const actual_result = evalCheck(test.expression, {} as BIDSContext)
if (actual_result == test.result) {
// @ts-expect-error
const context = expressionFunctions as BIDSContext
const actual_result = evalCheck(test.expression, context)
if (equal(actual_result, test.result)) {
results.push([
colors.cyan(test.expression),
pretty_null(test.result),
Expand Down

0 comments on commit 27141af

Please sign in to comment.