From 4cac3c97e5c293e8991f02b7109ea89409fc470f Mon Sep 17 00:00:00 2001 From: Reinert Lemmens Date: Wed, 2 Oct 2024 23:05:18 +0200 Subject: [PATCH] Update all examples and tests with dimensionality --- examples/feature_collection.ts | 24 +++++++-- examples/geometry/geometry_collection.ts | 14 ++--- examples/geometry/line_string.ts | 10 ++-- examples/geometry/multi_line_string.ts | 14 ++--- examples/geometry/multi_point.ts | 10 ++-- examples/geometry/multi_polygon.ts | 12 ++--- examples/geometry/point.ts | 10 ++-- examples/geometry/polygon.ts | 12 ++--- tests/feature.test.ts | 20 ++++++- tests/feature_collection.test.ts | 61 ++++++++++++++++------ tests/geojson.test.ts | 24 +++++++-- tests/geometry/geometry_collection.test.ts | 31 ++++++++++- tests/geometry/line_string.test.ts | 23 +++++++- tests/geometry/multi_line_string.test.ts | 31 ++++++++++- tests/geometry/multi_point.test.ts | 23 +++++++- tests/geometry/multi_polygon.test.ts | 23 +++++++- tests/geometry/point.test.ts | 23 +++++++- tests/geometry/polygon.test.ts | 23 +++++++- tests/position.test.ts | 26 +++++++-- 19 files changed, 330 insertions(+), 84 deletions(-) diff --git a/examples/feature_collection.ts b/examples/feature_collection.ts index b6ac493..9f211df 100644 --- a/examples/feature_collection.ts +++ b/examples/feature_collection.ts @@ -1,6 +1,6 @@ -import { GeoJSONFeatureCollection } from "../src"; +import { GeoJSON2DFeatureCollection, GeoJSON3DFeatureCollection, GeoJSONFeatureCollection } from "../src"; -export const singleGeoJsonFeatureCollection: GeoJSONFeatureCollection = { +export const singleGeoJsonFeatureCollection2D: GeoJSON2DFeatureCollection = { type: "FeatureCollection", features: [ { @@ -14,7 +14,21 @@ export const singleGeoJsonFeatureCollection: GeoJSONFeatureCollection = { ], }; -export const multiGeoJsonFeatureCollection: GeoJSONFeatureCollection = { +export const singleGeoJsonFeatureCollection3D: GeoJSON3DFeatureCollection = { + type: "FeatureCollection", + features: [ + { + type: "Feature", + properties: {}, + geometry: { + type: "Point", + coordinates: [0.0, 0.0, 1.0], + }, + }, + ], +}; + +export const multiGeoJsonFeatureCollection2D: GeoJSON2DFeatureCollection = { type: "FeatureCollection", features: [ { @@ -39,7 +53,7 @@ export const multiGeoJsonFeatureCollection: GeoJSONFeatureCollection = { ], }; -export const multiGeoJsonFeatureCollectionWithBbox: GeoJSONFeatureCollection = { - ...multiGeoJsonFeatureCollection, +export const multiGeoJsonFeatureCollectionWithBbox2D: GeoJSONFeatureCollection = { + ...multiGeoJsonFeatureCollection2D, bbox: [0.0, 0.0, 10.0, 10.0], }; diff --git a/examples/geometry/geometry_collection.ts b/examples/geometry/geometry_collection.ts index 729a580..e0d57b5 100644 --- a/examples/geometry/geometry_collection.ts +++ b/examples/geometry/geometry_collection.ts @@ -1,4 +1,4 @@ -import { GeoJSONGeometryCollection } from "../../src"; +import { GeoJSON2DGeometryCollection, GeoJSON3DGeometryCollection } from "../../src"; import { geoJsonLineString3D } from "./line_string"; import { multiGeoJsonMultiLineString2D } from "./multi_line_string"; import { geoJsonMultiPoint2D } from "./multi_point"; @@ -6,32 +6,32 @@ import { singleGeoJsonMultiPolygon3D } from "./multi_polygon"; import { geoJsonPoint2D, geoJsonPoint2DWithBbox, geoJsonPoint3D } from "./point"; import { geoJsonPolygon2D } from "./polygon"; -export const singleGeoJsonGeometryCollection2D: GeoJSONGeometryCollection = { +export const singleGeoJsonGeometryCollection2D: GeoJSON2DGeometryCollection = { type: "GeometryCollection", geometries: [geoJsonPoint2D], }; -export const multiGeoJsonGeometryCollection2D: GeoJSONGeometryCollection = { +export const multiGeoJsonGeometryCollection2D: GeoJSON2DGeometryCollection = { type: "GeometryCollection", geometries: [geoJsonPoint2D, geoJsonMultiPoint2D, geoJsonPolygon2D, multiGeoJsonMultiLineString2D], }; -export const multiGeoJsonGeometryCollection3D: GeoJSONGeometryCollection = { +export const multiGeoJsonGeometryCollection3D: GeoJSON3DGeometryCollection = { type: "GeometryCollection", geometries: [geoJsonPoint3D, geoJsonLineString3D, singleGeoJsonMultiPolygon3D], }; -export const singleGeoJsonGeometryCollection2DWithBbox: GeoJSONGeometryCollection = { +export const singleGeoJsonGeometryCollection2DWithBbox: GeoJSON2DGeometryCollection = { ...singleGeoJsonGeometryCollection2D, bbox: geoJsonPoint2DWithBbox.bbox, }; -export const multiGeoJsonGeometryCollection2DWithBbox: GeoJSONGeometryCollection = { +export const multiGeoJsonGeometryCollection2DWithBbox: GeoJSON2DGeometryCollection = { ...multiGeoJsonGeometryCollection2D, bbox: [-3.0, -2.0, 30.0, 30.0], }; -export const multiGeoJsonGeometryCollection3DWithBbox: GeoJSONGeometryCollection = { +export const multiGeoJsonGeometryCollection3DWithBbox: GeoJSON3DGeometryCollection = { ...multiGeoJsonGeometryCollection3D, bbox: [0.0, 0.0, 0.0, 20.0, 10.0, 10.0], }; diff --git a/examples/geometry/line_string.ts b/examples/geometry/line_string.ts index 08746e3..c01a0b6 100644 --- a/examples/geometry/line_string.ts +++ b/examples/geometry/line_string.ts @@ -1,6 +1,6 @@ -import { GeoJSONLineString } from "../../src"; +import { GeoJSON2DLineString, GeoJSON3DLineString } from "../../src"; -export const geoJsonLineString2D: GeoJSONLineString = { +export const geoJsonLineString2D: GeoJSON2DLineString = { type: "LineString", coordinates: [ [1.0, 2.0], @@ -8,7 +8,7 @@ export const geoJsonLineString2D: GeoJSONLineString = { ], }; -export const geoJsonLineString3D: GeoJSONLineString = { +export const geoJsonLineString3D: GeoJSON3DLineString = { type: "LineString", coordinates: [ [0.0, 0.0, 0.0], @@ -17,12 +17,12 @@ export const geoJsonLineString3D: GeoJSONLineString = { ], }; -export const geoJsonLineString2DWithBbox: GeoJSONLineString = { +export const geoJsonLineString2DWithBbox: GeoJSON2DLineString = { ...geoJsonLineString2D, bbox: [1.0, 2.0, 3.0, 4.0], }; -export const geoJsonLineString3DWithBbox: GeoJSONLineString = { +export const geoJsonLineString3DWithBbox: GeoJSON3DLineString = { ...geoJsonLineString3D, bbox: [0.0, 0.0, 0.0, 20.0, 10.0, 2.0], }; diff --git a/examples/geometry/multi_line_string.ts b/examples/geometry/multi_line_string.ts index 5a4cbbe..f04dba7 100644 --- a/examples/geometry/multi_line_string.ts +++ b/examples/geometry/multi_line_string.ts @@ -1,4 +1,4 @@ -import { GeoJSONMultiLineString } from "../../src"; +import { GeoJSON2DMultiLineString, GeoJSON3DMultiLineString } from "../../src"; import { geoJsonLineString2D, geoJsonLineString2DWithBbox, @@ -6,11 +6,11 @@ import { geoJsonLineString3DWithBbox, } from "./line_string"; -export const singleGeoJsonMultiLineString2D: GeoJSONMultiLineString = { +export const singleGeoJsonMultiLineString2D: GeoJSON2DMultiLineString = { type: "MultiLineString", coordinates: [geoJsonLineString2D.coordinates], }; -export const multiGeoJsonMultiLineString2D: GeoJSONMultiLineString = { +export const multiGeoJsonMultiLineString2D: GeoJSON2DMultiLineString = { type: "MultiLineString", coordinates: [ geoJsonLineString2D.coordinates, @@ -20,22 +20,22 @@ export const multiGeoJsonMultiLineString2D: GeoJSONMultiLineString = { ], ], }; -export const singleGeoJsonMultiLineString3D: GeoJSONMultiLineString = { +export const singleGeoJsonMultiLineString3D: GeoJSON3DMultiLineString = { type: "MultiLineString", coordinates: [geoJsonLineString3D.coordinates], }; -export const singleGeoJsonMultiLineString2DWithBbox: GeoJSONMultiLineString = { +export const singleGeoJsonMultiLineString2DWithBbox: GeoJSON2DMultiLineString = { ...singleGeoJsonMultiLineString2D, bbox: geoJsonLineString2DWithBbox.bbox, }; -export const multiGeoJsonMultiLineString2DWithBbox: GeoJSONMultiLineString = { +export const multiGeoJsonMultiLineString2DWithBbox: GeoJSON2DMultiLineString = { ...multiGeoJsonMultiLineString2D, bbox: [1.0, 2.0, 30.0, 30.0], }; -export const singleGeoJsonMultiLineString3DWithBbox: GeoJSONMultiLineString = { +export const singleGeoJsonMultiLineString3DWithBbox: GeoJSON3DMultiLineString = { ...singleGeoJsonMultiLineString3D, bbox: geoJsonLineString3DWithBbox.bbox, }; diff --git a/examples/geometry/multi_point.ts b/examples/geometry/multi_point.ts index eda72a9..07a2a24 100644 --- a/examples/geometry/multi_point.ts +++ b/examples/geometry/multi_point.ts @@ -1,6 +1,6 @@ -import { GeoJSONMultiPoint } from "../../src"; +import { GeoJSON2DMultiPoint, GeoJSON3DMultiPoint } from "../../src"; -export const geoJsonMultiPoint2D: GeoJSONMultiPoint = { +export const geoJsonMultiPoint2D: GeoJSON2DMultiPoint = { type: "MultiPoint", coordinates: [ [0.0, 0.0], @@ -9,7 +9,7 @@ export const geoJsonMultiPoint2D: GeoJSONMultiPoint = { ], }; -export const geoJsonMultiPoint3D: GeoJSONMultiPoint = { +export const geoJsonMultiPoint3D: GeoJSON3DMultiPoint = { type: "MultiPoint", coordinates: [ [0.0, 0.0, 0.0], @@ -18,12 +18,12 @@ export const geoJsonMultiPoint3D: GeoJSONMultiPoint = { ], }; -export const geoJsonMultiPoint2DWithBbox: GeoJSONMultiPoint = { +export const geoJsonMultiPoint2DWithBbox: GeoJSON2DMultiPoint = { ...geoJsonMultiPoint2D, bbox: [-3.0, -2.0, 8.0, 4.0], }; -export const geoJsonMultiPoint3DWithBbox: GeoJSONMultiPoint = { +export const geoJsonMultiPoint3DWithBbox: GeoJSON3DMultiPoint = { ...geoJsonMultiPoint3D, bbox: [-3.0, -2.0, 0.0, 8.0, 4.0, 5.0], }; diff --git a/examples/geometry/multi_polygon.ts b/examples/geometry/multi_polygon.ts index 0ac4927..ea482a5 100644 --- a/examples/geometry/multi_polygon.ts +++ b/examples/geometry/multi_polygon.ts @@ -1,4 +1,4 @@ -import { GeoJSON2DMultiPolygon, GeoJSONMultiPolygon } from "../../src"; +import { GeoJSON2DMultiPolygon, GeoJSON3DMultiPolygon } from "../../src"; import { geoJsonPolygon2D, geoJsonPolygon2DWithBbox, @@ -13,27 +13,27 @@ export const singleGeoJsonMultiPolygon2D: GeoJSON2DMultiPolygon = { coordinates: [geoJsonPolygon2D.coordinates], }; -export const multiGeoJsonMultiPolygon2D: GeoJSONMultiPolygon = { +export const multiGeoJsonMultiPolygon2D: GeoJSON2DMultiPolygon = { type: "MultiPolygon", coordinates: [geoJsonPolygon2D.coordinates, geoJsonPolygon2DWithHole.coordinates], }; -export const singleGeoJsonMultiPolygon3D: GeoJSONMultiPolygon = { +export const singleGeoJsonMultiPolygon3D: GeoJSON3DMultiPolygon = { type: "MultiPolygon", coordinates: [geoJsonPolygon3D.coordinates], }; -export const singleGeoJsonMultiPolygon2DWithBbox: GeoJSONMultiPolygon = { +export const singleGeoJsonMultiPolygon2DWithBbox: GeoJSON2DMultiPolygon = { ...singleGeoJsonMultiPolygon2D, bbox: geoJsonPolygon2DWithBbox.bbox, }; -export const multiGeoJsonMultiPolygon2DWithBbox: GeoJSONMultiPolygon = { +export const multiGeoJsonMultiPolygon2DWithBbox: GeoJSON2DMultiPolygon = { ...multiGeoJsonMultiPolygon2D, bbox: geoJsonPolygon2DWithHoleAndBbox.bbox, }; -export const singleGeoJsonMultiPolygon3DWithBbox: GeoJSONMultiPolygon = { +export const singleGeoJsonMultiPolygon3DWithBbox: GeoJSON3DMultiPolygon = { ...singleGeoJsonMultiPolygon3D, bbox: geoJsonPolygon3DWithBbox.bbox, }; diff --git a/examples/geometry/point.ts b/examples/geometry/point.ts index 5a16dd5..ee0afc8 100644 --- a/examples/geometry/point.ts +++ b/examples/geometry/point.ts @@ -1,21 +1,21 @@ -import { GeoJSONPoint } from "../../src"; +import { GeoJSON2DPoint, GeoJSON3DPoint } from "../../src"; -export const geoJsonPoint2D: GeoJSONPoint = { +export const geoJsonPoint2D: GeoJSON2DPoint = { type: "Point", coordinates: [1.0, 2.0], }; -export const geoJsonPoint3D: GeoJSONPoint = { +export const geoJsonPoint3D: GeoJSON3DPoint = { type: "Point", coordinates: [1.0, 2.0, 10.0], }; -export const geoJsonPoint2DWithBbox: GeoJSONPoint = { +export const geoJsonPoint2DWithBbox: GeoJSON2DPoint = { ...geoJsonPoint2D, bbox: [1.0, 2.0, 1.0, 2.0], }; -export const geoJsonPoint3DWithBbox: GeoJSONPoint = { +export const geoJsonPoint3DWithBbox: GeoJSON3DPoint = { ...geoJsonPoint3D, bbox: [1.0, 2.0, 10.0, 1.0, 2.0, 10.0], }; diff --git a/examples/geometry/polygon.ts b/examples/geometry/polygon.ts index 8d2feaf..e35be6e 100644 --- a/examples/geometry/polygon.ts +++ b/examples/geometry/polygon.ts @@ -1,4 +1,4 @@ -import { GeoJSON2DPolygon, GeoJSONPolygon } from "../../src"; +import { GeoJSON2DPolygon, GeoJSON3DPolygon } from "../../src"; export const geoJsonPolygon2D: GeoJSON2DPolygon = { type: "Polygon", @@ -13,7 +13,7 @@ export const geoJsonPolygon2D: GeoJSON2DPolygon = { ], }; -export const geoJsonPolygon3D: GeoJSONPolygon = { +export const geoJsonPolygon3D: GeoJSON3DPolygon = { type: "Polygon", coordinates: [ [ @@ -26,7 +26,7 @@ export const geoJsonPolygon3D: GeoJSONPolygon = { ], }; -export const geoJsonPolygon2DWithHole: GeoJSONPolygon = { +export const geoJsonPolygon2DWithHole: GeoJSON2DPolygon = { ...geoJsonPolygon2D, coordinates: [ [ @@ -46,17 +46,17 @@ export const geoJsonPolygon2DWithHole: GeoJSONPolygon = { ], }; -export const geoJsonPolygon2DWithBbox: GeoJSONPolygon = { +export const geoJsonPolygon2DWithBbox: GeoJSON2DPolygon = { ...geoJsonPolygon2D, bbox: [0.0, 0.0, 1.0, 1.0], }; -export const geoJsonPolygon3DWithBbox: GeoJSONPolygon = { +export const geoJsonPolygon3DWithBbox: GeoJSON3DPolygon = { ...geoJsonPolygon3D, bbox: [0.0, 0.0, 0.0, 1.0, 2.0, 2.0], }; -export const geoJsonPolygon2DWithHoleAndBbox: GeoJSONPolygon = { +export const geoJsonPolygon2DWithHoleAndBbox: GeoJSON2DPolygon = { ...geoJsonPolygon2DWithHole, bbox: [0.0, 0.0, 10.0, 10.0], }; diff --git a/tests/feature.test.ts b/tests/feature.test.ts index 9642f7f..1b62252 100644 --- a/tests/feature.test.ts +++ b/tests/feature.test.ts @@ -6,7 +6,7 @@ import { geoJsonFeaturePolygon2D, geoJsonFeaturePolygon3DWithBbox, } from "../examples/feature"; -import { GeoJSONFeatureSchema } from "../src"; +import { GeoJSON2DFeatureSchema, GeoJSON3DFeatureSchema, GeoJSONFeatureSchema } from "../src"; function passGeoJSONFeatureSchemaTest(object: unknown) { expect(GeoJSONFeatureSchema.parse(object)).toEqual(object); @@ -146,4 +146,22 @@ describe("GeoJSONFeature", () => { bbox: ["bbox must not contain strings"], }); }); + + describe("2D", () => { + it("allows a 2D feature", () => { + expect(GeoJSON2DFeatureSchema.parse(geoJsonFeaturePolygon2D)).toEqual(geoJsonFeaturePolygon2D); + }); + it("does not allow a 3D feature", () => { + expect(() => GeoJSON2DFeatureSchema.parse(geoJsonFeaturePoint3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D feature", () => { + expect(GeoJSON3DFeatureSchema.parse(geoJsonFeaturePoint3D)).toEqual(geoJsonFeaturePoint3D); + }); + it("does not allow a 2D feature", () => { + expect(() => GeoJSON3DFeatureSchema.parse(geoJsonFeaturePolygon2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/feature_collection.test.ts b/tests/feature_collection.test.ts index b8095fc..d53c8bb 100644 --- a/tests/feature_collection.test.ts +++ b/tests/feature_collection.test.ts @@ -2,11 +2,16 @@ import { describe, expect, it } from "@jest/globals"; import { ZodError } from "zod"; import { geoJsonFeaturePoint2D, geoJsonFeaturePoint3D } from "../examples/feature"; import { - multiGeoJsonFeatureCollection, - multiGeoJsonFeatureCollectionWithBbox, - singleGeoJsonFeatureCollection, + multiGeoJsonFeatureCollection2D, + multiGeoJsonFeatureCollectionWithBbox2D, + singleGeoJsonFeatureCollection2D, + singleGeoJsonFeatureCollection3D, } from "../examples/feature_collection"; -import { GeoJSONFeatureCollectionSchema } from "../src"; +import { + GeoJSON2DFeatureCollectionSchema, + GeoJSON3DFeatureCollectionSchema, + GeoJSONFeatureCollectionSchema, +} from "../src"; function passGeoJSONFeatureCollectionSchemaTest(object: unknown) { expect(GeoJSONFeatureCollectionSchema.parse(object)).toEqual(object); @@ -17,61 +22,83 @@ function failGeoJSONFeatureCollectionSchemaTest(object: unknown) { describe("GeoJSONFeatureCollection", () => { it("allows a feature collection with one feature", () => { - passGeoJSONFeatureCollectionSchemaTest(singleGeoJsonFeatureCollection); + passGeoJSONFeatureCollectionSchemaTest(singleGeoJsonFeatureCollection2D); }); it("allows a feature collection with multiple features", () => { - passGeoJSONFeatureCollectionSchemaTest(multiGeoJsonFeatureCollection); + passGeoJSONFeatureCollectionSchemaTest(multiGeoJsonFeatureCollection2D); }); it("allows a feature collection and preserves extra keys", () => { passGeoJSONFeatureCollectionSchemaTest({ - ...singleGeoJsonFeatureCollection, + ...singleGeoJsonFeatureCollection2D, color: "#00FF00", }); }); it("allows a feature collection with multiple features and bbox", () => { - passGeoJSONFeatureCollectionSchemaTest(multiGeoJsonFeatureCollectionWithBbox); + passGeoJSONFeatureCollectionSchemaTest(multiGeoJsonFeatureCollectionWithBbox2D); }); it("allows a feature collection with empty features array", () => { - passGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection, features: [] }); + passGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection2D, features: [] }); }); it("does not allow a feature collection without features key", () => { failGeoJSONFeatureCollectionSchemaTest({ type: "FeatureCollection" }); }); it("does not allow a feature collection with the coordinates key", () => { - failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection, coordinates: [] }); + failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection2D, coordinates: [] }); }); it("does not allow a feature collection with the geometry key", () => { - failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection, geometry: {} }); + failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection2D, geometry: {} }); }); it("does not allow a feature collection with the properties key", () => { - failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection, properties: {} }); + failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection2D, properties: {} }); }); it("does not allow a feature collection with the geometries key", () => { - failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection, geometries: [] }); + failGeoJSONFeatureCollectionSchemaTest({ ...singleGeoJsonFeatureCollection2D, geometries: [] }); }); it("does not allow a feature collection with inconsistent position dimensions across features", () => { failGeoJSONFeatureCollectionSchemaTest({ - ...multiGeoJsonFeatureCollection, + ...multiGeoJsonFeatureCollection2D, features: [geoJsonFeaturePoint2D, geoJsonFeaturePoint3D], }); }); it("does not allow a feature with a geometry with incorrect bbox", () => { failGeoJSONFeatureCollectionSchemaTest({ - ...multiGeoJsonFeatureCollection, + ...multiGeoJsonFeatureCollection2D, bbox: [40, 40, 80, 80], }); }); it("does not allow a feature with a geometry with invalid bbox dimensions", () => { failGeoJSONFeatureCollectionSchemaTest({ - ...multiGeoJsonFeatureCollection, + ...multiGeoJsonFeatureCollection2D, bbox: [0.0, 0.0, 0.0, 10.0, 10.0, 0.0], }); }); it("does not allow a feature with a geometry with badly formatted bbox", () => { failGeoJSONFeatureCollectionSchemaTest({ - ...multiGeoJsonFeatureCollection, + ...multiGeoJsonFeatureCollection2D, bbox: ["bbox must not contain strings"], }); }); + + describe("2D", () => { + it("allows a 2D feature collection", () => { + expect(GeoJSON2DFeatureCollectionSchema.parse(singleGeoJsonFeatureCollection2D)).toEqual( + singleGeoJsonFeatureCollection2D, + ); + }); + it("does not allow a 3D feature collection", () => { + expect(() => GeoJSON2DFeatureCollectionSchema.parse(singleGeoJsonFeatureCollection3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D feature collection", () => { + expect(GeoJSON3DFeatureCollectionSchema.parse(singleGeoJsonFeatureCollection3D)).toEqual( + singleGeoJsonFeatureCollection3D, + ); + }); + it("does not allow a 2D feature collection", () => { + expect(() => GeoJSON3DFeatureCollectionSchema.parse(singleGeoJsonFeatureCollection2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geojson.test.ts b/tests/geojson.test.ts index c8a9b40..2bc0a1f 100644 --- a/tests/geojson.test.ts +++ b/tests/geojson.test.ts @@ -1,9 +1,9 @@ import { describe, expect, it } from "@jest/globals"; import { ZodError } from "zod"; import { geoJsonFeaturePolygon2D } from "../examples/feature"; -import { multiGeoJsonFeatureCollection } from "../examples/feature_collection"; +import { multiGeoJsonFeatureCollection2D } from "../examples/feature_collection"; import { geoJsonPoint3D } from "../examples/geometry/point"; -import { GeoJSONSchema } from "../src"; +import { GeoJSON2DSchema, GeoJSON3DSchema, GeoJSONSchema } from "../src"; describe("GeoJSONSchema", () => { it("allows a basic geometry", () => { @@ -13,10 +13,28 @@ describe("GeoJSONSchema", () => { expect(GeoJSONSchema.parse(geoJsonFeaturePolygon2D)).toEqual(geoJsonFeaturePolygon2D); }); it("allows a basic feature collection", () => { - expect(GeoJSONSchema.parse(multiGeoJsonFeatureCollection)).toEqual(multiGeoJsonFeatureCollection); + expect(GeoJSONSchema.parse(multiGeoJsonFeatureCollection2D)).toEqual(multiGeoJsonFeatureCollection2D); }); it("does not allow a geojson with invalid type", () => { expect(() => GeoJSONSchema.parse({ type: "SkippityBoop" })).toThrow(ZodError); }); + + describe("2D", () => { + it("allows a 2D geojson", () => { + expect(GeoJSON2DSchema.parse(geoJsonFeaturePolygon2D)).toEqual(geoJsonFeaturePolygon2D); + }); + it("does not allow a 3D geojson", () => { + expect(() => GeoJSON2DSchema.parse(geoJsonPoint3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D geojson", () => { + expect(GeoJSON3DSchema.parse(geoJsonPoint3D)).toEqual(geoJsonPoint3D); + }); + it("does not allow a 2D geojson", () => { + expect(() => GeoJSON3DSchema.parse(geoJsonFeaturePolygon2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geometry/geometry_collection.test.ts b/tests/geometry/geometry_collection.test.ts index 07ac704..0649bc0 100644 --- a/tests/geometry/geometry_collection.test.ts +++ b/tests/geometry/geometry_collection.test.ts @@ -1,4 +1,5 @@ -import { describe, it } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { ZodError } from "zod"; import { multiGeoJsonGeometryCollection2D, multiGeoJsonGeometryCollection2DWithBbox, @@ -10,7 +11,11 @@ import { import { geoJsonLineString3D } from "../../examples/geometry/line_string"; import { geoJsonMultiPoint2D } from "../../examples/geometry/multi_point"; import { geoJsonPoint2D } from "../../examples/geometry/point"; -import { GeoJSONGeometryCollectionSchema } from "../../src"; +import { + GeoJSON2DGeometryCollectionSchema, + GeoJSON3DGeometryCollectionSchema, + GeoJSONGeometryCollectionSchema, +} from "../../src"; import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers"; function passGeoJSONGeometryCollectionTest(value: unknown): void { @@ -150,4 +155,26 @@ describe("GeoJSONGeometryCollection", () => { bbox: ["bbox cannot contain strings"], }); }); + + describe("2D", () => { + it("allows a 2D geometry collection", () => { + expect(GeoJSON2DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection2D)).toEqual( + multiGeoJsonGeometryCollection2D, + ); + }); + it("does not allow a 3D geometry collection", () => { + expect(() => GeoJSON2DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D geometry collection", () => { + expect(GeoJSON3DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection3D)).toEqual( + multiGeoJsonGeometryCollection3D, + ); + }); + it("does not allow a 2D geometry collection", () => { + expect(() => GeoJSON3DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geometry/line_string.test.ts b/tests/geometry/line_string.test.ts index 81e563b..9ad81e9 100644 --- a/tests/geometry/line_string.test.ts +++ b/tests/geometry/line_string.test.ts @@ -1,11 +1,12 @@ -import { describe, it } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { ZodError } from "zod"; import { geoJsonLineString2D, geoJsonLineString2DWithBbox, geoJsonLineString3D, geoJsonLineString3DWithBbox, } from "../../examples/geometry/line_string"; -import { GeoJSONLineStringSchema } from "../../src"; +import { GeoJSON2DLineStringSchema, GeoJSON3DLineStringSchema, GeoJSONLineStringSchema } from "../../src"; import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers"; function passGeoJSONLineStringTest(value: unknown): void { @@ -120,4 +121,22 @@ describe("GeoJSONLineString", () => { bbox: ["badformat"], }); }); + + describe("2D", () => { + it("allows a 2D line string", () => { + expect(GeoJSON2DLineStringSchema.parse(geoJsonLineString2D)).toEqual(geoJsonLineString2D); + }); + it("does not allow a 3D line string", () => { + expect(() => GeoJSON2DLineStringSchema.parse(geoJsonLineString3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D line string", () => { + expect(GeoJSON3DLineStringSchema.parse(geoJsonLineString3D)).toEqual(geoJsonLineString3D); + }); + it("does not allow a 2D line string", () => { + expect(() => GeoJSON3DLineStringSchema.parse(geoJsonLineString2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geometry/multi_line_string.test.ts b/tests/geometry/multi_line_string.test.ts index 314b58c..fc3d837 100644 --- a/tests/geometry/multi_line_string.test.ts +++ b/tests/geometry/multi_line_string.test.ts @@ -1,4 +1,5 @@ -import { describe, it } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { ZodError } from "zod"; import { geoJsonLineString2D, geoJsonLineString3D } from "../../examples/geometry/line_string"; import { multiGeoJsonMultiLineString2D, @@ -8,7 +9,11 @@ import { singleGeoJsonMultiLineString3D, singleGeoJsonMultiLineString3DWithBbox, } from "../../examples/geometry/multi_line_string"; -import { GeoJSONMultiLineStringSchema } from "../../src"; +import { + GeoJSON2DMultiLineStringSchema, + GeoJSON3DMultiLineStringSchema, + GeoJSONMultiLineStringSchema, +} from "../../src"; import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers"; function passGeoJSONMultiLineStringTest(value: unknown): void { @@ -133,4 +138,26 @@ describe("GeoJSONMultiLineString", () => { bbox: ["hello"], }); }); + + describe("2D", () => { + it("allows a 2D multi-line string", () => { + expect(GeoJSON2DMultiLineStringSchema.parse(singleGeoJsonMultiLineString2D)).toEqual( + singleGeoJsonMultiLineString2D, + ); + }); + it("does not allow a 3D multi-line string", () => { + expect(() => GeoJSON2DMultiLineStringSchema.parse(singleGeoJsonMultiLineString3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D multi-line string", () => { + expect(GeoJSON3DMultiLineStringSchema.parse(singleGeoJsonMultiLineString3D)).toEqual( + singleGeoJsonMultiLineString3D, + ); + }); + it("does not allow a 2D multi-line string", () => { + expect(() => GeoJSON3DMultiLineStringSchema.parse(singleGeoJsonMultiLineString2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geometry/multi_point.test.ts b/tests/geometry/multi_point.test.ts index 1adbf9b..3a771ae 100644 --- a/tests/geometry/multi_point.test.ts +++ b/tests/geometry/multi_point.test.ts @@ -1,4 +1,5 @@ -import { describe, it } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { ZodError } from "zod"; import { geoJsonMultiPoint2D, geoJsonMultiPoint2DWithBbox, @@ -6,7 +7,7 @@ import { geoJsonMultiPoint3DWithBbox, } from "../../examples/geometry/multi_point"; import { geoJsonPoint2D, geoJsonPoint3D } from "../../examples/geometry/point"; -import { GeoJSONMultiPointSchema } from "../../src"; +import { GeoJSON2DMultiPointSchema, GeoJSON3DMultiPointSchema, GeoJSONMultiPointSchema } from "../../src"; import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers"; function passGeoJSONMultiPointTest(value: unknown): void { @@ -117,4 +118,22 @@ describe("GeoJSONMultiPoint", () => { bbox: ["hello"], }); }); + + describe("2D", () => { + it("allows a 2D multi-point", () => { + expect(GeoJSON2DMultiPointSchema.parse(geoJsonMultiPoint2D)).toEqual(geoJsonMultiPoint2D); + }); + it("does not allow a 3D multi-point", () => { + expect(() => GeoJSON2DMultiPointSchema.parse(geoJsonMultiPoint3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D multi-point", () => { + expect(GeoJSON3DMultiPointSchema.parse(geoJsonMultiPoint3D)).toEqual(geoJsonMultiPoint3D); + }); + it("does not allow a 2D multi-point", () => { + expect(() => GeoJSON3DMultiPointSchema.parse(geoJsonMultiPoint2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geometry/multi_polygon.test.ts b/tests/geometry/multi_polygon.test.ts index 1ab936c..146ce55 100644 --- a/tests/geometry/multi_polygon.test.ts +++ b/tests/geometry/multi_polygon.test.ts @@ -1,4 +1,5 @@ -import { describe, it } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { ZodError } from "zod"; import { multiGeoJsonMultiPolygon2D, multiGeoJsonMultiPolygon2DWithBbox, @@ -8,7 +9,7 @@ import { singleGeoJsonMultiPolygon3DWithBbox, } from "../../examples/geometry/multi_polygon"; import { geoJsonPolygon2D, geoJsonPolygon3D } from "../../examples/geometry/polygon"; -import { GeoJSONMultiPolygonSchema } from "../../src"; +import { GeoJSON2DMultiPolygonSchema, GeoJSON3DMultiPolygonSchema, GeoJSONMultiPolygonSchema } from "../../src"; import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers"; function passGeoJSONMultiPolygonTest(value: unknown): void { @@ -170,4 +171,22 @@ describe("GeoJSONMultiPolygon", () => { bbox: ["hello"], }); }); + + describe("2D", () => { + it("allows a 2D multi-polygon", () => { + expect(GeoJSON2DMultiPolygonSchema.parse(singleGeoJsonMultiPolygon2D)).toEqual(singleGeoJsonMultiPolygon2D); + }); + it("does not allow a 3D multi-polygon", () => { + expect(() => GeoJSON2DMultiPolygonSchema.parse(singleGeoJsonMultiPolygon3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D multi-polygon", () => { + expect(GeoJSON3DMultiPolygonSchema.parse(singleGeoJsonMultiPolygon3D)).toEqual(singleGeoJsonMultiPolygon3D); + }); + it("does not allow a 2D multi-polygon", () => { + expect(() => GeoJSON3DMultiPolygonSchema.parse(singleGeoJsonMultiPolygon2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geometry/point.test.ts b/tests/geometry/point.test.ts index 5ddab7e..d0a7a9e 100644 --- a/tests/geometry/point.test.ts +++ b/tests/geometry/point.test.ts @@ -1,11 +1,12 @@ -import { describe, it } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { ZodError } from "zod"; import { geoJsonPoint2D, geoJsonPoint2DWithBbox, geoJsonPoint3D, geoJsonPoint3DWithBbox, } from "../../examples/geometry/point"; -import { GeoJSONPointSchema } from "../../src"; +import { GeoJSON2DPointSchema, GeoJSON3DPointSchema, GeoJSONPointSchema } from "../../src"; import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers"; function passGeoJSONPointTest(value: unknown): void { @@ -109,4 +110,22 @@ describe("GeoJSONPoint", () => { coordinates: [], }); }); + + describe("2D", () => { + it("allows a 2D point", () => { + expect(GeoJSON2DPointSchema.parse(geoJsonPoint2D)).toEqual(geoJsonPoint2D); + }); + it("does not allow a 3D point", () => { + expect(() => GeoJSON2DPointSchema.parse(geoJsonPoint3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D point", () => { + expect(GeoJSON3DPointSchema.parse(geoJsonPoint3D)).toEqual(geoJsonPoint3D); + }); + it("does not allow a 2D point", () => { + expect(() => GeoJSON3DPointSchema.parse(geoJsonPoint2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/geometry/polygon.test.ts b/tests/geometry/polygon.test.ts index bc667ef..d4f7464 100644 --- a/tests/geometry/polygon.test.ts +++ b/tests/geometry/polygon.test.ts @@ -1,11 +1,12 @@ -import { describe, it } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { ZodError } from "zod"; import { geoJsonPolygon2D, geoJsonPolygon2DWithHole, geoJsonPolygon2DWithHoleAndBbox, geoJsonPolygon3D, } from "../../examples/geometry/polygon"; -import { GeoJSONPolygonSchema } from "../../src"; +import { GeoJSON2DPolygonSchema, GeoJSON3DPolygonSchema, GeoJSONPolygonSchema } from "../../src"; import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers"; function passGeoJSONPolygonTest(value: unknown): void { @@ -166,4 +167,22 @@ describe("GeoJSONPolygon", () => { bbox: ["hello"], }); }); + + describe("2D", () => { + it("allows a 2D polygon", () => { + expect(GeoJSON2DPolygonSchema.parse(geoJsonPolygon2D)).toEqual(geoJsonPolygon2D); + }); + it("does not allow a 3D polygon", () => { + expect(() => GeoJSON2DPolygonSchema.parse(geoJsonPolygon3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D polygon", () => { + expect(GeoJSON3DPolygonSchema.parse(geoJsonPolygon3D)).toEqual(geoJsonPolygon3D); + }); + it("does not allow a 2D polygon", () => { + expect(() => GeoJSON3DPolygonSchema.parse(geoJsonPolygon2D)).toThrow(ZodError); + }); + }); }); diff --git a/tests/position.test.ts b/tests/position.test.ts index ebf2796..714e8a8 100644 --- a/tests/position.test.ts +++ b/tests/position.test.ts @@ -1,14 +1,16 @@ import { describe, expect, it } from "@jest/globals"; import { ZodError } from "zod"; -import { GeoJSONPosition, GeoJSONPositionSchema } from "../src"; +import { GeoJSON2DPositionSchema, GeoJSON3DPositionSchema, GeoJSONPosition, GeoJSONPositionSchema } from "../src"; + +const position2D: GeoJSONPosition = [0, 0]; + +const position3D: GeoJSONPosition = [1, 2, 3]; describe("GeoJSONPosition", () => { it("allows 2D positions", () => { - const position2D: GeoJSONPosition = [0, 0]; expect(GeoJSONPositionSchema.parse(position2D)).toEqual(position2D); }); it("allows 3D positions", () => { - const position3D: GeoJSONPosition = [1, 2, 3]; expect(GeoJSONPositionSchema.parse(position3D)).toEqual(position3D); }); it("allows unknown 4D positions", () => { @@ -23,4 +25,22 @@ describe("GeoJSONPosition", () => { it("does not allow empty positions", () => { expect(() => GeoJSONPositionSchema.parse([])).toThrow(ZodError); }); + + describe("2D", () => { + it("allows a 2D position", () => { + expect(GeoJSON2DPositionSchema.parse(position2D)).toEqual(position2D); + }); + it("does not allow a 3D position", () => { + expect(() => GeoJSON2DPositionSchema.parse(position3D)).toThrow(ZodError); + }); + }); + + describe("3D", () => { + it("allows a 3D position", () => { + expect(GeoJSON3DPositionSchema.parse(position3D)).toEqual(position3D); + }); + it("does not allow a 2D position", () => { + expect(() => GeoJSON3DPositionSchema.parse(position2D)).toThrow(ZodError); + }); + }); });