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); });