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

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-06-16 08:01:01 -05:00
import {expect, test} from 'vitest';
import evaluate from '@/swcx/evaluate.js';
2024-06-18 21:46:51 -05:00
import expression from '@/swcx/test/expression.js';
2024-06-16 08:01:01 -05:00
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);
2024-06-18 21:46:51 -05:00
let evaluated = evaluate(await expression('y = x++'), {scope});
2024-06-16 08:01:01 -05:00
expect(evaluated.value)
.to.equal(4);
expect(scope.get('x'))
.to.equal(5);
expect(scope.get('y'))
.to.equal(4);
2024-06-18 21:46:51 -05:00
evaluated = evaluate(await expression('y = x--'), {scope});
2024-06-16 08:01:01 -05:00
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);
2024-06-18 21:46:51 -05:00
let evaluated = evaluate(await expression('y = ++x'), {scope});
2024-06-16 08:01:01 -05:00
expect(evaluated.value)
.to.equal(5);
expect(scope.get('x'))
.to.equal(5);
expect(scope.get('y'))
.to.equal(5);
2024-06-18 21:46:51 -05:00
evaluated = evaluate(await expression('y = --x'), {scope});
2024-06-16 08:01:01 -05:00
expect(evaluated.value)
.to.equal(4);
expect(scope.get('x'))
.to.equal(4);
expect(scope.get('y'))
.to.equal(4);
});