-
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(toBeMultiLineStringGeometry): add new matcher
Verifies an object is a valid GeoJSON MultiLineString Geometry. Fixes some API typos in lineStringGeometry and multiPointGeometry. Resolves: #12
- Loading branch information
1 parent
ee5de52
commit 3d3a15e
Showing
14 changed files
with
630 additions
and
9 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,104 @@ | ||
const { validCoordinate } = require('../coordinates/validCoordinate') | ||
|
||
/** | ||
* Verifies an object is a valid GeoJSON MultiLineString Geometry. This geometry requires a | ||
* 'type' property that must equal "MultiLineString", and a 'coordinates' property that contains | ||
* an array of linestring arrays (i.e. each linestring array containing at least two or more valid | ||
* WGS-84 GeoJSON coordinates). | ||
* | ||
* The coordinates may be an empty array, but may not be an array of empty arrays. | ||
* | ||
* Foreign members are allowed with the exceptions thrown below. | ||
* | ||
* @memberof Core.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/12 | ||
* @param {object} geometryObject a GeoJSON MultiLineString Geometry object | ||
* @returns {boolean} True if a valid GeoJSON MultiLineString Geometry. If invalid, it will throw an error. | ||
* @throws {Error} Argument not an object | ||
* @throws {Error} Must have a type property with value 'MultiLineString' | ||
* @throws {Error} forbidden from having a property 'geometry', 'properties', or 'features' | ||
* @example | ||
* const multiLineString = { | ||
"type": "MultiLineString", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 1.0] | ||
], | ||
[ | ||
[102.0, 2.0], | ||
[103.0, 3.0] | ||
] | ||
] | ||
} | ||
const multiLineStringOneCoordinate = { | ||
"type": "MultiLineString", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0] | ||
] | ||
] | ||
} | ||
const point = { | ||
type: "Point", | ||
coordinates: [100.0, 0.0] | ||
} | ||
console.log(multiLineStringGeometry(multiLineString)) // true | ||
console.log(multiLineStringGeometry(point)) // throws error | ||
console.log(multiLineStringGeometry(multiLineStringOneCoordinate)) // throws error | ||
*/ | ||
function multiLineStringGeometry(geometryObject) { | ||
if ( | ||
typeof geometryObject !== 'object' || | ||
Array.isArray(geometryObject) || | ||
geometryObject === null | ||
) { | ||
throw new Error('Argument must be a GeoJSON MultiLineString Geometry object.') | ||
} | ||
|
||
if (!('coordinates' in geometryObject)) { | ||
throw new Error(`GeoJSON MultiLineString Geometry must contain a 'coordinates' property.`) | ||
} | ||
|
||
if (geometryObject.type !== 'MultiLineString') { | ||
throw new Error(`Must have a type property with value 'MultiLineString'`) | ||
} | ||
|
||
if ('geometry' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON MultiLineString Geometry objects are forbidden from having a property 'geometry'.` | ||
) | ||
} | ||
|
||
if ('properties' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON MultiLineString Geometry objects are forbidden from having a property 'properties'.` | ||
) | ||
} | ||
|
||
if ('features' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON MultiLineString Geometry objects are forbidden from having a property 'features'.` | ||
) | ||
} | ||
|
||
// // // Geometry objects are allowed to have empty arrays as coordinates, however validCoordinate may not. | ||
// // If coordinates is an empty array, we're done. Otherwise, check for coordinate validity. | ||
// if (!Array.isArray(geometryObject.coordinates) && geometryObject.coordinates.length !== 1) { | ||
// throw new Error('Coordinates property must be an array of valid GeoJSON coordinates') | ||
// } | ||
|
||
for (let i = 0; i < geometryObject.coordinates.length; i++) { | ||
if (geometryObject.coordinates[i].length === 1) { | ||
throw new Error('Coordinates array must contain two or more valid GeoJSON coordinates') | ||
} | ||
for (let j = 0; j < geometryObject.coordinates[i].length; j++) { | ||
validCoordinate(geometryObject.coordinates[i][j]) | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.multiLineStringGeometry = multiLineStringGeometry |
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,83 @@ | ||
const { multiLineStringGeometry } = require('../../core/geometries/multiLineStringGeometry') | ||
|
||
// eslint-disable-next-line jsdoc/require-returns | ||
/** | ||
* Verifies an object is a valid GeoJSON MultiLineString Geometry. This geometry requires a | ||
* 'type' property that must equal "MultiLineString", and a 'coordinates' property that contains | ||
* an array of linestring arrays (i.e. each linestring array containing at least two or more valid | ||
* WGS-84 GeoJSON coordinates). | ||
* | ||
* The coordinates may be an empty array, but may not be an array of empty arrays. | ||
* | ||
* Foreign members are allowed with the exception of 'geometry', 'properties', or 'features'. | ||
* | ||
* @memberof Matchers.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/12 | ||
* @param {object} geometryObject a GeoJSON MultiLineString Geometry object | ||
* @example | ||
const multiLineString = { | ||
"type": "MultiLineString", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 1.0] | ||
], | ||
[ | ||
[102.0, 2.0], | ||
[103.0, 3.0] | ||
] | ||
] | ||
} | ||
const multiLineStringOneCoordinate = { | ||
"type": "MultiLineString", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0] | ||
] | ||
] | ||
} | ||
const point = { | ||
type: "Point", | ||
coordinates: [100.0, 0.0] | ||
} | ||
expect(multiLineString).toBeMultiLineStringGeometry() | ||
expect(point).not.toBeMultiLineStringGeometry() | ||
expect(multiLineStringOneCoordinate).not.toBeMultiLineStringGeometry() | ||
*/ | ||
function toBeMultiLineStringGeometry(geometryObject) { | ||
const { printReceived, matcherHint } = this.utils | ||
const passMessage = | ||
// eslint-disable-next-line prefer-template | ||
matcherHint('.not.toBeMultiLineStringGeometry', 'GeometryObject', '') + | ||
'\n\n' + | ||
`Expected input to not be a valid GeoJSON MultiLineString geometry.\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('.toBeMultiLineStringGeometry', 'GeometryObject', '') + | ||
'\n\n' + | ||
`${errorMessage}\n\n` + | ||
`Received: ${printReceived(geometryObject)}` | ||
) | ||
} | ||
|
||
try { | ||
multiLineStringGeometry(geometryObject) | ||
} catch (err) { | ||
return { pass: false, message: () => failMessage(err.message) } | ||
} | ||
return { pass: true, message: () => passMessage } | ||
} | ||
|
||
exports.toBeMultiLineStringGeometry = toBeMultiLineStringGeometry |
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__/toBeMultiLineStringGeometry.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({type: 'MultiLineString', coordinates: [[[0, 0], [1, 1]]]}).not.toBeMultiLineStringGeometry 1`] = ` | ||
"[2mexpect([22m[31mGeometryObject[39m[2m).not.toBeMultiLineStringGeometry()[22m | ||
Expected input to not be a valid GeoJSON MultiLineString geometry. | ||
Received: [31m{\\"coordinates\\": [[[0, 0], [1, 1]]], \\"type\\": \\"MultiLineString\\"}[39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect(false).toBeMultiLineStringGeometry() 1`] = ` | ||
"[2mexpect([22m[31mGeometryObject[39m[2m).toBeMultiLineStringGeometry()[22m | ||
Argument must be a GeoJSON MultiLineString Geometry object. | ||
Received: [31mfalse[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
Oops, something went wrong.