diff --git a/packages/babel/test/evaluation.js b/packages/babel/test/evaluation.js index 632691bb0d..999373933d 100644 --- a/packages/babel/test/evaluation.js +++ b/packages/babel/test/evaluation.js @@ -4,7 +4,67 @@ var parse = require("../lib/helpers/parse"); var assert = require("assert"); suite("evaluation", function () { - test("binary expression", function () { + test("UnaryExpression: void a", function () { + traverse(parse("void 0"), { + enter: function (node) { + if (this.isUnaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: undefined }); + } + } + }); + }); + + test("UnaryExpression: !a", function () { + traverse(parse("!true"), { + enter: function (node) { + if (this.isUnaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: false }); + } + } + }); + }); + + test("UnaryExpression +a", function () { + traverse(parse("+'2'"), { + enter: function (node) { + if (this.isUnaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 2 }); + } + } + }); + }); + + test("UnaryExpression -a", function () { + traverse(parse("-'2'"), { + enter: function (node) { + if (this.isUnaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: -2 }); + } + } + }); + }); + + test("UnaryExpression: ~a", function () { + traverse(parse("~1"), { + enter: function (node) { + if (this.isUnaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: -2 }); + } + } + }); + }); + + test("BinaryExpression: a - b", function () { + traverse(parse("3 - 1"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 2 }); + } + } + }); + }); + + test("BinaryExpression: a + b", function () { traverse(parse("5 + 5"), { enter: function (node) { if (this.isBinaryExpression()) { @@ -12,7 +72,109 @@ suite("evaluation", function () { } } }); + }); + test("BinaryExpression: a / b", function () { + traverse(parse("10 / 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 5 }); + } + } + }); + }); + + test("BinaryExpression: a * b", function () { + traverse(parse("2 * 3"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 6 }); + } + } + }); + }); + + test("BinaryExpression: a % b", function () { + traverse(parse("4 % 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 0 }); + } + } + }); + }); + + test("BinaryExpression: a ** b", function () { + traverse(parse("2 ** 3", { features: { "es7.exponentiationOperator": true } }), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 8 }); + } + } + }); + }); + + test("BinaryExpression: a < b", function () { + traverse(parse("1 < 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: true }); + } + } + }); + }); + + test("BinaryExpression: a > b", function () { + traverse(parse("1 > 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: false }); + } + } + }); + }); + + test("BinaryExpression: a <= b", function () { + traverse(parse("1 <= 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: true }); + } + } + }); + }); + + test("BinaryExpression: a >= b", function () { + traverse(parse("1 >= 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: false }); + } + } + }); + }); + + test("BinaryExpression: a == b", function () { + traverse(parse("1 == '1'"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: true }); + } + } + }); + }); + + test("BinaryExpression: a != b", function () { + traverse(parse("1 != 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: true }); + } + } + }); + }); + + test("BinaryExpression: a === b", function () { traverse(parse("'str' === 'str'"), { enter: function(node) { if (this.isBinaryExpression()) { @@ -20,7 +182,6 @@ suite("evaluation", function () { } } }); - traverse(parse("'four' === 4"), { enter: function(node) { if (this.isBinaryExpression()) { @@ -30,7 +191,94 @@ suite("evaluation", function () { }); }); - test("logical expression", function () { + test("BinaryExpression: a !== b", function () { + traverse(parse("'four' !== '4'"), { + enter: function(node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: true }); + } + } + }); + traverse(parse("'str' !== 'str'"), { + enter: function(node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: false }); + } + } + }); + }); + + test("BinaryExpression: a | b", function () { + traverse(parse("1 | 0"), { + enter: function(node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 1 }); + } + } + }); + }); + + test("BinaryExpression: a & b", function () { + traverse(parse("1 & 1"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 1 }); + } + } + }); + }); + + test("BinaryExpression: a ^ b", function () { + traverse(parse("1 ^ 0"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 1 }); + } + } + }); + }); + + test("BinaryExpression: a << b", function () { + traverse(parse("1 << 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 4 }); + } + } + }); + }); + + test("BinaryExpression: a >> b", function () { + traverse(parse("1 >> 2"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: true, value: 0 }); + } + } + }); + }); + + test("BinaryExpression: a in b (not evaluated)", function () { + traverse(parse("1 in [1]"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: false, value: undefined }); + } + } + }); + }); + + test("BinaryExpression: a instanceof b (not evaluated)", function () { + traverse(parse("A instanceof B"), { + enter: function (node) { + if (this.isBinaryExpression()) { + assert.deepEqual(this.evaluate(), { confident: false, value: undefined }); + } + } + }); + }); + + test("LogicalExpression", function () { traverse(parse("'abc' === 'abc' && 1 === 1"), { enter: function(node) { if (this.isLogicalExpression()) {