From 44c419a662f114587f6059aa8a705c7f341fb67b Mon Sep 17 00:00:00 2001 From: Lauren Budorick Date: Mon, 24 Jul 2017 12:47:49 -0700 Subject: [PATCH] Correctly evaluate enum-based identity functions (#5023) --- src/style-spec/function/index.js | 2 +- .../text-anchor/property-function/style.json | 33 ++++-------- .../text-justify/property-function/style.json | 15 ++---- .../property-function/style.json | 12 ++--- test/unit/style-spec/function.test.js | 51 +++++++++++++++++++ 5 files changed, 72 insertions(+), 41 deletions(-) diff --git a/src/style-spec/function/index.js b/src/style-spec/function/index.js index e3c5db5ea9c..898f2c3b79e 100644 --- a/src/style-spec/function/index.js +++ b/src/style-spec/function/index.js @@ -212,7 +212,7 @@ function evaluateExponentialFunction(parameters, propertySpec, input) { function evaluateIdentityFunction(parameters, propertySpec, input) { if (propertySpec.type === 'color') { input = parseColor(input); - } else if (getType(input) !== propertySpec.type) { + } else if (getType(input) !== propertySpec.type && (propertySpec.type !== 'enum' || !propertySpec.values[input])) { input = undefined; } return coalesce(input, parameters.default, propertySpec.default); diff --git a/test/integration/render-tests/text-anchor/property-function/style.json b/test/integration/render-tests/text-anchor/property-function/style.json index 27c9c79b7b1..08d0351ca73 100644 --- a/test/integration/render-tests/text-anchor/property-function/style.json +++ b/test/integration/render-tests/text-anchor/property-function/style.json @@ -16,7 +16,7 @@ "features": [ { "type": "Feature", - "properties": { "x": 0 }, + "properties": { "x": "bottom-right" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -24,7 +24,7 @@ }, { "type": "Feature", - "properties": { "x": 1 }, + "properties": { "x": "bottom" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -32,7 +32,7 @@ }, { "type": "Feature", - "properties": { "x": 2 }, + "properties": { "x": "bottom-left" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -40,7 +40,7 @@ }, { "type": "Feature", - "properties": { "x": 3 }, + "properties": { "x": "right" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -48,7 +48,7 @@ }, { "type": "Feature", - "properties": { "x": 4 }, + "properties": { "x": "center" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -56,7 +56,7 @@ }, { "type": "Feature", - "properties": { "x": 5 }, + "properties": { "x": "left" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -64,7 +64,7 @@ }, { "type": "Feature", - "properties": { "x": 6 }, + "properties": { "x": "top-right" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -72,7 +72,7 @@ }, { "type": "Feature", - "properties": { "x": 7 }, + "properties": { "x": "top" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -80,7 +80,7 @@ }, { "type": "Feature", - "properties": { "x": 8 }, + "properties": { "x": "top-left" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -100,19 +100,8 @@ "text-field": "x", "text-size": 40, "text-anchor": { - "type": "categorical", - "property": "x", - "stops": [ - [0, "bottom-right"], - [1, "bottom"], - [2, "bottom-left"], - [3, "right"], - [4, "center"], - [5, "left"], - [6, "top-right"], - [7, "top"], - [8, "top-left"] - ] + "type": "identity", + "property": "x" }, "text-allow-overlap": true, "text-font": [ diff --git a/test/integration/render-tests/text-justify/property-function/style.json b/test/integration/render-tests/text-justify/property-function/style.json index 49258e755ba..eea89606379 100644 --- a/test/integration/render-tests/text-justify/property-function/style.json +++ b/test/integration/render-tests/text-justify/property-function/style.json @@ -16,7 +16,7 @@ "features": [ { "type": "Feature", - "properties": { "x": 0 }, + "properties": { "x": "right" }, "geometry": { "type": "Point", "coordinates": [ -100, 0 ] @@ -24,7 +24,7 @@ }, { "type": "Feature", - "properties": { "x": 1 }, + "properties": { "x": "center" }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] @@ -32,7 +32,7 @@ }, { "type": "Feature", - "properties": { "x": 2 }, + "properties": { "x": "left" }, "geometry": { "type": "Point", "coordinates": [ 100, 0 ] @@ -51,13 +51,8 @@ "layout": { "text-field": "A very very long \n line label", "text-justify": { - "type": "categorical", - "property": "x", - "stops": [ - [0, "right"], - [1, "center"], - [2, "left"] - ] + "type": "identity", + "property": "x" }, "text-allow-overlap": true, "text-font": [ diff --git a/test/integration/render-tests/text-transform/property-function/style.json b/test/integration/render-tests/text-transform/property-function/style.json index 5ea96e11c58..909c098d6f5 100644 --- a/test/integration/render-tests/text-transform/property-function/style.json +++ b/test/integration/render-tests/text-transform/property-function/style.json @@ -16,7 +16,7 @@ "features": [ { "type": "Feature", - "properties": { "x": 0 }, + "properties": { "x": "uppercase" }, "geometry": { "type": "Point", "coordinates": [ 0, -10 ] @@ -24,7 +24,7 @@ }, { "type": "Feature", - "properties": { "x": 1 }, + "properties": { "x": "lowercase" }, "geometry": { "type": "Point", "coordinates": [ 0, 10 ] @@ -43,12 +43,8 @@ "layout": { "text-field": "hello", "text-transform": { - "type": "categorical", - "property": "x", - "stops": [ - [0, "uppercase"], - [1, "lowercase"] - ] + "type": "identity", + "property": "x" }, "text-font": [ "Open Sans Semibold", diff --git a/test/unit/style-spec/function.test.js b/test/unit/style-spec/function.test.js index 0248082e23c..5f10b23bfed 100644 --- a/test/unit/style-spec/function.test.js +++ b/test/unit/style-spec/function.test.js @@ -972,6 +972,57 @@ test('identity function', (t) => { t.end(); }); + t.test('valid enum', (t) => { + const f = createFunction({ + property: 'foo', + type: 'identity' + }, { + type: 'enum', + values: { + bar: {} + }, + default: 'def' + }); + + t.equal(f(0, {foo: 'bar'}), 'bar'); + + t.end(); + }); + + t.test('invalid enum, spec default', (t) => { + const f = createFunction({ + property: 'foo', + type: 'identity' + }, { + type: 'enum', + values: { + bar: {} + }, + default: 'def' + }); + + t.equal(f(0, {foo: 'baz'}), 'def'); + + t.end(); + }); + + t.test('invalid type for enum, spec default', (t) => { + const f = createFunction({ + property: 'foo', + type: 'identity' + }, { + type: 'enum', + values: { + bar: {} + }, + default: 'def' + }); + + t.equal(f(0, {foo: 3}), 'def'); + + t.end(); + }); + t.end(); });