Skip to content

Commit

Permalink
Enable implicit type assertions for array types
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Nov 22, 2017
1 parent 1fb68f3 commit 4058f99
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/style-spec/expression/parsing_context.js
Expand Up @@ -4,6 +4,9 @@ const Scope = require('./scope');
const {checkSubtype} = require('./types');
const ParsingError = require('./parsing_error');
const Literal = require('./definitions/literal');
const Assertion = require('./definitions/assertion');
const ArrayAssertion = require('./definitions/array');
const Coercion = require('./definitions/coercion');

import type {Expression} from './expression';
import type {Type} from './types';
Expand Down Expand Up @@ -77,10 +80,10 @@ class ParsingContext {
expected.kind === 'boolean';

if (canAssert && actual.kind === 'value') {
const Assertion = require('./definitions/assertion');
parsed = new Assertion(expected, [parsed]);
} else if (expected.kind === 'array' && actual.kind === 'value') {
parsed = new ArrayAssertion(expected, parsed);
} else if (expected.kind === 'color' && (actual.kind === 'value' || actual.kind === 'string')) {
const Coercion = require('./definitions/coercion');
parsed = new Coercion(expected, [parsed]);
}

Expand Down
21 changes: 21 additions & 0 deletions test/integration/expression-tests/array/implicit-1/test.json
@@ -0,0 +1,21 @@
{
"expression": ["at", 0, ["get", "array"]],
"inputs": [
[{}, {"properties": {"array": [0, 1, 2]}}],
[{}, {"properties": {"array": "not"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "value"
},
"outputs": [
0,
{
"error": "Expected value to be of type array, but found string instead."
}
]
}
}
26 changes: 26 additions & 0 deletions test/integration/expression-tests/array/implicit-2/test.json
@@ -0,0 +1,26 @@
{
"propertySpec": {"type": "array", "value": "string"},
"expression": ["get", "array"],
"inputs": [
[{}, {"properties": {"array": ["a", "b"]}}],
[{}, {"properties": {"array": [1, 2]}}],
[{}, {"properties": {"array": "not"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "array<string>"
},
"outputs": [
["a", "b"],
{
"error": "Expected value to be of type array<string>, but found array<number, 2> instead."
},
{
"error": "Expected value to be of type array<string>, but found string instead."
}
]
}
}
26 changes: 26 additions & 0 deletions test/integration/expression-tests/array/implicit-3/test.json
@@ -0,0 +1,26 @@
{
"propertySpec": {"type": "array", "value": "number", "length": 2},
"expression": ["get", "array"],
"inputs": [
[{}, {"properties": {"array": [1, 2]}}],
[{}, {"properties": {"array": [1, 2, 3]}}],
[{}, {"properties": {"array": "not"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "array<number, 2>"
},
"outputs": [
[1, 2],
{
"error": "Expected value to be of type array<number, 2>, but found array<number, 3> instead."
},
{
"error": "Expected value to be of type array<number, 2>, but found string instead."
}
]
}
}

0 comments on commit 4058f99

Please sign in to comment.