import {expect, test} from 'vitest'; import evaluate from '@/swcx/evaluate.js'; import expression from '@/swcx/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 postfix updates', async ({scope}) => { scope.set('x', 4); let evaluated = evaluate(await expression('y = x++'), {scope}); expect(evaluated.value) .to.equal(4); expect(scope.get('x')) .to.equal(5); expect(scope.get('y')) .to.equal(4); evaluated = evaluate(await expression('y = x--'), {scope}); expect(evaluated.value) .to.equal(5); expect(scope.get('x')) .to.equal(4); expect(scope.get('y')) .to.equal(5); }); scopeTest('evaluates prefix updates', async ({scope}) => { scope.set('x', 4); let evaluated = evaluate(await expression('y = ++x'), {scope}); expect(evaluated.value) .to.equal(5); expect(scope.get('x')) .to.equal(5); expect(scope.get('y')) .to.equal(5); evaluated = evaluate(await expression('y = --x'), {scope}); expect(evaluated.value) .to.equal(4); expect(scope.get('x')) .to.equal(4); expect(scope.get('y')) .to.equal(4); });