silphius/app/swcx/evaluators/member.test.js
2024-06-17 05:20:01 -05:00

45 lines
1.1 KiB
JavaScript

import {expect, test} from 'vitest';
import {first} from '@/swcx/builders.js';
import evaluate from '@/swcx/evaluate.js';
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;
evaluated = evaluate(await first('O.x'), {scope});
expect(evaluated.value)
.to.equal(32);
});
scopeTest('evaluates optional member expression', async ({scope}) => {
let evaluated;
evaluated = evaluate(await first('O?.y'), {scope});
expect(evaluated.value)
.to.equal(undefined);
});
scopeTest('evaluates computed member expression', async ({scope}) => {
let evaluated;
evaluated = evaluate(await first('O["x"]'), {scope});
expect(evaluated.value)
.to.equal(32);
});
scopeTest('evaluates async member expression', async ({scope}) => {
let evaluated;
evaluated = evaluate(await first('O[await "x"]'), {scope});
expect(evaluated.async)
.to.equal(true);
expect(await evaluated.value)
.to.equal(32);
});