Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Sep 28, 2016
1 parent 096b15b commit 976f32f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 112 deletions.
10 changes: 5 additions & 5 deletions Source/Scene/Cesium3DTileStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ define([
* @example
* tileset.style = new Cesium.Cesium3DTileStyle({
* color : {
* conditions : {
* '${Height} >= 100' : 'color("purple", 0.5)',
* '${Height} >= 50' : 'color("red")',
* 'true' : 'color("blue")'
* }
* conditions : [
* ['${Height} >= 100', 'color("purple", 0.5)'],
* ['${Height} >= 50', 'color("red")'],
* ['true', 'color("blue")']
* ]
* },
* show : '${Height} > 0',
* meta : {
Expand Down
10 changes: 5 additions & 5 deletions Source/Scene/Cesium3DTileset.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,11 @@ define([
* @example
* tileset.style = new Cesium.Cesium3DTileStyle({
* color : {
* conditions : {
* '${Height} >= 100' : 'color("purple", 0.5)',
* '${Height} >= 50' : 'color("red")',
* 'true' : 'color("blue")'
* }
* conditions : [
* ['${Height} >= 100', 'color("purple", 0.5)'],
* ['${Height} >= 50', 'color("red")'],
* ['true' : 'color("blue")']
* ]
* },
* show : '${Height} > 0',
* meta : {
Expand Down
44 changes: 23 additions & 21 deletions Source/Scene/ConditionsExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ define([
* @example
* var expression = new Cesium.Expression({
* expression : 'regExp("^1(\\d)").exec(${id})',
* conditions : {
* '${expression} === "1"' : 'color("#FF0000")',
* '${expression} === "2"' : 'color("#00FF00")',
* 'true' : 'color("#FFFFFF")'
* }
* conditions : [
* ['${expression} === "1"', 'color("#FF0000")'],
* ['${expression} === "2"', 'color("#00FF00")'],
* ['true', 'color("#FFFFFF")']
* ]
* });
* expression.evaluateColor(feature, result); // returns a Cesium.Color object
*
Expand Down Expand Up @@ -78,23 +78,25 @@ define([
function setRuntime(expression) {
var runtimeConditions = [];
var conditions = expression._conditions;
var exp = expression._expression;
var length = conditions.length;
for (var i = 0; i < length; ++i) {
var statement = conditions[i];
var cond = String(statement[0]);
var condExpression = String(statement[1]);
if (defined(exp)) {
cond = cond.replace(expressionPlaceholder, exp);
condExpression = condExpression.replace(expressionPlaceholder, exp);
} else {
cond = cond.replace(expressionPlaceholder, 'undefined');
condExpression = condExpression.replace(expressionPlaceholder, 'undefined');
if (defined(conditions)) {
var exp = expression._expression;
var length = conditions.length;
for (var i = 0; i < length; ++i) {
var statement = conditions[i];
var cond = String(statement[0]);
var condExpression = String(statement[1]);
if (defined(exp)) {
cond = cond.replace(expressionPlaceholder, exp);
condExpression = condExpression.replace(expressionPlaceholder, exp);
} else {
cond = cond.replace(expressionPlaceholder, 'undefined');
condExpression = condExpression.replace(expressionPlaceholder, 'undefined');
}
runtimeConditions.push(new Statement(
new Expression(cond),
new Expression(condExpression)
));
}
runtimeConditions.push(new Statement(
new Expression(cond),
new Expression(condExpression)
));
}

expression._runtimeConditions = runtimeConditions;
Expand Down
6 changes: 5 additions & 1 deletion Source/Scene/Expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ define([
*/
Expression.prototype.evaluate = function(feature) {
scratchColorIndex = 0;
return this._runtimeAst.evaluate(feature);
var result = this._runtimeAst.evaluate(feature);
if (result instanceof Color) {
return Color.clone(result);
}
return result;
};

/**
Expand Down
50 changes: 25 additions & 25 deletions Specs/Scene/Cesium3DTileStyleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ defineSuite([

it ('sets color value to conditional', function() {
var jsonExp = {
conditions : {
'${height} > 2' : 'color("cyan")',
'true' : 'color("blue")'
}
conditions : [
['${height} > 2', 'color("cyan")'],
['true', 'color("blue")']
]
};

var style = new Cesium3DTileStyle({
Expand Down Expand Up @@ -295,11 +295,11 @@ defineSuite([
var style = new Cesium3DTileStyle({
"color" : {
"expression" : "regExp('^1(\\d)').exec(${id})",
"conditions" : {
"${expression} === '1'" : "color('#FF0000')",
"${expression} === '2'" : "color('#00FF00')",
"true" : "color('#FFFFFF')"
}
"conditions" : [
["${expression} === '1'", "color('#FF0000')"],
["${expression} === '2'", "color('#00FF00')"],
["true", "color('#FFFFFF')"]
]
}
});
expect(style.show.evaluate(feature1)).toEqual(true);
Expand All @@ -311,14 +311,14 @@ defineSuite([
var style = new Cesium3DTileStyle({
"color" : {
"expression" : "${Height}",
"conditions" : {
"(${expression} >= 1.0) && (${expression} < 10.0)" : "color('#FF00FF')",
"(${expression} >= 10.0) && (${expression} < 30.0)" : "color('#FF0000')",
"(${expression} >= 30.0) && (${expression} < 50.0)" : "color('#FFFF00')",
"(${expression} >= 50.0) && (${expression} < 70.0)" : "color('#00FF00')",
"(${expression} >= 70.0) && (${expression} < 100.0)" : "color('#00FFFF')",
"(${expression} >= 100.0)" : "color('#0000FF')"
}
"conditions" : [
["(${expression} >= 1.0) && (${expression} < 10.0)", "color('#FF00FF')"],
["(${expression} >= 10.0) && (${expression} < 30.0)", "color('#FF0000')"],
["(${expression} >= 30.0) && (${expression} < 50.0)", "color('#FFFF00')"],
["(${expression} >= 50.0) && (${expression} < 70.0)", "color('#00FF00')"],
["(${expression} >= 70.0) && (${expression} < 100.0)", "color('#00FFFF')"],
["(${expression} >= 100.0)", "color('#0000FF')"]
]
}
});
expect(style.show.evaluate(feature1)).toEqual(true);
Expand All @@ -329,14 +329,14 @@ defineSuite([
it ('applies color style with conditional', function() {
var style = new Cesium3DTileStyle({
"color" : {
"conditions" : {
"(${Height} >= 100.0)" : "color('#0000FF')",
"(${Height} >= 70.0)" : "color('#00FFFF')",
"(${Height} >= 50.0)" : "color('#00FF00')",
"(${Height} >= 30.0)" : "color('#FFFF00')",
"(${Height} >= 10.0)" : "color('#FF0000')",
"(${Height} >= 1.0)" : "color('#FF00FF')"
}
"conditions" : [
["(${Height} >= 100.0)", "color('#0000FF')"],
["(${Height} >= 70.0)", "color('#00FFFF')"],
["(${Height} >= 50.0)", "color('#00FF00')"],
["(${Height} >= 30.0)", "color('#FFFF00')"],
["(${Height} >= 10.0)", "color('#FF0000')"],
["(${Height} >= 1.0)", "color('#FF00FF')"]
]
}
});
expect(style.show.evaluate(feature1)).toEqual(true);
Expand Down
16 changes: 8 additions & 8 deletions Specs/Scene/Cesium3DTilesetSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1177,10 +1177,10 @@ defineSuite([
// ${id} < 10 will always evaluate to true
tileset.style = new Cesium3DTileStyle({
color : {
conditions : {
'${id} < 10' : 'color("red")',
'true' : 'color("blue")'
}
conditions : [
['${id} < 10', 'color("red")'],
['true', 'color("blue")']
]
}
});
var color = scene.renderForSpecs();
Expand All @@ -1192,10 +1192,10 @@ defineSuite([
// ${id}>= 10 will always evaluate to false
tileset.style = new Cesium3DTileStyle({
color : {
conditions : {
'${id} >= 10' : 'color("red")',
'true' : 'color("blue")'
}
conditions : [
['${id} >= 10', 'color("red")'],
['true', 'color("blue")']
]
}
});
color = scene.renderForSpecs();
Expand Down
94 changes: 47 additions & 47 deletions Specs/Scene/ConditionsExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,37 @@ defineSuite([
};

var jsonExp = {
conditions : {
'${Height} > 100' : 'color("blue")',
'${Height} > 50' : 'color("red")',
'true' : 'color("green")'
}
conditions : [
['${Height} > 100', 'color("blue")'],
['${Height} > 50', 'color("red")'],
['true', 'color("green")']
]
};

var jsonExpWithExpression = {
expression : '${Height}/2',
conditions : {
'${expression} > 50' : 'color("blue")',
'${expression} > 25' : 'color("red")',
'true' : 'color("green")'
}
conditions : [
['${expression} > 50', 'color("blue")'],
['${expression} > 25', 'color("red")'],
['true', 'color("green")']
]
};

var jsonExpWithMultipleExpression = {
expression : '${Height}/2',
conditions : {
'${expression} > 50 && ${expression} < 100' : 'color("blue")',
'${expression} > 25 && ${expression} < 26' : 'color("red")',
'true' : 'color("green")'
}
conditions : [
['${expression} > 50 && ${expression} < 100', 'color("blue")'],
['${expression} > 25 && ${expression} < 26', 'color("red")'],
['true', 'color("green")']
]
};


var jsonExpWithUndefinedExpression = {
conditions : {
'${expression} === undefined' : 'color("blue")',
'true' : 'color("green")'
}
conditions : [
['${expression} === undefined', 'color("blue")'],
['true', 'color("green")']
]
};


Expand All @@ -60,60 +60,60 @@ defineSuite([
it('constructs with expression', function() {
var expression = new ConditionsExpression(jsonExpWithExpression);
expect(expression._expression).toEqual('${Height}/2');
expect(expression._conditions).toEqual({
'${expression} > 50' : 'color("blue")',
'${expression} > 25' : 'color("red")',
'true' : 'color("green")'
});
expect(expression._conditions).toEqual([
['${expression} > 50', 'color("blue")'],
['${expression} > 25', 'color("red")'],
['true', 'color("green")']
]);
});

it('evaluates undefined expression', function() {
var expression = new ConditionsExpression(jsonExpWithExpression);
expect(expression._expression).toEqual('${Height}/2');
expect(expression._conditions).toEqual({
'${expression} > 50' : 'color("blue")',
'${expression} > 25' : 'color("red")',
'true' : 'color("green")'
});
expect(expression._conditions).toEqual([
['${expression} > 50', 'color("blue")'],
['${expression} > 25', 'color("red")'],
['true', 'color("green")']
]);
});

it('evaluates conditional', function() {
var expression = new ConditionsExpression(jsonExp);
expect(expression.evaluate(new MockFeature('101'))).toEqual(Color.BLUE);
expect(expression.evaluate(new MockFeature('52'))).toEqual(Color.RED);
expect(expression.evaluate(new MockFeature('3'))).toEqual(Color.GREEN);
expect(expression.evaluate(new MockFeature(101))).toEqual(Color.BLUE);
expect(expression.evaluate(new MockFeature(52))).toEqual(Color.RED);
expect(expression.evaluate(new MockFeature(3))).toEqual(Color.GREEN);
});

it('evaluates conditional with multiple expressions', function() {
var expression = new ConditionsExpression(jsonExpWithMultipleExpression);
expect(expression.evaluate(new MockFeature('101'))).toEqual(Color.BLUE);
expect(expression.evaluate(new MockFeature('52'))).toEqual(Color.GREEN);
expect(expression.evaluate(new MockFeature('3'))).toEqual(Color.GREEN);
expect(expression.evaluate(new MockFeature(101))).toEqual(Color.BLUE);
expect(expression.evaluate(new MockFeature(52))).toEqual(Color.GREEN);
expect(expression.evaluate(new MockFeature(3))).toEqual(Color.GREEN);
});

it('constructs and evaluates empty conditional', function() {
var expression = new ConditionsExpression({
"conditions" : {}
"conditions" : []
});
expect(expression._conditions).toEqual({});
expect(expression.evaluate(new MockFeature('101'))).toEqual(undefined);
expect(expression.evaluate(new MockFeature('52'))).toEqual(undefined);
expect(expression.evaluate(new MockFeature('3'))).toEqual(undefined);
expect(expression._conditions).toEqual([]);
expect(expression.evaluate(new MockFeature(101))).toEqual(undefined);
expect(expression.evaluate(new MockFeature(52))).toEqual(undefined);
expect(expression.evaluate(new MockFeature(3))).toEqual(undefined);
});

it('constructs and evaluates empty', function() {
var expression = new ConditionsExpression({});
var expression = new ConditionsExpression([]);
expect(expression._conditions).toEqual(undefined);
expect(expression.evaluate(new MockFeature('101'))).toEqual(undefined);
expect(expression.evaluate(new MockFeature('52'))).toEqual(undefined);
expect(expression.evaluate(new MockFeature('3'))).toEqual(undefined);
expect(expression.evaluate(new MockFeature(101))).toEqual(undefined);
expect(expression.evaluate(new MockFeature(52))).toEqual(undefined);
expect(expression.evaluate(new MockFeature(3))).toEqual(undefined);
});

it('evaluates conditional with expression', function() {
var expression = new ConditionsExpression(jsonExpWithExpression);
expect(expression.evaluate(new MockFeature('101'))).toEqual(Color.BLUE);
expect(expression.evaluate(new MockFeature('52'))).toEqual(Color.RED);
expect(expression.evaluate(new MockFeature('3'))).toEqual(Color.GREEN);
expect(expression.evaluate(new MockFeature(101))).toEqual(Color.BLUE);
expect(expression.evaluate(new MockFeature(52))).toEqual(Color.RED);
expect(expression.evaluate(new MockFeature(3))).toEqual(Color.GREEN);
});

it('evaluates undefined conditional expression', function() {
Expand Down

0 comments on commit 976f32f

Please sign in to comment.