Skip to content

Commit

Permalink
Tests are done
Browse files Browse the repository at this point in the history
  • Loading branch information
w8r committed Jul 18, 2022
1 parent 1d150a0 commit b1cd7db
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 85 deletions.
85 changes: 0 additions & 85 deletions test/genericTestCases.test.js

This file was deleted.

85 changes: 85 additions & 0 deletions test/genericTestCases.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, it, assert } from 'vitest';
import path from 'path';
import glob from 'glob';
import load from 'load-json-file';
import fs from 'fs';
import stringify from 'json-stringify-pretty-compact';
import * as martinez from '../src';

function extractExpectedResults(features) {
return features.map((feature) => {
let mode = feature.properties.operation;
var op;
switch (mode) {
case 'union':
op = martinez.union;
break;
case 'intersection':
op = martinez.intersection;
break;
case 'xor':
op = martinez.xor;
break;
case 'diff':
op = martinez.diff;
break;
case 'diff_ba':
op = (a, b) => martinez.diff(b, a);
break;
}
if (op == null) {
throw `Invalid mode: ${mode}`;
}
return {
op: op,
coordinates: feature.geometry.coordinates
};
});
}

const caseDir = path.join(__dirname, 'genericTestCases');
const testCases = glob.sync(path.join(caseDir, '*.geojson'));
if (testCases.length === 0) {
throw 'No test cases found, this must not happen';
}

describe('Generic test cases', () => {
testCases.forEach((testCaseFile) => {
let testName = 'Generic test case: ' + path.basename(testCaseFile);
it(testName, () => {
const data = load.sync(testCaseFile);
if (data.features.length < 2) {
throw `Test case file must contain at least two features, but ${testCaseFile} doesn't.`;
}

let p1Geometry = data.features[0].geometry;
let p2Geometry = data.features[1].geometry;

let p1 =
p1Geometry.type === 'Polygon'
? [p1Geometry.coordinates]
: p1Geometry.coordinates;
let p2 =
p2Geometry.type === 'Polygon'
? [p2Geometry.coordinates]
: p2Geometry.coordinates;

let expectedResults = extractExpectedResults(data.features.slice(2));

let featureIndex = 2;
for (const expectedResult of expectedResults) {
const result = expectedResult.op(p1, p2);
assert.deepEqual(result, expectedResult.coordinates);

// Update output data for re-generation mode
data.features[featureIndex].geometry.type = 'MultiPolygon';
data.features[featureIndex].geometry.coordinates = result;
featureIndex += 1;
}

if (process.env.REGEN) {
fs.writeFileSync(testCaseFile, stringify(data));
}
});
});
});

0 comments on commit b1cd7db

Please sign in to comment.