From a97ace50d79430096adc9ad42f8704dfd4cb6eb9 Mon Sep 17 00:00:00 2001 From: DanielJDufour Date: Tue, 7 May 2019 13:53:21 +0000 Subject: [PATCH 1/2] updated .gitignore to ignore testing data files and geotrans source code folder --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 7eed809..a988cd2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ node_modules .DS_STORE coverage .nyc_output +*.csv +*.tgz +geotrans3.7/ From 56401a8942b90d5b5b0a8914b9c5b4df44438700 Mon Sep 17 00:00:00 2001 From: DanielJDufour Date: Tue, 7 May 2019 14:00:31 +0000 Subject: [PATCH 2/2] added ability to test consistency with GEOTRANS --- package-lock.json | 6 ++++++ package.json | 3 ++- test/setup.js | 36 ++++++++++++++++++++++++++++++++++++ test/test.js | 21 +++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/setup.js diff --git a/package-lock.json b/package-lock.json index 313655d..39b891f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1131,6 +1131,12 @@ "object.getownpropertydescriptors": "^2.0.3" } }, + "node-fetch": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.5.0.tgz", + "integrity": "sha512-YuZKluhWGJwCcUu4RlZstdAxr8bFfOVHakc1mplwHkk8J+tqM1Y5yraYvIUpeX8aY7+crCwiELJq7Vl0o0LWXw==", + "dev": true + }, "npm-run-path": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", diff --git a/package.json b/package.json index 942c99e..cf22fae 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "module": "mgrs.js", "scripts": { "lint": "eslint mgrs.js && eslint test", - "test": "npm run build && nyc mocha", + "test": "npm run build && nyc mocha test/test.js", "build": "mkdir -p dist && rollup -c" }, "repository": { @@ -27,6 +27,7 @@ "chai": "^4.2.0", "eslint": "^5.16.0", "mocha": "^6.1.1", + "node-fetch": "^2.5.0", "nyc": "^13.3.0", "rollup": "^1.9.0" } diff --git a/test/setup.js b/test/setup.js new file mode 100644 index 0000000..2d0d9d7 --- /dev/null +++ b/test/setup.js @@ -0,0 +1,36 @@ +const { writeFileSync } = require('fs'); +const fetch = require('node-fetch'); + +async function process() { + // originally downloaded from + // http://earth-info.nga.mil/GandG/update/index.php?action=home + // and found in the unpacked downloaded GEOTRANS folder at + // geotrans3.7/SpreadsheetTester/TestFiles/outputs/output/mgrsToGeo_WE.txt + const url = 'https://s3.amazonaws.com/mgrs.io/mgrsToGeo_WE.txt'; + + let text = await fetch(url).then(response => response.text()); + + const [header, description, blank, ...rows] = text.split('\r\n'); + + let testCases = rows + .filter(testCase => { + return testCase.includes('Successful-Equivalent'); + }) + .map(row => { + const cells = row.replace(/\t+/g, '\t').split('\t'); + return { + latitude: cells[6].trim(), + longitude: cells[7].trim(), + mgrs: cells[5].trim() + }; + }) + + + console.log("testCases", testCases); + const csv = testCases.map(({mgrs, latitude, longitude}) => `${mgrs}\t${latitude}\t${longitude}`).join('\n'); + writeFileSync('testing-data.csv', csv, 'utf8'); + + console.log("wrote testing-data.csv"); +} + +process(); \ No newline at end of file diff --git a/test/test.js b/test/test.js index 596c781..38e7ee6 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,6 @@ const should = require('chai').should(); // eslint-disable-line no-unused-vars const mgrs = require('../dist/mgrs'); +const { readFileSync } = require('fs'); describe('First MGRS set', () => { const mgrsStr = '33UXP04'; @@ -111,3 +112,23 @@ describe ('data validation', () => { }); }); }); + +if (process.env.CHECK_GEOTRANS) { + describe('Consistency with GEOTRANS', () => { + it('Should be consistent with GEOTRANS', () => { + const fileText = readFileSync('./test/testing-data.csv', 'utf8'); + const lines = fileText.split('\n').filter(Boolean); + lines.forEach(line => { + const [ mgrsString, expectedLatitude, expectedLongitude ] = line.split('\t'); + const [ actualLongitude, actualLatitude ] = mgrs.toPoint(mgrsString); + try { + actualLatitude.should.equal(expectedLatitude); + actualLongitude.should.equal(expectedLongitude); + } catch (error) { + console.error('mgrsString:', mgrsString); + throw error; + } + }); + }); + }); +}