Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Failing expression tests #1826

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading