|
1 | | -import { assert } from '@std/assert' |
2 | | -import { contextFunction, 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' |
@@ -282,3 +282,35 @@ Deno.test('contextFunction test', async (t) => { |
282 | 282 | } |
283 | 283 | }) |
284 | 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