silphius/app/astride/evaluators/member.test.js

45 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-06-16 08:01:01 -05:00
import {expect, test} from 'vitest';
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: {x: 32}},
get(k) { return this.S[k]; },
set(k, v) { return this.S[k] = v; }
});
},
});
scopeTest('evaluates member expression', async ({scope}) => {
let evaluated;
2024-06-18 21:46:51 -05:00
evaluated = evaluate(await expression('O.x'), {scope});
2024-06-16 08:01:01 -05:00
expect(evaluated.value)
.to.equal(32);
});
scopeTest('evaluates optional member expression', async ({scope}) => {
let evaluated;
2024-06-18 21:46:51 -05:00
evaluated = evaluate(await expression('O?.y'), {scope});
2024-06-16 08:01:01 -05:00
expect(evaluated.value)
.to.equal(undefined);
});
scopeTest('evaluates computed member expression', async ({scope}) => {
let evaluated;
2024-06-18 21:46:51 -05:00
evaluated = evaluate(await expression('O["x"]'), {scope});
2024-06-16 08:01:01 -05:00
expect(evaluated.value)
.to.equal(32);
});
scopeTest('evaluates async member expression', async ({scope}) => {
let evaluated;
2024-06-18 21:46:51 -05:00
evaluated = evaluate(await expression('O[await "x"]'), {scope});
2024-06-16 08:01:01 -05:00
expect(evaluated.async)
.to.equal(true);
expect(await evaluated.value)
.to.equal(32);
});