silphius/app/astride/evaluators/conditional.test.js
2024-06-22 08:02:23 -05:00

44 lines
1.2 KiB
JavaScript

import {expect, test} from 'vitest';
import evaluate from '@/astride/evaluate.js';
import expression from '@/astride/test/expression.js';
const scopeTest = test.extend({
scope: async ({}, use) => {
await use({
S: {O: {}},
get(k) { return this.S[k]; },
set(k, v) { return this.S[k] = v; }
});
},
});
scopeTest('evaluates conditional expression', async ({scope}) => {
scope.set('x', true);
let evaluated;
evaluated = evaluate(await expression('x ? 2 : 3'), {scope});
expect(evaluated.value)
.to.equal(2);
scope.set('x', false);
evaluated = evaluate(await expression('x ? 2 : 3'), {scope});
expect(evaluated.value)
.to.equal(3);
});
scopeTest('evaluates async conditional expression', async ({scope}) => {
scope.set('x', true);
let evaluated;
evaluated = evaluate(await expression('(await x) ? 2 : 3'), {scope});
expect(await evaluated.async)
.to.equal(true);
expect(await evaluated.value)
.to.equal(2);
scope.set('x', false);
evaluated = evaluate(await expression('(await x) ? 2 : 3'), {scope});
expect(await evaluated.async)
.to.equal(true);
expect(await evaluated.value)
.to.equal(3);
});