Skip to content

Commit

Permalink
Support both === and !==
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 18, 2016
1 parent 8823472 commit 0f5016a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
21 changes: 19 additions & 2 deletions Source/Scene/Expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ define([
"use strict";

var unaryOperators = ['!', '-', '+'];
var binaryOperators = ['+', '-', '*', '/', '%', '==', '!=', '>', '>=', '<', '<=', '&&', '||', '!~', '=~'];
var binaryOperators = ['+', '-', '*', '/', '%', '===', '==', '!==', '!=', '>', '>=', '<', '<=', '&&', '||', '!~', '=~'];

var variableRegex = /\${(.*?)}/g;
var backslashRegex = /\\/g;
Expand Down Expand Up @@ -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 === '<') {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
50 changes: 42 additions & 8 deletions Specs/Scene/ExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -1292,13 +1312,27 @@ 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('', {});
var expected = '(1.0 == 2.0)';
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('', {});
Expand Down

0 comments on commit 0f5016a

Please sign in to comment.