From 0f5016a79341fb58c4f589e65631a72f71996720 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Tue, 18 Oct 2016 11:26:37 -0400 Subject: [PATCH] Support both === and !== --- Source/Scene/Expression.js | 21 +++++++++++++-- Specs/Scene/ExpressionSpec.js | 50 +++++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Source/Scene/Expression.js b/Source/Scene/Expression.js index a7a347a22d22..629894b7b827 100644 --- a/Source/Scene/Expression.js +++ b/Source/Scene/Expression.js @@ -18,7 +18,7 @@ define([ "use strict"; var unaryOperators = ['!', '-', '+']; - var binaryOperators = ['+', '-', '*', '/', '%', '==', '!=', '>', '>=', '<', '<=', '&&', '||', '!~', '=~']; + var binaryOperators = ['+', '-', '*', '/', '%', '===', '==', '!==', '!=', '>', '>=', '<', '<=', '&&', '||', '!~', '=~']; var variableRegex = /\${(.*?)}/g; var backslashRegex = /\\/g; @@ -531,8 +531,12 @@ define([ node.evaluate = node._evaluateDivide; } else if (node._value === '%') { node.evaluate = node._evaluateMod; + } else if (node._value === '===') { + node.evaluate = node._evaluateEqualsStrict; } else if (node._value === '==') { node.evaluate = node._evaluateEquals; + } else if (node._value === '!==') { + node.evaluate = node._evaluateNotEquals; } else if (node._value === '!=') { node.evaluate = node._evaluateNotEquals; } else if (node._value === '<') { @@ -834,6 +838,15 @@ define([ return left % right; }; + Node.prototype._evaluateEqualsStrict = function(feature) { + var left = this._left.evaluate(feature); + var right = this._right.evaluate(feature); + if ((right instanceof Color) && (left instanceof Color)) { + return Color.equals(left, right); + } + return left === right; + }; + Node.prototype._evaluateEquals = function(feature) { var left = this._left.evaluate(feature); var right = this._right.evaluate(feature); @@ -1095,9 +1108,13 @@ define([ //>>includeEnd('debug'); return value + left; case ExpressionNodeType.BINARY: - // Supported types: ||, &&, ==, !=, <, >, <=, >=, +, -, *, /, % + // Supported types: ||, &&, ===, ==, !==, !=, <, >, <=, >=, +, -, *, /, % if (value === '%') { return 'mod(' + left + ', ' + right + ')'; + } else if (value === '===') { + return '(' + left + ' == ' + right + ')'; + } else if (value === '!==') { + return '(' + left + ' != ' + right + ')'; } return '(' + left + ' ' + value + ' ' + right + ')'; case ExpressionNodeType.CONDITIONAL: diff --git a/Specs/Scene/ExpressionSpec.js b/Specs/Scene/ExpressionSpec.js index 806db406cb67..a4729515b1d8 100644 --- a/Specs/Scene/ExpressionSpec.js +++ b/Specs/Scene/ExpressionSpec.js @@ -160,14 +160,6 @@ defineSuite([ return new Expression('2 & 3'); }).toThrowDeveloperError(); - expect(function() { - return new Expression('2 === 3'); - }).toThrowDeveloperError(); - - expect(function() { - return new Expression('2 !== 3'); - }).toThrowDeveloperError(); - expect(function() { return new Expression('2 << 3'); }).toThrowDeveloperError(); @@ -543,6 +535,20 @@ defineSuite([ expect(expression.evaluate(undefined)).toEqual(2); }); + it('evaluates binary equals strict', function() { + var expression = new Expression('\'hello\' === \'hello\''); + expect(expression.evaluate(undefined)).toEqual(true); + + expression = new Expression('1 === 2'); + expect(expression.evaluate(undefined)).toEqual(false); + + expression = new Expression('false === true === false'); + expect(expression.evaluate(undefined)).toEqual(true); + + expression = new Expression('1 === "1"'); + expect(expression.evaluate(undefined)).toEqual(false); + }); + it('evaluates binary equals', function() { var expression = new Expression('\'hello\' == \'hello\''); expect(expression.evaluate(undefined)).toEqual(true); @@ -552,6 +558,20 @@ defineSuite([ expression = new Expression('false == true == false'); expect(expression.evaluate(undefined)).toEqual(true); + + expression = new Expression('1 == "1"'); + expect(expression.evaluate(undefined)).toEqual(true); + }); + + it('evaluates binary not equals strict', function() { + var expression = new Expression('\'hello\' !== \'hello\''); + expect(expression.evaluate(undefined)).toEqual(false); + + expression = new Expression('1 !== 2'); + expect(expression.evaluate(undefined)).toEqual(true); + + expression = new Expression('false !== true !== false'); + expect(expression.evaluate(undefined)).toEqual(true); }); it('evaluates binary not equals', function() { @@ -1292,6 +1312,13 @@ defineSuite([ expect(shaderExpression).toEqual(expected); }); + it('gets shader expression for binary equals strict', function() { + var expression = new Expression('1.0 === 2.0'); + var shaderExpression = expression.getShaderExpression('', {}); + var expected = '(1.0 == 2.0)'; + expect(shaderExpression).toEqual(expected); + }); + it('gets shader expression for binary equals', function() { var expression = new Expression('1.0 == 2.0'); var shaderExpression = expression.getShaderExpression('', {}); @@ -1299,6 +1326,13 @@ defineSuite([ expect(shaderExpression).toEqual(expected); }); + it('gets shader expression for binary not equals strict', function() { + var expression = new Expression('1.0 !== 2.0'); + var shaderExpression = expression.getShaderExpression('', {}); + var expected = '(1.0 != 2.0)'; + expect(shaderExpression).toEqual(expected); + }); + it('gets shader expression for binary not equals', function() { var expression = new Expression('1.0 != 2.0'); var shaderExpression = expression.getShaderExpression('', {});