silphius/app/swcx/evaluators/conditional.test.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-06-16 08:01:01 -05:00
import {expect, test} from 'vitest';
import {first} from '@/swcx/builders.js';
import evaluate from '@/swcx/evaluate.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 first('x ? 2 : 3'), {scope});
expect(evaluated.value)
.to.equal(2);
scope.set('x', false);
evaluated = evaluate(await first('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 first('(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 first('(await x) ? 2 : 3'), {scope});
expect(await evaluated.async)
.to.equal(true);
expect(await evaluated.value)
.to.equal(3);
});