Skip to content

Commit

Permalink
fix(toBeLineStringGeometry): prohibit single coordinate in "coordinat…
Browse files Browse the repository at this point in the history
…es" member

This is in the spec (https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.4) but I overlooked
it in the previous test designs. Updated the #11 issue to reflect.
  • Loading branch information
M-Scott-Lassiter committed May 28, 2022
1 parent 22df5c7 commit ee5de52
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/core/geometries/lineStringGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { validCoordinate } = require('../coordinates/validCoordinate')
/**
* Verifies an object is a valid GeoJSON LineString Geometry. This geometry requires a
* 'type' property that must equal "LineString", and a 'coordinates' property that contains
* an array of valid WGS-84 GeoJSON coordinate(s). The coordinates may be an empty array,
* an array of two or more valid WGS-84 GeoJSON coordinate(s). 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.
Expand Down Expand Up @@ -73,6 +73,9 @@ function lineStringGeometry(geometryObject) {
if (!Array.isArray(geometryObject.coordinates) && geometryObject.coordinates.length !== 1) {
throw new Error('Coordinates property must be an array of valid GeoJSON coordinates')
}
if (geometryObject.coordinates.length === 1) {
throw new Error('Coordinates array must contain two or more valid GeoJSON coordinates')
}
for (let i = 0; i < geometryObject.coordinates.length; i++) {
validCoordinate(geometryObject.coordinates[i])
}
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/geometries/toBeLineStringGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { lineStringGeometry } = require('../../core/geometries/lineStringGeometry
/**
* Verifies an object is a valid GeoJSON LineString Geometry. This geometry requires a
* 'type' property that must equal "LineString", and a 'coordinates' property that contains
* an array of valid WGS-84 GeoJSON coordinate(s). The coordinates may be an empty array,
* an array of two or more valid WGS-84 GeoJSON coordinate(s). 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'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Error Snapshot Testing. Throws error: expect({type: 'LineString', coordinates: [[0, 0]]}).not.toBeLineStringGeometry 1`] = `
exports[`Error Snapshot Testing. Throws error: expect({type: 'LineString', coordinates: [[0, 0], [1, 1]]}).not.toBeLineStringGeometry 1`] = `
"expect(GeometryObject).not.toBeLineStringGeometry()
Expected input to not be a valid GeoJSON LineString geometry.
Received: [31m{\\"coordinates\\": [[0, 0]], \\"type\\": \\"LineString\\"}[39m"
Received: [31m{\\"coordinates\\": [[0, 0], [1, 1]], \\"type\\": \\"LineString\\"}[39m"
`;

exports[`Error Snapshot Testing. Throws error: expect(false).toBeLineStringGeometry() 1`] = `
Expand Down
25 changes: 22 additions & 3 deletions tests/geometries/toBeLineStringGeometry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ describe('Valid Use Cases', () => {
type: 'LineString',
someRandomProp: true,
geometries: testLineString2,
coordinates: [[180, 10.2, -125]]
coordinates: [
[180, 10.2, -125],
[-180, -90]
]
}

test.each(['Test 1', 1])('ID: %p', (input) => {
Expand Down Expand Up @@ -246,6 +249,16 @@ describe('Inalid Use Cases', () => {
})
})

describe('Expect to fail with only a single coordinate:', () => {
test('coordinates: [[0, 0]]', () => {
const testLineString = {
type: 'LineString',
coordinates: [[0, 0]]
}
expect(testLineString).not.toBeLineStringGeometry()
})
})

describe('Expect to fail with coordinates array of empty arrays:', () => {
test.each([...emptyArrays])('coordinates: %p', (coordinate) => {
const testLineString = {
Expand Down Expand Up @@ -345,9 +358,15 @@ describe('Inalid Use Cases', () => {
})

describe('Error Snapshot Testing. Throws error:', () => {
test(`expect({type: 'LineString', coordinates: [[0, 0]]}).not.toBeLineStringGeometry`, () => {
test(`expect({type: 'LineString', coordinates: [[0, 0], [1, 1]]}).not.toBeLineStringGeometry`, () => {
expect(() =>
expect({ type: 'LineString', coordinates: [[0, 0]] }).not.toBeLineStringGeometry()
expect({
type: 'LineString',
coordinates: [
[0, 0],
[1, 1]
]
}).not.toBeLineStringGeometry()
).toThrowErrorMatchingSnapshot()
})
test('expect(false).toBeLineStringGeometry()', () => {
Expand Down

0 comments on commit ee5de52

Please sign in to comment.