-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toBeGeometryCollection): add new matcher
Verifies an object is a valid GeoJSON GeometryCollection. Also updates the `anyGeometry` core function and matcher to allow GeometryCollection objects. Resolves: #16
- Loading branch information
1 parent
79ef8c9
commit 63cc919
Showing
17 changed files
with
900 additions
and
30 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
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,105 @@ | ||
const { anyGeometry } = require('./anyGeometry') | ||
|
||
/** | ||
* Verifies an object is a valid GeoJSON GeometryCollection. This object requires a | ||
* 'type' property that must equal "GeometryCollection", and a 'geometries' property that contains | ||
* an array of GeoJSON Geometry objects (Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon) | ||
* | ||
* The geometries may be an empty array, but may not be an array of empty arrays or objects. | ||
* | ||
* Foreign members are allowed with the exceptions thrown below. | ||
* | ||
* @memberof Core.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/16 | ||
* @param {object} geometryObject a GeoJSON Geometry object | ||
* @returns {boolean} True if a valid GeoJSON GeometryCollection Geometry. If invalid, it will throw an error. | ||
* @throws {Error} Argument not an object | ||
* @throws {Error} Must have a type property with value 'GeometryCollection' | ||
* @throws {Error} Forbidden from having a property 'geometry', 'properties', or 'features' | ||
* @example | ||
* const collection = { | ||
* type: 'GeometryCollection', | ||
* geometries: [{ | ||
* "type": 'Point', | ||
* "coordinates": [100.0, 0.0] | ||
* }, { | ||
* type: 'LineString', | ||
* coordinates: [ | ||
* [101.0, 0.0], | ||
* [102.0, 1.0] | ||
* ] | ||
* }, { | ||
* type: 'Polygon', | ||
* coordinates: [ | ||
* [ | ||
* [102.0, 2.0], | ||
* [103.0, 2.0], | ||
* [103.0, 3.0], | ||
* [102.0, 3.0], | ||
* [102.0, 2.0] | ||
* ] | ||
* ] | ||
* }, { | ||
* type: 'Point', | ||
* coordinates: [150.0, 73.0] | ||
* }] | ||
* } | ||
* | ||
* const goodExample = geometryCollection(collection) // true | ||
* | ||
* const badExample1 = geometryCollection(collection.geometries) // throws error | ||
* const badExample2 = geometryCollection(collection.geometries[1]) // throws error | ||
*/ | ||
function geometryCollection(geometryObject) { | ||
if ( | ||
typeof geometryObject !== 'object' || | ||
Array.isArray(geometryObject) || | ||
geometryObject === null | ||
) { | ||
throw new Error('Argument must be a GeoJSON GeometryCollection object.') | ||
} | ||
|
||
if (!('geometries' in geometryObject)) { | ||
throw new Error( | ||
`GeoJSON GeometryCollection must contain a 'geometries' with an array of GeoJSON geometries.` | ||
) | ||
} | ||
|
||
if (geometryObject.type !== 'GeometryCollection') { | ||
throw new Error(`Must have a type property with value 'GeometryCollection'.`) | ||
} | ||
|
||
if ('geometry' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON GeometryCollection objects are forbidden from having a property 'geometry'.` | ||
) | ||
} | ||
|
||
if ('properties' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON GeometryCollection objects are forbidden from having a property 'properties'.` | ||
) | ||
} | ||
|
||
if ('features' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON GeometryCollection objects are forbidden from having a property 'features'.` | ||
) | ||
} | ||
|
||
if (!Array.isArray(geometryObject.geometries)) { | ||
throw new Error('Geometries property must be an array of valid GeoJSON geometry objects.') | ||
} | ||
|
||
geometryObject.geometries.forEach((geometry) => { | ||
if (geometry?.type === 'GeometryCollection') { | ||
geometryCollection(geometry) | ||
} else { | ||
anyGeometry(geometry) | ||
} | ||
}) | ||
|
||
return true | ||
} | ||
|
||
exports.geometryCollection = geometryCollection |
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
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,93 @@ | ||
const { geometryCollection } = require('../../core/geometries/geometryCollection') | ||
|
||
// eslint-disable-next-line jsdoc/require-returns | ||
/** | ||
* Verifies an object is a valid GeoJSON GeometryCollection. This object requires a | ||
* 'type' property that must equal "GeometryCollection", and a 'geometries' property that contains | ||
* an array of GeoJSON Geometry objects (Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon) | ||
* | ||
* The geometries may be an empty array, but may not be an array of empty arrays or objects. | ||
* | ||
* @memberof Matchers.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/16 | ||
* @param {object} geometryObject any GeoJSON Geometry object | ||
* @example | ||
* const collection = { | ||
* type: 'GeometryCollection', | ||
* geometries: [{ | ||
* "type": 'Point', | ||
* "coordinates": [100.0, 0.0] | ||
* }, { | ||
* type: 'LineString', | ||
* coordinates: [ | ||
* [101.0, 0.0], | ||
* [102.0, 1.0] | ||
* ] | ||
* }, { | ||
* type: 'Polygon', | ||
* coordinates: [ | ||
* [ | ||
* [102.0, 2.0], | ||
* [103.0, 2.0], | ||
* [103.0, 3.0], | ||
* [102.0, 3.0], | ||
* [102.0, 2.0] | ||
* ] | ||
* ] | ||
* }, { | ||
* type: 'Point', | ||
* coordinates: [150.0, 73.0] | ||
* }] | ||
* } | ||
* | ||
* test('Object is valid GeoJSON Geometry Object', () => { | ||
* expect(collection).toBeGeometryCollection() | ||
* }) | ||
* @example | ||
* const lineString = { | ||
* type: 'LineString', | ||
* coordinates: [ | ||
* [101.0, 0.0], | ||
* [102.0, 1.0] | ||
* ] | ||
* } | ||
* test('Object is NOT valid GeoJSON Geometry Object', () => { | ||
* expect(collection.geometries).not.toBeGeometryCollection() | ||
* expect(lineString).not.toBeGeometryCollection() | ||
* }) | ||
*/ | ||
function toBeGeometryCollection(geometryObject) { | ||
const { printReceived, matcherHint } = this.utils | ||
const passMessage = | ||
// eslint-disable-next-line prefer-template | ||
matcherHint('.not.toBeGeometryCollection', 'GeometryObject', '') + | ||
'\n\n' + | ||
`Expected input to not be a valid GeoJSON GeometryCollection 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('.toBeGeometryCollection', 'GeometryObject', '') + | ||
'\n\n' + | ||
`${errorMessage}\n\n` + | ||
`Received: ${printReceived(geometryObject)}` | ||
) | ||
} | ||
|
||
try { | ||
geometryCollection(geometryObject) | ||
} catch (err) { | ||
return { pass: false, message: () => failMessage(err.message) } | ||
} | ||
return { pass: true, message: () => passMessage } | ||
} | ||
|
||
exports.toBeGeometryCollection = toBeGeometryCollection |
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
16 changes: 8 additions & 8 deletions
16
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 |
---|---|---|
@@ -1,17 +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" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect(false).toBeAnyGeometry 1`] = ` | ||
"[2mexpect([22m[31mGeometryObject[39m[2m).toBeAnyGeometry()[22m | ||
Cannot use 'in' operator to search for 'geometry' in false | ||
Received: [31mfalse[39m" | ||
`; |
Oops, something went wrong.