@alu0101337760/constant-folding
Version:
Constant Folding javascript code
52 lines (44 loc) • 1.61 kB
JavaScript
const constantFolding = require('../src/constant-folding');
const should = require('chai').should();
const example1 = `a=2*3;`;
const example2 = `5*5*5`;
const example3 = `var d = 3+"c";`;
const example4 = `30+2-4`;
const example5 = `const funFunction = Number()`;
const example6 = `var a = 2+3*5+b;`;
const arr1 = `[0, 1, 2].length`;
const arr2 = `[1, 2, 3][2-1];`;
const expectedArr2 = "2;";
const expectedArr1 = "3;";
const expected2 = `125;`;
const expected3 = `var d = '3c';`;
const expected4 = `28;`;
const expected5 = `const funFunction = Number();`;
const expected6 = `var a = 17 + b;`;
const expectedOutput = `a = 6;`;
describe('constantFolding tests', () => {
it('works correctly with normal functions', () => {
constantFolding(example1).should.equal(expectedOutput);
});
it('works correctly with normal functions', () => {
constantFolding(example2).should.equal(expected2);
});
it('works correctly with normal functions', () => {
constantFolding(example3).should.equal(expected3);
});
it('works correctly with normal functions', () => {
constantFolding(example4).should.equal(expected4);
});
it('works correctly with normal functions', () => {
constantFolding(example6).should.equal(expected6);
});
it("Works when there's no binary expression with literals", () => {
constantFolding(example5).should.equal(expected5);
});
it("Works with Array.length", () => {
constantFolding(arr1).should.equal(expectedArr1);
});
it("Works with binary expressions as index", () => {
constantFolding(arr2).should.equal(expectedArr2);
});
});