babeler/test/binary-expression.js
2022-02-27 07:32:34 -06:00

52 lines
2.1 KiB
JavaScript

import {parse} from '@babel/parser';
import {expect} from 'chai';
import Sandbox from '../src/sandbox';
it('evaluates BinaryExpression', () => {
expect(new Sandbox(parse('1 + 2')).next().value)
.to.deep.include({value: 3});
expect(new Sandbox(parse('1 - 2')).next().value)
.to.deep.include({value: -1});
expect(new Sandbox(parse('4 / 2')).next().value)
.to.deep.include({value: 2});
expect(new Sandbox(parse('10 % 3')).next().value)
.to.deep.include({value: 1});
expect(new Sandbox(parse('1 * 2')).next().value)
.to.deep.include({value: 2});
expect(new Sandbox(parse('1 > 2')).next().value)
.to.deep.include({value: false});
expect(new Sandbox(parse('1 < 2')).next().value)
.to.deep.include({value: true});
expect(new Sandbox(parse('const foo = {a: 69}; "a" in foo')).next().value)
.to.deep.include({value: true});
expect(new Sandbox(parse('const foo = {a: 69}; "b" in foo')).next().value)
.to.deep.include({value: false});
expect(new Sandbox(parse('1 >= 2')).next().value)
.to.deep.include({value: false});
expect(new Sandbox(parse('1 <= 2')).next().value)
.to.deep.include({value: true});
expect(new Sandbox(parse('2 ** 3')).next().value)
.to.deep.include({value: 8});
expect(new Sandbox(parse('1 === 2')).next().value)
.to.deep.include({value: false});
expect(new Sandbox(parse('1 !== 2')).next().value)
.to.deep.include({value: true});
expect(new Sandbox(parse('7 & 3')).next().value)
.to.deep.include({value: 3});
expect(new Sandbox(parse('1 | 2')).next().value)
.to.deep.include({value: 3});
expect(new Sandbox(parse('16 >> 2')).next().value)
.to.deep.include({value: 4});
expect(new Sandbox(parse('16 >>> 5')).next().value)
.to.deep.include({value: 0});
expect(new Sandbox(parse('1 << 2')).next().value)
.to.deep.include({value: 4});
expect(new Sandbox(parse('1 ^ 2')).next().value)
.to.deep.include({value: 3});
expect(new Sandbox(parse('1 == 2')).next().value)
.to.deep.include({value: false});
expect(new Sandbox(parse('1 != 2')).next().value)
.to.deep.include({value: true});
});