Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate expression tests to jest #965

Merged
merged 14 commits into from
Feb 8, 2022
1 change: 0 additions & 1 deletion .github/workflows/test-expression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ jobs:
node-version: 16
architecture: x64
- run: npm ci
- run: npm run build-dev
- run: npm run test-expression
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
"test-browser": "jest ./test/integration/browser",
"test-render": "node --loader ts-node/esm --experimental-specifier-resolution=node --experimental-json-modules --max-old-space-size=2048 test/integration/render/render.test.ts",
"test-query": "jest test/integration/query",
"test-expression": "node --loader ts-node/esm --experimental-specifier-resolution=node test/integration/expression/expression.test.ts",
"test-expression": "jest test/integration/expression",
"test-unit": "jest ./src",
"codegen": "npm run generate-style-code && npm run generate-struct-arrays && npm run generate-style-spec && npm run generate-shaders",
"benchmark": "node --loader ts-node/esm --experimental-specifier-resolution=node bench/run-benchmarks.ts",
Expand Down
120 changes: 52 additions & 68 deletions test/integration/expression/expression.test.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,69 @@
import {fileURLToPath} from 'url';

import {run} from './expression';
import path from 'path';
import fs from 'fs';
import glob from 'glob';
import {createPropertyExpression} from '../../../src/style-spec/expression';
import {isFunction} from '../../../src/style-spec/function';
import convertFunction from '../../../src/style-spec/function/convert';
import {toString} from '../../../src/style-spec/expression/types';
import {CanonicalTileID} from '../../../src/source/tile_id';
import MercatorCoordinate from '../../../src/geo/mercator_coordinate';
import Point from '@mapbox/point-geometry';
import {getGeometry} from './lib/geometry';
import {stringify, deepEqual, stripPrecision} from './lib/util';

const ignores = {};
const expressionTestFileNames = glob.sync('**/test.json', {cwd: __dirname});//, {cwd: __dirname});
birkskyum marked this conversation as resolved.
Show resolved Hide resolved
describe('expression', () => {

function getPoint(coord, canonical) {
const p: Point = canonical.getTilePoint(MercatorCoordinate.fromLngLat({lng: coord[0], lat: coord[1]}, 0));
p.x = Math.round(p.x);
p.y = Math.round(p.y);
return p;
}
expressionTestFileNames.forEach((expressionTestFileName: any) => {
test(expressionTestFileName, (done) => {

function convertPoint(coord, canonical, out) {
out.push([getPoint(coord, canonical)]);
}
const fixture = JSON.parse(fs.readFileSync(path.join(__dirname, expressionTestFileName), 'utf8'));

function convertPoints(coords, canonical, out) {
for (let i = 0; i < coords.length; i++) {
convertPoint(coords[i], canonical, out);
}
}
try {
const result = evaluateFixture(fixture);

function convertLine(line, canonical, out) {
const l = [];
for (let i = 0; i < line.length; i++) {
l.push(getPoint(line[i], canonical));
}
out.push(l);
}
if (process.env.UPDATE) {
fixture.expected = {
compiled: result.compiled,
outputs: stripPrecision(result.outputs),
serialized: result.serialized
};

function convertLines(lines, canonical, out) {
for (let i = 0; i < lines.length; i++) {
convertLine(lines[i], canonical, out);
}
}
delete fixture.metadata;

function getGeometry(feature, geometry, canonical) {
if (geometry.coordinates) {
const coords = geometry.coordinates;
const type = geometry.type;
feature.type = type;
feature.geometry = [];
if (type === 'Point') {
convertPoint(coords, canonical, feature.geometry);
} else if (type === 'MultiPoint') {
feature.type = 'Point';
convertPoints(coords, canonical, feature.geometry);
} else if (type === 'LineString') {
convertLine(coords, canonical, feature.geometry);
} else if (type === 'MultiLineString') {
feature.type = 'LineString';
convertLines(coords, canonical, feature.geometry);
} else if (type === 'Polygon') {
convertLines(coords, canonical, feature.geometry);
} else if (type === 'MultiPolygon') {
feature.type = 'Polygon';
for (let i = 0; i < coords.length; i++) {
const polygon = [];
convertLines(coords[i], canonical, polygon);
feature.geometry.push(polygon);
}
}
}
}
const dir = path.join(__dirname, expressionTestFileName);
fs.writeFile(path.join(dir, 'test.json'), `${stringify(fixture)}\n`, done);
return;
}

let tests;
const expected = fixture.expected;
const compileOk = deepEqual(result.compiled, expected.compiled);
const evalOk = compileOk && deepEqual(result.outputs, expected.outputs);

let recompileOk = true;
let roundTripOk = true;
let serializationOk = true;
if (expected.compiled.result !== 'error') {
serializationOk = compileOk && deepEqual(expected.serialized, result.serialized);
recompileOk = compileOk && deepEqual(result.recompiled, expected.compiled);
roundTripOk = recompileOk && deepEqual(result.roundTripOutputs, expected.outputs);
}

// @ts-ignore
const __filename = fileURLToPath(import.meta.url);
expect(compileOk).toBeTruthy();
expect(evalOk).toBeTruthy();
expect(recompileOk).toBeTruthy();
expect(roundTripOk).toBeTruthy();
expect(serializationOk).toBeTruthy();

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

run('js', {ignores, tests}, (fixture) => {
});
});

});

function evaluateFixture(fixture) {
HarelM marked this conversation as resolved.
Show resolved Hide resolved
const spec = Object.assign({}, fixture.propertySpec);
let availableImages;
let canonical;
Expand Down Expand Up @@ -169,6 +152,7 @@ run('js', {ignores, tests}, (fixture) => {
serialized?: any;
roundTripOutputs?: any;
} = {compiled: {}, recompiled: {}};

const expression = (() => {
if (isFunction(fixture.expression)) {
return createPropertyExpression(convertFunction(fixture.expression, spec), spec);
Expand All @@ -191,4 +175,4 @@ run('js', {ignores, tests}, (fixture) => {
}

return result;
});
}
200 changes: 0 additions & 200 deletions test/integration/expression/expression.ts

This file was deleted.

Loading