@alu0101353647/constant-folding
Version:
Constant Folding javascript code
33 lines (31 loc) • 1.64 kB
JavaScript
const everything = require('../src/constant-folding');
const should = require('chai').should();
const examples = [
{ example: `a=2*3;`, expectedOutput: `a = 6;`},
{ example: `a=2-3;`, expectedOutput: `a = -1;`},
{ example: `a=6/2;`, expectedOutput: `a = 3;`},
{ example: `a=2*6/3;`, expectedOutput: `a = 4;`},
{ example: `[3, [4, 5], [6], [[7], 8]]`, expectedOutput: `[3, 4, 5, 6, 7, 8];`},
{ example: `["a", "b", "c"].concat(["d", "e"], "f", "g", ["h"]);`, expectedOutput: `["a", "b", "c", "d", "e", "f", "g", "h"];`},
{ example: `["a", "b", "c"].join();`, expectedOutput: `"a,b,c";`},
{ example: `["a", "b", "c"].join('@');`, expectedOutput: `"a@b@c";`},
{ example: `[1, 2, 3].length;`, expectedOutput: `3;`},
{ example: `[1, 2, 3][2-1];`, expectedOutput: `2;`},
{ example: `[1, 2, 3].shift();`, expectedOutput: `1;`},
{ example: `[1, 2, 3].slice(0, 1+1);`, expectedOutput: `[1, 2];`},
{ example: `[1, 2, 3].slice(1, 2);`, expectedOutput: `[2];`},
{ example: `[1, 2, 3].slice(1);`, expectedOutput: `[1];`},
{ example: `["a", "b", "c"].pop();`, expectedOutput: `c;`},
{ example: `["a", "b", "c"].reverse();`, expectedOutput: `["c", "b", "a"];`},
{ example: `[0, 24, 67].reverse().concat([2], [3, 4, [[6]]], 3, 5)[0] + 5;`, expectedOutput: `72;`},
{ example: `32 * 3;`, expectedOutput: `96;`},
];
describe('constantFolding tests', () => {
let i = 0;
for (let example of examples) {
it(`Test #${i}`, () => {
everything.constantFolding(example.example).should.equal(example.expectedOutput);
});
i += 1;
}
});