Skip to content

Commit

Permalink
Correctly evaluate enum-based identity functions (#5023)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Budorick authored Jul 24, 2017
1 parent 11b6d07 commit 44c419a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/style-spec/function/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,71 @@
"features": [
{
"type": "Feature",
"properties": { "x": 0 },
"properties": { "x": "bottom-right" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 1 },
"properties": { "x": "bottom" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 2 },
"properties": { "x": "bottom-left" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 3 },
"properties": { "x": "right" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 4 },
"properties": { "x": "center" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 5 },
"properties": { "x": "left" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 6 },
"properties": { "x": "top-right" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 7 },
"properties": { "x": "top" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 8 },
"properties": { "x": "top-left" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
Expand All @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
"features": [
{
"type": "Feature",
"properties": { "x": 0 },
"properties": { "x": "right" },
"geometry": {
"type": "Point",
"coordinates": [ -100, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 1 },
"properties": { "x": "center" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
},
{
"type": "Feature",
"properties": { "x": 2 },
"properties": { "x": "left" },
"geometry": {
"type": "Point",
"coordinates": [ 100, 0 ]
Expand All @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
"features": [
{
"type": "Feature",
"properties": { "x": 0 },
"properties": { "x": "uppercase" },
"geometry": {
"type": "Point",
"coordinates": [ 0, -10 ]
}
},
{
"type": "Feature",
"properties": { "x": 1 },
"properties": { "x": "lowercase" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 10 ]
Expand All @@ -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",
Expand Down
51 changes: 51 additions & 0 deletions test/unit/style-spec/function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit 44c419a

Please sign in to comment.