2024-06-16 08:01:01 -05:00
|
|
|
import {expect, test} from 'vitest';
|
|
|
|
|
2024-06-22 08:02:23 -05:00
|
|
|
import evaluate from '@/astride/evaluate.js';
|
|
|
|
import expression from '@/astride/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 calls', async ({scope}) => {
|
|
|
|
scope.set('f', (...args) => args.reduce((l, r) => l + r, 0));
|
2024-06-18 21:46:51 -05:00
|
|
|
const evaluated = evaluate(await expression('f(1, 2, 3)'), {scope});
|
2024-06-16 08:01:01 -05:00
|
|
|
expect(evaluated.value)
|
|
|
|
.to.equal(6);
|
|
|
|
});
|
|
|
|
|
|
|
|
scopeTest('evaluates async calls', async ({scope}) => {
|
|
|
|
const f = (...args) => args.reduce((l, r) => l + r, 0);
|
|
|
|
scope.set('f', f);
|
|
|
|
scope.set('O', {f});
|
2024-06-18 21:46:51 -05:00
|
|
|
const evaluated = evaluate(await expression('f(await 1, 2, 3)'), {scope});
|
2024-06-16 08:01:01 -05:00
|
|
|
expect(evaluated.async)
|
|
|
|
.to.equal(true);
|
|
|
|
expect(await evaluated.value)
|
|
|
|
.to.equal(6);
|
2024-06-18 21:46:51 -05:00
|
|
|
const evaluatedOptional = evaluate(await expression('O?.f(await 1, 2, 3)'), {scope});
|
2024-06-16 08:01:01 -05:00
|
|
|
expect(evaluatedOptional.async)
|
|
|
|
.to.equal(true);
|
|
|
|
expect(await evaluatedOptional.value)
|
|
|
|
.to.equal(6);
|
|
|
|
});
|
|
|
|
|
|
|
|
scopeTest('evaluates member calls', async ({scope}) => {
|
|
|
|
scope.set('O', {f: (...args) => args.reduce((l, r) => l + r, 0)});
|
2024-06-18 21:46:51 -05:00
|
|
|
expect(evaluate(await expression('O.f(1, 2, 3)'), {scope}).value)
|
2024-06-16 08:01:01 -05:00
|
|
|
.to.equal(6);
|
2024-06-18 21:46:51 -05:00
|
|
|
expect(evaluate(await expression('O["f"](1, 2, 3)'), {scope}).value)
|
2024-06-16 08:01:01 -05:00
|
|
|
.to.equal(6);
|
|
|
|
});
|
|
|
|
|
|
|
|
scopeTest('evaluates optional calls', async ({scope}) => {
|
|
|
|
scope.set('O', {});
|
2024-06-18 21:46:51 -05:00
|
|
|
expect(evaluate(await expression('g?.(1, 2, 3)'), {scope}).value)
|
2024-06-16 08:01:01 -05:00
|
|
|
.to.equal(undefined);
|
2024-06-30 14:20:37 -05:00
|
|
|
expect(evaluate(await expression('O?.g?.(1, 2, 3)'), {scope}).value)
|
|
|
|
.to.equal(undefined);
|
2024-06-16 08:01:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
scopeTest('evaluates async calls', async ({scope}) => {
|
|
|
|
scope.set('O', {f: (...args) => args.reduce((l, r) => l + r, 0)});
|
2024-06-18 21:46:51 -05:00
|
|
|
const evaluated = evaluate(await expression('O.f(await 1, 2, 3)'), {scope});
|
2024-06-16 08:01:01 -05:00
|
|
|
expect(evaluated.async)
|
|
|
|
.to.equal(true);
|
|
|
|
expect(await evaluated.value)
|
|
|
|
.to.equal(6);
|
|
|
|
});
|