Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toBeAnyGeometry): add new matcher
Verifies an object meets validity requirements for one of the six basic GeoJSON geometry types: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon Resolves: #15
- Loading branch information
1 parent
b828396
commit ed7c3eb
Showing
16 changed files
with
478 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const { pointGeometry } = require('./pointGeometry') | ||
const { multiPointGeometry } = require('./multiPointGeometry') | ||
const { lineStringGeometry } = require('./lineStringGeometry') | ||
const { multiLineStringGeometry } = require('./multiLineStringGeometry') | ||
const { polygonGeometry } = require('./polygonGeometry') | ||
const { multiPolygonGeometry } = require('./multiPolygonGeometry') | ||
|
||
/** | ||
* Verifies an object meets validity requirements for one of the six basic GeoJSON geometry types: | ||
* Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon | ||
* | ||
* @memberof Core.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/15 | ||
* @param {object} geometryObject A WGS-84 array of [longitude, latitude] or [longitude, latitude, alititude] | ||
* @returns {boolean} True if a valid GeoJSON geometry object. If invalid, it will throw an error. | ||
* @throws {Error} Input must be either a valid Point, MultiPoint, LineString, MultiLineString, Polygon, or MultiPolygon | ||
* @example | ||
point = { | ||
"type": "Point", | ||
"coordinates": [100.0, 0.0] | ||
} | ||
lineString = { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
[180.0, 40.0], | ||
[180.0, 50.0], | ||
[170.0, 50.0], | ||
[170.0, 40.0], | ||
[180.0, 40.0] | ||
] | ||
] | ||
} | ||
polygon = { | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
] | ||
] | ||
} | ||
feature = { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [102.0, 0.5] | ||
} | ||
} | ||
console.log(anyGeometry(point)) // true | ||
console.log(anyGeometry(lineString)) // true | ||
console.log(anyGeometry(polygon)) // true | ||
console.log(anyGeometry(feature)) // throws error | ||
*/ | ||
function anyGeometry(geometryObject) { | ||
// if ( | ||
// typeof geometryObject !== 'object' || | ||
// Array.isArray(geometryObject) || | ||
// geometryObject === null | ||
// ) { | ||
// throw new Error('Argument must be a GeoJSON Geometry object.') | ||
// } | ||
|
||
if (geometryObject?.type === 'Point') { | ||
pointGeometry(geometryObject) | ||
return true | ||
} | ||
|
||
if (geometryObject?.type === 'MultiPoint') { | ||
multiPointGeometry(geometryObject) | ||
return true | ||
} | ||
|
||
if (geometryObject?.type === 'LineString') { | ||
lineStringGeometry(geometryObject) | ||
return true | ||
} | ||
|
||
if (geometryObject?.type === 'MultiLineString') { | ||
multiLineStringGeometry(geometryObject) | ||
return true | ||
} | ||
|
||
if (geometryObject?.type === 'Polygon') { | ||
polygonGeometry(geometryObject) | ||
return true | ||
} | ||
|
||
if (geometryObject?.type === 'MultiPolygon') { | ||
multiPolygonGeometry(geometryObject) | ||
return true | ||
} | ||
|
||
throw new Error( | ||
'Object must be either a valid Point, MultiPoint, LineString, MultiLineString, Polygon, or MultiPolygon' | ||
) | ||
} | ||
|
||
exports.anyGeometry = anyGeometry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const { anyGeometry } = require('../../core/geometries/anyGeometry') | ||
|
||
// eslint-disable-next-line jsdoc/require-returns | ||
/** | ||
* Verifies an object meets validity requirements for one of the six basic GeoJSON geometry types: | ||
* Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon | ||
* | ||
* @memberof Matchers.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/15 | ||
* @param {object} geometryObject any GeoJSON Geometry object | ||
* @example | ||
point = { | ||
"type": "Point", | ||
"coordinates": [100.0, 0.0] | ||
} | ||
lineString = { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
[180.0, 40.0], | ||
[180.0, 50.0], | ||
[170.0, 50.0], | ||
[170.0, 40.0], | ||
[180.0, 40.0] | ||
] | ||
] | ||
} | ||
polygon = { | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
] | ||
] | ||
} | ||
feature = { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [102.0, 0.5] | ||
} | ||
} | ||
expect(point).toBeAnyGeometry() | ||
expect(lineString).toBeAnyGeometry() | ||
expect(polygon).toBeAnyGeometry() | ||
expect(feature).not.toBeAnyGeometry() | ||
expect([322, -34.549, 0]).not.toBeAnyGeometry() | ||
expect({coordinates: [22, -34.549, 22]}).not.toBeAnyGeometry() | ||
*/ | ||
function toBeAnyGeometry(geometryObject) { | ||
const { printReceived, matcherHint } = this.utils | ||
const passMessage = | ||
// eslint-disable-next-line prefer-template | ||
matcherHint('.not.toBeAnyGeometry', 'GeometryObject', '') + | ||
'\n\n' + | ||
`Expected input to not be a valid GeoJSON geometry object.\n\n` + | ||
`Received: ${printReceived(geometryObject)}` | ||
|
||
/** | ||
* Combines a custom error message with built in Jest tools to provide a more descriptive error | ||
* meessage to the end user. | ||
* | ||
* @param {string} errorMessage Error message text to return to the user | ||
* @returns {string} Concatenated Jest test result string | ||
*/ | ||
function failMessage(errorMessage) { | ||
return ( | ||
// eslint-disable-next-line prefer-template, no-unused-expressions | ||
matcherHint('.toBeAnyGeometry', 'GeometryObject', '') + | ||
'\n\n' + | ||
`${errorMessage}\n\n` + | ||
`Received: ${printReceived(geometryObject)}` | ||
) | ||
} | ||
|
||
try { | ||
anyGeometry(geometryObject) | ||
} catch (err) { | ||
return { pass: false, message: () => failMessage(err.message) } | ||
} | ||
return { pass: true, message: () => passMessage } | ||
} | ||
|
||
exports.toBeAnyGeometry = toBeAnyGeometry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/geometries/__snapshots__/toBeAnyGeometry.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0, 0]).toBeAnyGeometry 1`] = ` | ||
"[2mexpect([22m[31mGeometryObject[39m[2m).toBeAnyGeometry()[22m | ||
Object must be either a valid Point, MultiPoint, LineString, MultiLineString, Polygon, or MultiPolygon | ||
Received: [31mfalse[39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect({type: 'Point', coordinates: [0, 0]}).not.toBeAnyGeometry 1`] = ` | ||
"[2mexpect([22m[31mGeometryObject[39m[2m).not.toBeAnyGeometry()[22m | ||
Expected input to not be a valid GeoJSON geometry object. | ||
Received: [31m{\\"coordinates\\": [0, 0], \\"type\\": \\"Point\\"}[39m" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// This matcher works on all the same geometry cases. | ||
// Accordingly, those specific tests check that this function also work. | ||
// This test suite also checks it fails with types GeometryCollection, Feature, and FeatureCollection. | ||
// Finally, it tests the unique snapshots. | ||
|
||
describe('Invalid Use Cases', () => { | ||
test('Expect to fail with GeometryCollection', () => { | ||
const geometryCollection = { | ||
type: 'GeometryCollection', | ||
geometries: [ | ||
{ | ||
type: 'Point', | ||
coordinates: [100.0, 0.0] | ||
}, | ||
{ | ||
type: 'LineString', | ||
coordinates: [ | ||
[101.0, 0.0], | ||
[102.0, 1.0] | ||
] | ||
} | ||
] | ||
} | ||
expect(geometryCollection).not.toBeAnyGeometry() | ||
}) | ||
|
||
test('Expect to fail with Feature', () => { | ||
const feature = { | ||
type: 'Feature', | ||
geometry: { | ||
type: 'Point', | ||
coordinates: [102.0, 0.5] | ||
} | ||
} | ||
expect(feature).not.toBeAnyGeometry() | ||
}) | ||
|
||
test('Expect to fail with FeatureCollection', () => { | ||
const featureCollection = { | ||
type: 'FeatureCollection', | ||
features: [ | ||
{ | ||
type: 'Feature', | ||
geometry: { | ||
type: 'Point', | ||
coordinates: [102.0, 0.5] | ||
}, | ||
properties: { | ||
prop0: 'value0' | ||
} | ||
}, | ||
{ | ||
type: 'Feature', | ||
geometry: { | ||
type: 'LineString', | ||
coordinates: [ | ||
[102.0, 0.0], | ||
[103.0, 1.0], | ||
[104.0, 0.0], | ||
[105.0, 1.0] | ||
] | ||
}, | ||
properties: { | ||
prop0: 'value0', | ||
prop1: 0.0 | ||
} | ||
}, | ||
{ | ||
type: 'Feature', | ||
geometry: { | ||
type: 'Polygon', | ||
coordinates: [ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
] | ||
] | ||
}, | ||
properties: { | ||
prop0: 'value0', | ||
prop1: { | ||
this: 'that' | ||
} | ||
} | ||
} | ||
] | ||
} | ||
expect(featureCollection).not.toBeAnyGeometry() | ||
}) | ||
}) | ||
|
||
describe('Error Snapshot Testing. Throws error:', () => { | ||
test(`expect({type: 'Point', coordinates: [0, 0]}).not.toBeAnyGeometry`, () => { | ||
expect(() => | ||
expect({ type: 'Point', coordinates: [0, 0] }).not.toBeAnyGeometry() | ||
).toThrowErrorMatchingSnapshot() | ||
}) | ||
test('expect([0, 0, 0]).toBeAnyGeometry', () => { | ||
expect(() => expect(false).toBeAnyGeometry()).toThrowErrorMatchingSnapshot() | ||
}) | ||
}) |
Oops, something went wrong.