|
1 | | -import { assert } from '@std/assert' |
2 | | -import { expressionFunctions } from './expressionLanguage.ts' |
| 1 | +import { assert, assertEquals } from '@std/assert' |
| 2 | +import { contextFunction, expressionFunctions, formatter, prepareContext } from './expressionLanguage.ts' |
3 | 3 | import { dataFile, rootFileTree } from './fixtures.test.ts' |
4 | 4 | import { BIDSContext } from './context.ts' |
5 | 5 | import type { DatasetIssues } from '../issues/datasetIssues.ts' |
@@ -235,3 +235,82 @@ Deno.test('test expression functions', async (t) => { |
235 | 235 | assert(allequal([1, 2, 1, 2], [1, 2, 1]) === false) |
236 | 236 | }) |
237 | 237 | }) |
| 238 | + |
| 239 | +Deno.test('contextFunction test', async (t) => { |
| 240 | + const match = expressionFunctions.match |
| 241 | + await t.step('check veridical reconstruction of match expressions', () => { |
| 242 | + // match() functions frequently contain escapes in regex patterns |
| 243 | + // We receive these from the schema as written in YAML, so we need to ensure |
| 244 | + // that they are correctly reconstructed in the contextFunction() function |
| 245 | + // |
| 246 | + // The goal is to avoid schema authors needing to double-escape their regex patterns |
| 247 | + // and other implementations to account for the double-escaping |
| 248 | + let pattern = String.raw`^\.nii(\.gz)?$` |
| 249 | + |
| 250 | + // Check both a literal and a variable pattern produce the same result |
| 251 | + for (const check of ['match(extension, pattern)', `match(extension, '${pattern}')`]) { |
| 252 | + const niftiCheck = contextFunction(check) |
| 253 | + for ( |
| 254 | + const { extension, expected } of [ |
| 255 | + { extension: '.nii', expected: true }, |
| 256 | + { extension: '.nii.gz', expected: true }, |
| 257 | + { extension: '.tsv', expected: false }, |
| 258 | + { extension: ',nii,gz', expected: false }, // Check that . is not treated as a wildcard |
| 259 | + ] |
| 260 | + ) { |
| 261 | + assert(match(extension, pattern) === expected) |
| 262 | + // Pass in a context object to provide any needed variables |
| 263 | + assert(niftiCheck({ match, extension, pattern } as unknown as BIDSContext) === expected) |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + pattern = String.raw`\S` |
| 268 | + for (const check of ['match(json.Name, pattern)', `match(json.Name, '${pattern}')`]) { |
| 269 | + const nonEmptyCheck = contextFunction(check) |
| 270 | + for ( |
| 271 | + const { Name, expected } of [ |
| 272 | + { Name: 'test', expected: true }, |
| 273 | + { Name: '', expected: false }, |
| 274 | + { Name: ' ', expected: false }, |
| 275 | + ] |
| 276 | + ) { |
| 277 | + assert(match(Name, pattern) === expected) |
| 278 | + assert( |
| 279 | + nonEmptyCheck({ match, json: { Name }, pattern } as unknown as BIDSContext) === expected, |
| 280 | + ) |
| 281 | + } |
| 282 | + } |
| 283 | + }) |
| 284 | +}) |
| 285 | + |
| 286 | +Deno.test('formatter test', async (t) => { |
| 287 | + await t.step('simple strings', () => { |
| 288 | + const context = {} as BIDSContext |
| 289 | + for ( |
| 290 | + const str of [ |
| 291 | + 'simple string', |
| 292 | + 'string with `backticks` and \\back\\slashes', |
| 293 | + ] |
| 294 | + ) { |
| 295 | + assertEquals(formatter(str)(context), str) |
| 296 | + } |
| 297 | + }) |
| 298 | + await t.step('format strings', () => { |
| 299 | + const context = prepareContext( |
| 300 | + {a: 'stringa', b: 'stringb', c: 3, d: {e: 4}, f: [0, 1, 2], g: [1, 2, 3]} as unknown as BIDSContext |
| 301 | + ) |
| 302 | + for (const [str, expected] of [ |
| 303 | + ['{a}', 'stringa'], |
| 304 | + ['`{a}`', '`stringa`'], // Backticks are preserved |
| 305 | + ['`````{a}`````', '`````stringa`````'], |
| 306 | + ['{a} and {b} and {c}', 'stringa and stringb and 3'], |
| 307 | + ['{a}\\n{d.e}', 'stringa\\n4'], // Backslashes are preserved |
| 308 | + ['{intersects(f, g)}', '1,2'], // expressions are evaluated |
| 309 | + ['{z}', 'undefined'], |
| 310 | + // Unsupported Pythonisms |
| 311 | + // ['{{a}}', '{a}'], |
| 312 | + ]) { |
| 313 | + assertEquals(formatter(str)(context), expected) |
| 314 | + } |
| 315 | + }) |
| 316 | +}) |
0 commit comments