Skip to content

Commit

Permalink
add tests to @turf/destination (#682)
Browse files Browse the repository at this point in the history
* add tests to show destination results #681

* passed units to distance() in test.js
  • Loading branch information
stebogit authored and DenisCarriere committed Apr 21, 2017
1 parent 3f22f62 commit 9305807
Show file tree
Hide file tree
Showing 11 changed files with 1,836 additions and 18 deletions.
2 changes: 2 additions & 0 deletions packages/turf-destination/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/distance": "^4.1.0",
"@turf/truncate": "^4.1.0",
"@turf/great-circle": "^4.1.0",
"benchmark": "^1.0.0",
"tape": "^3.5.0"
},
Expand Down
31 changes: 24 additions & 7 deletions packages/turf-destination/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,46 @@ const load = require('load-json-file');
const fs = require('fs');
const path = require('path');
const test = require('tape');
const distance = require('@turf/distance');
const truncate = require('@turf/truncate');
const {featureCollection} = require('@turf/helpers');
const greatCircle = require('@turf/great-circle');
const destination = require('./');

const directories = {
in: path.join(__dirname, 'test', 'in') + path.sep,
out: path.join(__dirname, 'test', 'out') + path.sep
};

const round = (num, decimals) => {
const factor = Math.pow(10, decimals);
return Math.round(num * factor) / factor;
}

const fixtures = fs.readdirSync(directories.in).map(filename => {
return {
filename,
name: path.parse(filename).name,
geojson: load.sync(directories.in + filename)
inputPoint: load.sync(directories.in + filename)
};
});

test('turf-destination', t => {
for (const {filename, name, geojson} of fixtures) {
const dist = 100;
const bear = 180;
const results = destination(geojson, dist, bear, 'kilometers');
for (const {filename, name, inputPoint} of fixtures) {
let {bearing, dist, units} = inputPoint.properties || {};
bearing = (bearing !== undefined) ? bearing : 180;
dist = (dist !== undefined) ? dist : 100;
units = units || 'kilometers';

const resultPoint = destination(inputPoint, dist, bearing, units);
const line = truncate(greatCircle(inputPoint, resultPoint, {"stroke": "#F00", "stroke-width": 4}));
inputPoint.properties['marker-color'] = '#F00';
const result = featureCollection([line, inputPoint, resultPoint]);
const d = distance(inputPoint, resultPoint, units);

if (process.env.REGEN) write.sync(directories.out + filename, results);
t.deepEqual(results, load.sync(directories.out + filename), name);
if (process.env.REGEN) write.sync(directories.out + filename, result);
t.deepEqual(result, load.sync(directories.out + filename), name);
t.equals(dist, round(d, 9), 'distance');
}
t.end();
});
13 changes: 13 additions & 0 deletions packages/turf-destination/test/in/point-0.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "Feature",
"properties": {
"bearing": 0
},
"geometry": {
"type": "Point",
"coordinates": [
-75,
38.10096062273525
]
}
}
13 changes: 13 additions & 0 deletions packages/turf-destination/test/in/point-90.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "Feature",
"properties": {
"bearing": 90
},
"geometry": {
"type": "Point",
"coordinates": [
-75,
39
]
}
}
15 changes: 15 additions & 0 deletions packages/turf-destination/test/in/point-way-far-away.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Feature",
"properties": {
"bearing": 90,
"dist": 5000,
"units": "miles"
},
"geometry": {
"type": "Point",
"coordinates": [
-75,
39
]
}
}
Loading

0 comments on commit 9305807

Please sign in to comment.