silphius/app/astride/evaluators/binary.test.js
2024-06-22 08:02:23 -05:00

178 lines
5.0 KiB
JavaScript

import {expect, test} from 'vitest';
import evaluate from '@/astride/evaluate.js';
import expression from '@/astride/test/expression.js';
test('evaluates +', async () => {
expect(evaluate(await expression('10 + 2')))
.to.deep.include({value: 12});
});
test('evaluates -', async () => {
expect(evaluate(await expression('10 - 2')))
.to.deep.include({value: 8});
});
test('evaluates /', async () => {
expect(evaluate(await expression('10 / 2')))
.to.deep.include({value: 5});
});
test('evaluates %', async () => {
expect(evaluate(await expression('10 % 3')))
.to.deep.include({value: 1});
});
test('evaluates *', async () => {
expect(evaluate(await expression('10 * 2')))
.to.deep.include({value: 20});
});
test('evaluates >', async () => {
expect(evaluate(await expression('10 > 2')))
.to.deep.include({value: true});
});
test('evaluates <', async () => {
expect(evaluate(await expression('10 < 2')))
.to.deep.include({value: false});
});
test('evaluates in', async () => {
expect(evaluate(await expression('"i" in {i: 69}')))
.to.deep.include({value: true});
expect(evaluate(await expression('"j" in {i: 69}')))
.to.deep.include({value: false});
});
test('evaluates >=', async () => {
expect(evaluate(await expression('10 >= 2')))
.to.deep.include({value: true});
expect(evaluate(await expression('2 >= 2')))
.to.deep.include({value: true});
expect(evaluate(await expression('1 >= 2')))
.to.deep.include({value: false});
});
test('evaluates <=', async () => {
expect(evaluate(await expression('10 <= 2')))
.to.deep.include({value: false});
expect(evaluate(await expression('2 <= 2')))
.to.deep.include({value: true});
expect(evaluate(await expression('1 <= 2')))
.to.deep.include({value: true});
});
test('evaluates **', async () => {
expect(evaluate(await expression('2 ** 16')))
.to.deep.include({value: 65536});
});
test('evaluates ===', async () => {
expect(evaluate(await expression('10 === "10"')))
.to.deep.include({value: false});
expect(evaluate(await expression('10 === 10')))
.to.deep.include({value: true});
});
test('evaluates !==', async () => {
expect(evaluate(await expression('10 !== "10"')))
.to.deep.include({value: true});
expect(evaluate(await expression('10 !== 10')))
.to.deep.include({value: false});
});
test('evaluates ^', async () => {
expect(evaluate(await expression('7 ^ 2')))
.to.deep.include({value: 5});
});
test('evaluates &', async () => {
expect(evaluate(await expression('5 & 3')))
.to.deep.include({value: 1});
});
test('evaluates |', async () => {
expect(evaluate(await expression('1 | 2')))
.to.deep.include({value: 3});
});
test('evaluates >>', async () => {
expect(evaluate(await expression('8 >> 1')))
.to.deep.include({value: 4});
});
test('evaluates <<', async () => {
expect(evaluate(await expression('2 << 1')))
.to.deep.include({value: 4});
});
test('evaluates >>>', async () => {
expect(evaluate(await expression('-1 >>> 1')))
.to.deep.include({value: 2147483647});
});
test('evaluates ==', async () => {
expect(evaluate(await expression('10 == "10"')))
.to.deep.include({value: true});
expect(evaluate(await expression('10 == 10')))
.to.deep.include({value: true});
expect(evaluate(await expression('10 == "ten"')))
.to.deep.include({value: false});
});
test('evaluates !=', async () => {
expect(evaluate(await expression('10 != "10"')))
.to.deep.include({value: false});
expect(evaluate(await expression('10 != 10')))
.to.deep.include({value: false});
expect(evaluate(await expression('10 != "ten"')))
.to.deep.include({value: true});
});
test('evaluates ||', async () => {
expect(evaluate(await expression('true || true')))
.to.deep.include({value: true});
expect(evaluate(await expression('true || false')))
.to.deep.include({value: true});
expect(evaluate(await expression('false || false')))
.to.deep.include({value: false});
});
test('evaluates &&', async () => {
expect(evaluate(await expression('true && true')))
.to.deep.include({value: true});
expect(evaluate(await expression('true && false')))
.to.deep.include({value: false});
expect(evaluate(await expression('false && false')))
.to.deep.include({value: false});
});
test('evaluates ??', async () => {
const scope = {
get() { return undefined; },
};
expect(evaluate(await expression('null ?? 1')))
.to.deep.include({value: 1});
expect(evaluate(await expression('undefined ?? 1'), {scope}))
.to.deep.include({value: 1});
expect(evaluate(await expression('2 ?? 1')))
.to.deep.include({value: 2});
});
test('evaluates instanceof', async () => {
const scope = {
get() { return Object; },
};
expect(evaluate(await expression('({}) instanceof Object'), {scope}))
.to.deep.include({value: true});
});
test('evaluates promised expressions', async () => {
const evaluated = evaluate(await expression('(await 1) + (await 2)'));
expect(evaluated.async)
.to.equal(true);
expect(await evaluated.value)
.to.equal(3);
});