chore: tidy
This commit is contained in:
parent
925256dcdc
commit
8ba0c0681f
|
@ -1,15 +1,15 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
test('evaluates array of literals', async () => {
|
||||
expect(evaluate(await first('[1.5, 2, "three"]')))
|
||||
expect(evaluate(await expression('[1.5, 2, "three"]')))
|
||||
.to.deep.include({value: [1.5, 2, 'three']});
|
||||
});
|
||||
|
||||
test('evaluates array containing promises', async () => {
|
||||
const evaluated = evaluate(await first('[1.5, 2, await "three"]'));
|
||||
const evaluated = evaluate(await expression('[1.5, 2, await "three"]'));
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
const scopeTest = test.extend({
|
||||
scope: async ({}, use) => {
|
||||
|
@ -14,11 +14,11 @@ const scopeTest = test.extend({
|
|||
});
|
||||
|
||||
scopeTest('evaluates =', async ({scope}) => {
|
||||
expect(evaluate(await first('x = 4'), {scope}))
|
||||
expect(evaluate(await expression('x = 4'), {scope}))
|
||||
.to.deep.include({value: 4});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(4);
|
||||
expect(evaluate(await first('O.x = 4'), {scope}))
|
||||
expect(evaluate(await expression('O.x = 4'), {scope}))
|
||||
.to.deep.include({value: 4});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(4);
|
||||
|
@ -26,12 +26,12 @@ scopeTest('evaluates =', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates +=', async ({scope}) => {
|
||||
scope.set('x', 1);
|
||||
expect(evaluate(await first('x += 4'), {scope}))
|
||||
expect(evaluate(await expression('x += 4'), {scope}))
|
||||
.to.deep.include({value: 5});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(5);
|
||||
scope.set('O', {x: 1});
|
||||
expect(evaluate(await first('O.x += 4'), {scope}))
|
||||
expect(evaluate(await expression('O.x += 4'), {scope}))
|
||||
.to.deep.include({value: 5});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(5);
|
||||
|
@ -39,12 +39,12 @@ scopeTest('evaluates +=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates -=', async ({scope}) => {
|
||||
scope.set('x', 5);
|
||||
expect(evaluate(await first('x -= 4'), {scope}))
|
||||
expect(evaluate(await expression('x -= 4'), {scope}))
|
||||
.to.deep.include({value: 1});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(1);
|
||||
scope.set('O', {x: 5});
|
||||
expect(evaluate(await first('O.x -= 4'), {scope}))
|
||||
expect(evaluate(await expression('O.x -= 4'), {scope}))
|
||||
.to.deep.include({value: 1});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(1);
|
||||
|
@ -52,12 +52,12 @@ scopeTest('evaluates -=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates *=', async ({scope}) => {
|
||||
scope.set('x', 5);
|
||||
expect(evaluate(await first('x *= 4'), {scope}))
|
||||
expect(evaluate(await expression('x *= 4'), {scope}))
|
||||
.to.deep.include({value: 20});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(20);
|
||||
scope.set('O', {x: 5});
|
||||
expect(evaluate(await first('O.x *= 4'), {scope}))
|
||||
expect(evaluate(await expression('O.x *= 4'), {scope}))
|
||||
.to.deep.include({value: 20});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(20);
|
||||
|
@ -65,12 +65,12 @@ scopeTest('evaluates *=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates /=', async ({scope}) => {
|
||||
scope.set('x', 25);
|
||||
expect(evaluate(await first('x /= 5'), {scope}))
|
||||
expect(evaluate(await expression('x /= 5'), {scope}))
|
||||
.to.deep.include({value: 5});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(5);
|
||||
scope.set('O', {x: 25});
|
||||
expect(evaluate(await first('O.x /= 5'), {scope}))
|
||||
expect(evaluate(await expression('O.x /= 5'), {scope}))
|
||||
.to.deep.include({value: 5});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(5);
|
||||
|
@ -78,12 +78,12 @@ scopeTest('evaluates /=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates %=', async ({scope}) => {
|
||||
scope.set('x', 5);
|
||||
expect(evaluate(await first('x %= 2'), {scope}))
|
||||
expect(evaluate(await expression('x %= 2'), {scope}))
|
||||
.to.deep.include({value: 1});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(1);
|
||||
scope.set('O', {x: 5});
|
||||
expect(evaluate(await first('O.x %= 2'), {scope}))
|
||||
expect(evaluate(await expression('O.x %= 2'), {scope}))
|
||||
.to.deep.include({value: 1});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(1);
|
||||
|
@ -91,12 +91,12 @@ scopeTest('evaluates %=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates **=', async ({scope}) => {
|
||||
scope.set('x', 5);
|
||||
expect(evaluate(await first('x **= 3'), {scope}))
|
||||
expect(evaluate(await expression('x **= 3'), {scope}))
|
||||
.to.deep.include({value: 125});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(125);
|
||||
scope.set('O', {x: 5});
|
||||
expect(evaluate(await first('O.x **= 3'), {scope}))
|
||||
expect(evaluate(await expression('O.x **= 3'), {scope}))
|
||||
.to.deep.include({value: 125});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(125);
|
||||
|
@ -104,12 +104,12 @@ scopeTest('evaluates **=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates <<=', async ({scope}) => {
|
||||
scope.set('x', 2);
|
||||
expect(evaluate(await first('x <<= 1'), {scope}))
|
||||
expect(evaluate(await expression('x <<= 1'), {scope}))
|
||||
.to.deep.include({value: 4});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(4);
|
||||
scope.set('O', {x: 2});
|
||||
expect(evaluate(await first('O.x <<= 1'), {scope}))
|
||||
expect(evaluate(await expression('O.x <<= 1'), {scope}))
|
||||
.to.deep.include({value: 4});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(4);
|
||||
|
@ -117,12 +117,12 @@ scopeTest('evaluates <<=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates >>=', async ({scope}) => {
|
||||
scope.set('x', 8);
|
||||
expect(evaluate(await first('x >>= 2'), {scope}))
|
||||
expect(evaluate(await expression('x >>= 2'), {scope}))
|
||||
.to.deep.include({value: 2});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(2);
|
||||
scope.set('O', {x: 8});
|
||||
expect(evaluate(await first('O.x >>= 2'), {scope}))
|
||||
expect(evaluate(await expression('O.x >>= 2'), {scope}))
|
||||
.to.deep.include({value: 2});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(2);
|
||||
|
@ -130,12 +130,12 @@ scopeTest('evaluates >>=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates >>>=', async ({scope}) => {
|
||||
scope.set('x', -1);
|
||||
expect(evaluate(await first('x >>>= 1'), {scope}))
|
||||
expect(evaluate(await expression('x >>>= 1'), {scope}))
|
||||
.to.deep.include({value: 2147483647});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(2147483647);
|
||||
scope.set('O', {x: -1});
|
||||
expect(evaluate(await first('O.x >>>= 1'), {scope}))
|
||||
expect(evaluate(await expression('O.x >>>= 1'), {scope}))
|
||||
.to.deep.include({value: 2147483647});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(2147483647);
|
||||
|
@ -143,12 +143,12 @@ scopeTest('evaluates >>>=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates |=', async ({scope}) => {
|
||||
scope.set('x', 3);
|
||||
expect(evaluate(await first('x |= 5'), {scope}))
|
||||
expect(evaluate(await expression('x |= 5'), {scope}))
|
||||
.to.deep.include({value: 7});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(7);
|
||||
scope.set('O', {x: 3});
|
||||
expect(evaluate(await first('O.x |= 5'), {scope}))
|
||||
expect(evaluate(await expression('O.x |= 5'), {scope}))
|
||||
.to.deep.include({value: 7});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(7);
|
||||
|
@ -156,12 +156,12 @@ scopeTest('evaluates |=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates ^=', async ({scope}) => {
|
||||
scope.set('x', 7);
|
||||
expect(evaluate(await first('x ^= 2'), {scope}))
|
||||
expect(evaluate(await expression('x ^= 2'), {scope}))
|
||||
.to.deep.include({value: 5});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(5);
|
||||
scope.set('O', {x: 7});
|
||||
expect(evaluate(await first('O.x ^= 2'), {scope}))
|
||||
expect(evaluate(await expression('O.x ^= 2'), {scope}))
|
||||
.to.deep.include({value: 5});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(5);
|
||||
|
@ -169,12 +169,12 @@ scopeTest('evaluates ^=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates &=', async ({scope}) => {
|
||||
scope.set('x', 5);
|
||||
expect(evaluate(await first('x &= 3'), {scope}))
|
||||
expect(evaluate(await expression('x &= 3'), {scope}))
|
||||
.to.deep.include({value: 1});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(1);
|
||||
scope.set('O', {x: 5});
|
||||
expect(evaluate(await first('O.x &= 3'), {scope}))
|
||||
expect(evaluate(await expression('O.x &= 3'), {scope}))
|
||||
.to.deep.include({value: 1});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(1);
|
||||
|
@ -182,12 +182,12 @@ scopeTest('evaluates &=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates ||=', async ({scope}) => {
|
||||
scope.set('x', false);
|
||||
expect(evaluate(await first('x ||= true'), {scope}))
|
||||
expect(evaluate(await expression('x ||= true'), {scope}))
|
||||
.to.deep.include({value: true});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(true);
|
||||
scope.set('O', {x: false});
|
||||
expect(evaluate(await first('O.x ||= true'), {scope}))
|
||||
expect(evaluate(await expression('O.x ||= true'), {scope}))
|
||||
.to.deep.include({value: true});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(true);
|
||||
|
@ -195,20 +195,20 @@ scopeTest('evaluates ||=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates &&=', async ({scope}) => {
|
||||
scope.set('x', true);
|
||||
expect(evaluate(await first('x &&= true'), {scope}))
|
||||
expect(evaluate(await expression('x &&= true'), {scope}))
|
||||
.to.deep.include({value: true});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(true);
|
||||
expect(evaluate(await first('x &&= false'), {scope}))
|
||||
expect(evaluate(await expression('x &&= false'), {scope}))
|
||||
.to.deep.include({value: false});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(false);
|
||||
scope.set('O', {x: true});
|
||||
expect(evaluate(await first('O.x &&= true'), {scope}))
|
||||
expect(evaluate(await expression('O.x &&= true'), {scope}))
|
||||
.to.deep.include({value: true});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(true);
|
||||
expect(evaluate(await first('O.x &&= false'), {scope}))
|
||||
expect(evaluate(await expression('O.x &&= false'), {scope}))
|
||||
.to.deep.include({value: false});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(false);
|
||||
|
@ -216,48 +216,48 @@ scopeTest('evaluates &&=', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates ??=', async ({scope}) => {
|
||||
scope.set('x', null);
|
||||
expect(evaluate(await first('x ??= 2'), {scope}))
|
||||
expect(evaluate(await expression('x ??= 2'), {scope}))
|
||||
.to.deep.include({value: 2});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(2);
|
||||
expect(evaluate(await first('x ??= 4'), {scope}))
|
||||
expect(evaluate(await expression('x ??= 4'), {scope}))
|
||||
.to.deep.include({value: 2});
|
||||
expect(scope.get('x'))
|
||||
.to.equal(2);
|
||||
scope.set('O', {x: null});
|
||||
expect(evaluate(await first('O.x ??= 2'), {scope}))
|
||||
expect(evaluate(await expression('O.x ??= 2'), {scope}))
|
||||
.to.deep.include({value: 2});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(2);
|
||||
expect(evaluate(await first('O.x ??= 4'), {scope}))
|
||||
expect(evaluate(await expression('O.x ??= 4'), {scope}))
|
||||
.to.deep.include({value: 2});
|
||||
expect(scope.get('O').x)
|
||||
.to.equal(2);
|
||||
});
|
||||
|
||||
scopeTest('evaluates promised assignment', async ({scope}) => {
|
||||
const evaluated = evaluate(await first('x = await 4'), {scope});
|
||||
const evaluated = evaluate(await expression('x = await 4'), {scope});
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
.to.equal(4);
|
||||
expect(await scope.get('x'))
|
||||
.to.equal(4);
|
||||
const evaluatedComputedObject = evaluate(await first('O["x"] = await 4'), {scope});
|
||||
const evaluatedComputedObject = evaluate(await expression('O["x"] = await 4'), {scope});
|
||||
expect(evaluatedComputedObject.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluatedComputedObject.value)
|
||||
.to.equal(4);
|
||||
expect(await scope.get('O').x)
|
||||
.to.equal(4);
|
||||
const evaluatedObject = evaluate(await first('O.x = await 4'), {scope});
|
||||
const evaluatedObject = evaluate(await expression('O.x = await 4'), {scope});
|
||||
expect(evaluatedObject.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluatedObject.value)
|
||||
.to.equal(4);
|
||||
expect(await scope.get('O').x)
|
||||
.to.equal(4);
|
||||
const evaluatedPromisedObject = evaluate(await first('(await O).x = await 4'), {scope});
|
||||
const evaluatedPromisedObject = evaluate(await expression('(await O).x = await 4'), {scope});
|
||||
expect(evaluatedPromisedObject.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluatedPromisedObject.value)
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
test('evaluates await expressions', async () => {
|
||||
const evaluated = evaluate(await first('await 1'));
|
||||
const evaluated = evaluate(await expression('await 1'));
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
@ -12,7 +12,7 @@ test('evaluates await expressions', async () => {
|
|||
});
|
||||
|
||||
test('coalesces promises', async () => {
|
||||
const evaluated = evaluate(await first('await await await 1'));
|
||||
const evaluated = evaluate(await expression('await await await 1'));
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
|
|
@ -1,150 +1,150 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
test('evaluates +', async () => {
|
||||
expect(evaluate(await first('10 + 2')))
|
||||
expect(evaluate(await expression('10 + 2')))
|
||||
.to.deep.include({value: 12});
|
||||
});
|
||||
|
||||
test('evaluates -', async () => {
|
||||
expect(evaluate(await first('10 - 2')))
|
||||
expect(evaluate(await expression('10 - 2')))
|
||||
.to.deep.include({value: 8});
|
||||
});
|
||||
|
||||
test('evaluates /', async () => {
|
||||
expect(evaluate(await first('10 / 2')))
|
||||
expect(evaluate(await expression('10 / 2')))
|
||||
.to.deep.include({value: 5});
|
||||
});
|
||||
|
||||
test('evaluates %', async () => {
|
||||
expect(evaluate(await first('10 % 3')))
|
||||
expect(evaluate(await expression('10 % 3')))
|
||||
.to.deep.include({value: 1});
|
||||
});
|
||||
|
||||
test('evaluates *', async () => {
|
||||
expect(evaluate(await first('10 * 2')))
|
||||
expect(evaluate(await expression('10 * 2')))
|
||||
.to.deep.include({value: 20});
|
||||
});
|
||||
|
||||
test('evaluates >', async () => {
|
||||
expect(evaluate(await first('10 > 2')))
|
||||
expect(evaluate(await expression('10 > 2')))
|
||||
.to.deep.include({value: true});
|
||||
});
|
||||
|
||||
test('evaluates <', async () => {
|
||||
expect(evaluate(await first('10 < 2')))
|
||||
expect(evaluate(await expression('10 < 2')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
test('evaluates in', async () => {
|
||||
expect(evaluate(await first('"i" in {i: 69}')))
|
||||
expect(evaluate(await expression('"i" in {i: 69}')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('"j" in {i: 69}')))
|
||||
expect(evaluate(await expression('"j" in {i: 69}')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
test('evaluates >=', async () => {
|
||||
expect(evaluate(await first('10 >= 2')))
|
||||
expect(evaluate(await expression('10 >= 2')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('2 >= 2')))
|
||||
expect(evaluate(await expression('2 >= 2')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('1 >= 2')))
|
||||
expect(evaluate(await expression('1 >= 2')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
test('evaluates <=', async () => {
|
||||
expect(evaluate(await first('10 <= 2')))
|
||||
expect(evaluate(await expression('10 <= 2')))
|
||||
.to.deep.include({value: false});
|
||||
expect(evaluate(await first('2 <= 2')))
|
||||
expect(evaluate(await expression('2 <= 2')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('1 <= 2')))
|
||||
expect(evaluate(await expression('1 <= 2')))
|
||||
.to.deep.include({value: true});
|
||||
});
|
||||
|
||||
test('evaluates **', async () => {
|
||||
expect(evaluate(await first('2 ** 16')))
|
||||
expect(evaluate(await expression('2 ** 16')))
|
||||
.to.deep.include({value: 65536});
|
||||
});
|
||||
|
||||
test('evaluates ===', async () => {
|
||||
expect(evaluate(await first('10 === "10"')))
|
||||
expect(evaluate(await expression('10 === "10"')))
|
||||
.to.deep.include({value: false});
|
||||
expect(evaluate(await first('10 === 10')))
|
||||
expect(evaluate(await expression('10 === 10')))
|
||||
.to.deep.include({value: true});
|
||||
});
|
||||
|
||||
test('evaluates !==', async () => {
|
||||
expect(evaluate(await first('10 !== "10"')))
|
||||
expect(evaluate(await expression('10 !== "10"')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('10 !== 10')))
|
||||
expect(evaluate(await expression('10 !== 10')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
test('evaluates ^', async () => {
|
||||
expect(evaluate(await first('7 ^ 2')))
|
||||
expect(evaluate(await expression('7 ^ 2')))
|
||||
.to.deep.include({value: 5});
|
||||
});
|
||||
|
||||
test('evaluates &', async () => {
|
||||
expect(evaluate(await first('5 & 3')))
|
||||
expect(evaluate(await expression('5 & 3')))
|
||||
.to.deep.include({value: 1});
|
||||
});
|
||||
|
||||
test('evaluates |', async () => {
|
||||
expect(evaluate(await first('1 | 2')))
|
||||
expect(evaluate(await expression('1 | 2')))
|
||||
.to.deep.include({value: 3});
|
||||
});
|
||||
|
||||
test('evaluates >>', async () => {
|
||||
expect(evaluate(await first('8 >> 1')))
|
||||
expect(evaluate(await expression('8 >> 1')))
|
||||
.to.deep.include({value: 4});
|
||||
});
|
||||
|
||||
test('evaluates <<', async () => {
|
||||
expect(evaluate(await first('2 << 1')))
|
||||
expect(evaluate(await expression('2 << 1')))
|
||||
.to.deep.include({value: 4});
|
||||
});
|
||||
|
||||
test('evaluates >>>', async () => {
|
||||
expect(evaluate(await first('-1 >>> 1')))
|
||||
expect(evaluate(await expression('-1 >>> 1')))
|
||||
.to.deep.include({value: 2147483647});
|
||||
});
|
||||
|
||||
test('evaluates ==', async () => {
|
||||
expect(evaluate(await first('10 == "10"')))
|
||||
expect(evaluate(await expression('10 == "10"')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('10 == 10')))
|
||||
expect(evaluate(await expression('10 == 10')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('10 == "ten"')))
|
||||
expect(evaluate(await expression('10 == "ten"')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
test('evaluates !=', async () => {
|
||||
expect(evaluate(await first('10 != "10"')))
|
||||
expect(evaluate(await expression('10 != "10"')))
|
||||
.to.deep.include({value: false});
|
||||
expect(evaluate(await first('10 != 10')))
|
||||
expect(evaluate(await expression('10 != 10')))
|
||||
.to.deep.include({value: false});
|
||||
expect(evaluate(await first('10 != "ten"')))
|
||||
expect(evaluate(await expression('10 != "ten"')))
|
||||
.to.deep.include({value: true});
|
||||
});
|
||||
|
||||
test('evaluates ||', async () => {
|
||||
expect(evaluate(await first('true || true')))
|
||||
expect(evaluate(await expression('true || true')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('true || false')))
|
||||
expect(evaluate(await expression('true || false')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('false || false')))
|
||||
expect(evaluate(await expression('false || false')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
test('evaluates &&', async () => {
|
||||
expect(evaluate(await first('true && true')))
|
||||
expect(evaluate(await expression('true && true')))
|
||||
.to.deep.include({value: true});
|
||||
expect(evaluate(await first('true && false')))
|
||||
expect(evaluate(await expression('true && false')))
|
||||
.to.deep.include({value: false});
|
||||
expect(evaluate(await first('false && false')))
|
||||
expect(evaluate(await expression('false && false')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
|
@ -152,11 +152,11 @@ test('evaluates ??', async () => {
|
|||
const scope = {
|
||||
get() { return undefined; },
|
||||
};
|
||||
expect(evaluate(await first('null ?? 1')))
|
||||
expect(evaluate(await expression('null ?? 1')))
|
||||
.to.deep.include({value: 1});
|
||||
expect(evaluate(await first('undefined ?? 1'), {scope}))
|
||||
expect(evaluate(await expression('undefined ?? 1'), {scope}))
|
||||
.to.deep.include({value: 1});
|
||||
expect(evaluate(await first('2 ?? 1')))
|
||||
expect(evaluate(await expression('2 ?? 1')))
|
||||
.to.deep.include({value: 2});
|
||||
});
|
||||
|
||||
|
@ -164,12 +164,12 @@ test('evaluates instanceof', async () => {
|
|||
const scope = {
|
||||
get() { return Object; },
|
||||
};
|
||||
expect(evaluate(await first('({}) instanceof Object'), {scope}))
|
||||
expect(evaluate(await expression('({}) instanceof Object'), {scope}))
|
||||
.to.deep.include({value: true});
|
||||
});
|
||||
|
||||
test('evaluates promised expressions', async () => {
|
||||
const evaluated = evaluate(await first('(await 1) + (await 2)'));
|
||||
const evaluated = evaluate(await expression('(await 1) + (await 2)'));
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
const scopeTest = test.extend({
|
||||
scope: async ({}, use) => {
|
||||
|
@ -15,7 +15,7 @@ const scopeTest = test.extend({
|
|||
|
||||
scopeTest('evaluates calls', async ({scope}) => {
|
||||
scope.set('f', (...args) => args.reduce((l, r) => l + r, 0));
|
||||
const evaluated = evaluate(await first('f(1, 2, 3)'), {scope});
|
||||
const evaluated = evaluate(await expression('f(1, 2, 3)'), {scope});
|
||||
expect(evaluated.value)
|
||||
.to.equal(6);
|
||||
});
|
||||
|
@ -24,12 +24,12 @@ scopeTest('evaluates async calls', async ({scope}) => {
|
|||
const f = (...args) => args.reduce((l, r) => l + r, 0);
|
||||
scope.set('f', f);
|
||||
scope.set('O', {f});
|
||||
const evaluated = evaluate(await first('f(await 1, 2, 3)'), {scope});
|
||||
const evaluated = evaluate(await expression('f(await 1, 2, 3)'), {scope});
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
.to.equal(6);
|
||||
const evaluatedOptional = evaluate(await first('O?.f(await 1, 2, 3)'), {scope});
|
||||
const evaluatedOptional = evaluate(await expression('O?.f(await 1, 2, 3)'), {scope});
|
||||
expect(evaluatedOptional.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluatedOptional.value)
|
||||
|
@ -38,25 +38,25 @@ scopeTest('evaluates async calls', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates member calls', async ({scope}) => {
|
||||
scope.set('O', {f: (...args) => args.reduce((l, r) => l + r, 0)});
|
||||
expect(evaluate(await first('O.f(1, 2, 3)'), {scope}).value)
|
||||
expect(evaluate(await expression('O.f(1, 2, 3)'), {scope}).value)
|
||||
.to.equal(6);
|
||||
expect(evaluate(await first('O["f"](1, 2, 3)'), {scope}).value)
|
||||
expect(evaluate(await expression('O["f"](1, 2, 3)'), {scope}).value)
|
||||
.to.equal(6);
|
||||
});
|
||||
|
||||
scopeTest('evaluates optional calls', async ({scope}) => {
|
||||
scope.set('O', {});
|
||||
expect(evaluate(await first('g?.(1, 2, 3)'), {scope}).value)
|
||||
expect(evaluate(await expression('g?.(1, 2, 3)'), {scope}).value)
|
||||
.to.equal(undefined);
|
||||
expect(evaluate(await first('O?.g(1, 2, 3)'), {scope}).value)
|
||||
expect(evaluate(await expression('O?.g(1, 2, 3)'), {scope}).value)
|
||||
.to.equal(undefined);
|
||||
expect(evaluate(await first('O?.g?.(1, 2, 3)'), {scope}).value)
|
||||
expect(evaluate(await expression('O?.g?.(1, 2, 3)'), {scope}).value)
|
||||
.to.equal(undefined);
|
||||
});
|
||||
|
||||
scopeTest('evaluates async calls', async ({scope}) => {
|
||||
scope.set('O', {f: (...args) => args.reduce((l, r) => l + r, 0)});
|
||||
const evaluated = evaluate(await first('O.f(await 1, 2, 3)'), {scope});
|
||||
const evaluated = evaluate(await expression('O.f(await 1, 2, 3)'), {scope});
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
const scopeTest = test.extend({
|
||||
scope: async ({}, use) => {
|
||||
|
@ -16,11 +16,11 @@ const scopeTest = test.extend({
|
|||
scopeTest('evaluates conditional expression', async ({scope}) => {
|
||||
scope.set('x', true);
|
||||
let evaluated;
|
||||
evaluated = evaluate(await first('x ? 2 : 3'), {scope});
|
||||
evaluated = evaluate(await expression('x ? 2 : 3'), {scope});
|
||||
expect(evaluated.value)
|
||||
.to.equal(2);
|
||||
scope.set('x', false);
|
||||
evaluated = evaluate(await first('x ? 2 : 3'), {scope});
|
||||
evaluated = evaluate(await expression('x ? 2 : 3'), {scope});
|
||||
expect(evaluated.value)
|
||||
.to.equal(3);
|
||||
});
|
||||
|
@ -28,13 +28,13 @@ scopeTest('evaluates conditional expression', async ({scope}) => {
|
|||
scopeTest('evaluates async conditional expression', async ({scope}) => {
|
||||
scope.set('x', true);
|
||||
let evaluated;
|
||||
evaluated = evaluate(await first('(await x) ? 2 : 3'), {scope});
|
||||
evaluated = evaluate(await expression('(await x) ? 2 : 3'), {scope});
|
||||
expect(await evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
.to.equal(2);
|
||||
scope.set('x', false);
|
||||
evaluated = evaluate(await first('(await x) ? 2 : 3'), {scope});
|
||||
evaluated = evaluate(await expression('(await x) ? 2 : 3'), {scope});
|
||||
expect(await evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
test('evaluates numeric literals', async () => {
|
||||
expect(evaluate(await first('1')))
|
||||
expect(evaluate(await expression('1')))
|
||||
.to.deep.include({value: 1});
|
||||
});
|
||||
|
||||
test('evaluates string literals', async () => {
|
||||
expect(evaluate(await first('"1"')))
|
||||
expect(evaluate(await expression('"1"')))
|
||||
.to.deep.include({value: '1'});
|
||||
});
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
const scopeTest = test.extend({
|
||||
scope: async ({}, use) => {
|
||||
|
@ -15,28 +15,28 @@ const scopeTest = test.extend({
|
|||
|
||||
scopeTest('evaluates member expression', async ({scope}) => {
|
||||
let evaluated;
|
||||
evaluated = evaluate(await first('O.x'), {scope});
|
||||
evaluated = evaluate(await expression('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});
|
||||
evaluated = evaluate(await expression('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});
|
||||
evaluated = evaluate(await expression('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});
|
||||
evaluated = evaluate(await expression('O[await "x"]'), {scope});
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
test('evaluates object expression', async () => {
|
||||
let evaluated;
|
||||
evaluated = evaluate(await first(`({
|
||||
evaluated = evaluate(await expression(`({
|
||||
["foo"]: 16,
|
||||
bar: 32,
|
||||
'baz': 64,
|
||||
|
@ -20,7 +20,7 @@ test('evaluates object expression', async () => {
|
|||
|
||||
test('evaluates async object expression', async () => {
|
||||
let evaluated;
|
||||
evaluated = evaluate(await first(`({
|
||||
evaluated = evaluate(await expression(`({
|
||||
foo: await 32,
|
||||
})`));
|
||||
expect(evaluated.async)
|
||||
|
@ -33,7 +33,7 @@ test('evaluates async object expression', async () => {
|
|||
|
||||
test('evaluates object spread expression', async () => {
|
||||
let evaluated;
|
||||
evaluated = evaluate(await first(`({
|
||||
evaluated = evaluate(await expression(`({
|
||||
foo: 16,
|
||||
...({bar: 32}),
|
||||
})`));
|
||||
|
@ -46,7 +46,7 @@ test('evaluates object spread expression', async () => {
|
|||
|
||||
test('evaluates async spread expression', async () => {
|
||||
let evaluated;
|
||||
evaluated = evaluate(await first(`({
|
||||
evaluated = evaluate(await expression(`({
|
||||
foo: 16,
|
||||
...(await {bar: 32}),
|
||||
})`));
|
||||
|
|
|
@ -1,40 +1,40 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
test('evaluates +', async () => {
|
||||
expect(evaluate(await first('+1')))
|
||||
expect(evaluate(await expression('+1')))
|
||||
.to.deep.include({value: 1});
|
||||
});
|
||||
|
||||
test('evaluates -', async () => {
|
||||
expect(evaluate(await first('-1')))
|
||||
expect(evaluate(await expression('-1')))
|
||||
.to.deep.include({value: -1});
|
||||
});
|
||||
|
||||
test('evaluates !', async () => {
|
||||
expect(evaluate(await first('!true')))
|
||||
expect(evaluate(await expression('!true')))
|
||||
.to.deep.include({value: false});
|
||||
});
|
||||
|
||||
test('evaluates ~', async () => {
|
||||
expect(evaluate(await first('~1')))
|
||||
expect(evaluate(await expression('~1')))
|
||||
.to.deep.include({value: -2});
|
||||
});
|
||||
|
||||
test('evaluates typeof', async () => {
|
||||
expect(evaluate(await first('typeof "a"')))
|
||||
expect(evaluate(await expression('typeof "a"')))
|
||||
.to.deep.include({value: 'string'});
|
||||
});
|
||||
|
||||
test('evaluates void', async () => {
|
||||
expect(evaluate(await first('void 0')))
|
||||
expect(evaluate(await expression('void 0')))
|
||||
.to.deep.include({value: undefined});
|
||||
});
|
||||
|
||||
test('evaluates promised unary expression', async () => {
|
||||
const evaluated = evaluate(await first('-(await 4)'));
|
||||
const evaluated = evaluate(await expression('-(await 4)'));
|
||||
expect(evaluated.async)
|
||||
.to.equal(true);
|
||||
expect(await evaluated.value)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {expect, test} from 'vitest';
|
||||
|
||||
import {first} from '@/swcx/builders.js';
|
||||
import evaluate from '@/swcx/evaluate.js';
|
||||
import expression from '@/swcx/test/expression.js';
|
||||
|
||||
const scopeTest = test.extend({
|
||||
scope: async ({}, use) => {
|
||||
|
@ -15,14 +15,14 @@ const scopeTest = test.extend({
|
|||
|
||||
scopeTest('evaluates postfix updates', async ({scope}) => {
|
||||
scope.set('x', 4);
|
||||
let evaluated = evaluate(await first('y = x++'), {scope});
|
||||
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 first('y = x--'), {scope});
|
||||
evaluated = evaluate(await expression('y = x--'), {scope});
|
||||
expect(evaluated.value)
|
||||
.to.equal(5);
|
||||
expect(scope.get('x'))
|
||||
|
@ -33,14 +33,14 @@ scopeTest('evaluates postfix updates', async ({scope}) => {
|
|||
|
||||
scopeTest('evaluates prefix updates', async ({scope}) => {
|
||||
scope.set('x', 4);
|
||||
let evaluated = evaluate(await first('y = ++x'), {scope});
|
||||
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 first('y = --x'), {scope});
|
||||
evaluated = evaluate(await expression('y = --x'), {scope});
|
||||
expect(evaluated.value)
|
||||
.to.equal(4);
|
||||
expect(scope.get('x'))
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
export async function first(code) {
|
||||
export default async function(code) {
|
||||
const {parse} = await import('@swc/core');
|
||||
const ast = await parse(code);
|
||||
return ast.body[0].expression;
|
Loading…
Reference in New Issue
Block a user