Skip to content

Commit

Permalink
set up cross-platform testing for expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Jun 19, 2017
1 parent 04dbb97 commit 0a272d4
Show file tree
Hide file tree
Showing 68 changed files with 3,193 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"open-changed-examples": "git diff --name-only mb-pages HEAD -- docs/_posts/examples/*.html | awk '{print \"http://127.0.0.1:4000/mapbox-gl-js/example/\" substr($0,33,length($0)-37)}' | xargs open",
"test": "run-s lint lint-css test-unit test-flow",
"test-suite": "run-s test-render test-query",
"test-suite-clean": "find test/integration/*-tests -mindepth 2 -type d -not \\( -exec test -e \"{}/style.json\" \\; \\) -print | xargs -t rm -r",
"test-suite-clean": "find test/integration/{render,query}-tests -mindepth 2 -type d -not \\( -exec test -e \"{}/style.json\" \\; \\) -print | xargs -t rm -r",
"test-unit": "tap --reporter dot --no-coverage test/unit",
"test-render": "node test/render.test.js",
"test-query": "node test/query.test.js",
Expand Down
42 changes: 42 additions & 0 deletions test/expression.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

require('flow-remove-types/register');
const util = require('../src/util/util');
const expressionSuite = require('./integration').expression;
const compileExpression = require('../src/style-spec/function/compile');

let tests;

if (process.argv[1] === __filename && process.argv.length > 2) {
tests = process.argv.slice(2);
}

expressionSuite.run('js', {tests: tests}, (fixture) => {
const compiled = compileExpression(fixture.expression);

const testResult = {
compileResult: util.pick(compiled, ['result', 'js', 'isFeatureConstant', 'isZoomConstant', 'errors'])
};
if (compiled.result === 'success') testResult.compileResult.type = compiled.type.name;

if (compiled.result === 'success' && fixture.evaluate) {
const evaluateResults = [];
for (const input of fixture.evaluate) {
try {
const output = compiled.function.apply(null, input);
evaluateResults.push(output);
} catch (error) {
if (error.name === 'ExpressionEvaluationError') {
evaluateResults.push({ error: error.toJSON() });
} else {
evaluateResults.push({ error: error.message });
}
}
}
if (evaluateResults.length) {
testResult.evaluateResults = evaluateResults;
}
}

return testResult;
});
23 changes: 23 additions & 0 deletions test/integration/expression-tests/acos/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": [
"acos",
0.5
],
"evaluate": [
[
{},
{}
]
],
"expected": {
"compileResult": {
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": true,
"type": "number"
},
"evaluateResults": [
1.0471975511965976
]
}
}
57 changes: 57 additions & 0 deletions test/integration/expression-tests/and/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"expression": [
"&&",
[
"boolean",
[
"get",
[
"properties"
],
"x"
]
],
[
"boolean",
[
"get",
[
"properties"
],
"y"
]
]
],
"evaluate": [
[
{},
{
"properties": {
"x": true,
"y": false
}
}
],
[
{},
{
"properties": {
"x": true,
"y": true
}
}
]
],
"expected": {
"compileResult": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "boolean"
},
"evaluateResults": [
false,
true
]
}
}
23 changes: 23 additions & 0 deletions test/integration/expression-tests/asin/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": [
"asin",
0.5
],
"evaluate": [
[
{},
{}
]
],
"expected": {
"compileResult": {
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": true,
"type": "number"
},
"evaluateResults": [
0.5235987755982988
]
}
}
44 changes: 44 additions & 0 deletions test/integration/expression-tests/at/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"expression": [
"number",
[
"at",
[
"json_array",
[
"get",
[
"properties"
],
"arr"
]
],
1
]
],
"evaluate": [
[
{},
{
"properties": {
"arr": [
9,
8,
7
]
}
}
]
],
"expected": {
"compileResult": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"evaluateResults": [
8
]
}
}
23 changes: 23 additions & 0 deletions test/integration/expression-tests/atan/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": [
"atan",
1
],
"evaluate": [
[
{},
{}
]
],
"expected": {
"compileResult": {
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": true,
"type": "number"
},
"evaluateResults": [
0.7853981633974483
]
}
}
94 changes: 94 additions & 0 deletions test/integration/expression-tests/boolean/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"expression": [
"boolean",
[
"get",
[
"properties"
],
"x"
]
],
"evaluate": [
[
{},
{}
],
[
{},
{
"properties": {
"x": true
}
}
],
[
{},
{
"properties": {
"x": false
}
}
],
[
{},
{
"properties": {
"x": ""
}
}
],
[
{},
{
"properties": {
"x": "false"
}
}
],
[
{},
{
"properties": {
"x": 0
}
}
],
[
{},
{
"properties": {
"x": 1
}
}
],
[
{},
{
"properties": {
"x": null
}
}
]
],
"expected": {
"compileResult": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "boolean"
},
"evaluateResults": [
{
"error": "ExpressionEvaluationError: Property x not found in object with keys: []"
},
true,
false,
false,
true,
false,
true,
false
]
}
}
Loading

0 comments on commit 0a272d4

Please sign in to comment.