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

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