Skip to content

Commit

Permalink
Merge pull request #5 from reilem/improve-dimension-tests
Browse files Browse the repository at this point in the history
Improve dimension tests
  • Loading branch information
reilem authored Nov 1, 2024
2 parents 67911dc + a16d097 commit 1dbd047
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 78 deletions.
9 changes: 7 additions & 2 deletions examples/geometry/geometry_collection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GeoJSON2DGeometryCollection, GeoJSON3DGeometryCollection } from "../../src";
import { GeoJSON2DGeometryCollection, GeoJSON3DGeometryCollection, GeoJSONGeometryCollection } from "../../src";
import { geoJsonLineString3D } from "./line_string";
import { multiGeoJsonMultiLineString2D } from "./multi_line_string";
import { geoJsonMultiPoint2D } from "./multi_point";
import { singleGeoJsonMultiPolygon3D } from "./multi_polygon";
import { geoJsonPoint2D, geoJsonPoint2DWithBbox, geoJsonPoint3D } from "./point";
import { geoJsonPoint2D, geoJsonPoint2DWithBbox, geoJsonPoint3D, geoJsonPoint6D } from "./point";
import { geoJsonPolygon2D } from "./polygon";

export const singleGeoJsonGeometryCollection2D: GeoJSON2DGeometryCollection = {
Expand Down Expand Up @@ -35,3 +35,8 @@ export const multiGeoJsonGeometryCollection3DWithBbox: GeoJSON3DGeometryCollecti
...multiGeoJsonGeometryCollection3D,
bbox: [0.0, 0.0, 0.0, 20.0, 10.0, 10.0],
};

export const singleGeoJsonGeometryCollection6D: GeoJSONGeometryCollection = {
type: "GeometryCollection",
geometries: [geoJsonPoint6D],
};
10 changes: 9 additions & 1 deletion examples/geometry/line_string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeoJSON2DLineString, GeoJSON3DLineString } from "../../src";
import { GeoJSON2DLineString, GeoJSON3DLineString, GeoJSONLineString } from "../../src";

export const geoJsonLineString2D: GeoJSON2DLineString = {
type: "LineString",
Expand Down Expand Up @@ -26,3 +26,11 @@ export const geoJsonLineString3DWithBbox: GeoJSON3DLineString = {
...geoJsonLineString3D,
bbox: [0.0, 0.0, 0.0, 20.0, 10.0, 2.0],
};

export const geoJsonLineString5D: GeoJSONLineString = {
...geoJsonLineString2D,
coordinates: [
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
],
};
9 changes: 8 additions & 1 deletion examples/geometry/multi_line_string.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { GeoJSON2DMultiLineString, GeoJSON3DMultiLineString } from "../../src";
import { GeoJSON2DMultiLineString, GeoJSON3DMultiLineString, GeoJSONMultiLineString } from "../../src";
import {
geoJsonLineString2D,
geoJsonLineString2DWithBbox,
geoJsonLineString3D,
geoJsonLineString3DWithBbox,
geoJsonLineString5D,
} from "./line_string";

export const singleGeoJsonMultiLineString2D: GeoJSON2DMultiLineString = {
Expand All @@ -20,6 +21,7 @@ export const multiGeoJsonMultiLineString2D: GeoJSON2DMultiLineString = {
],
],
};

export const singleGeoJsonMultiLineString3D: GeoJSON3DMultiLineString = {
type: "MultiLineString",
coordinates: [geoJsonLineString3D.coordinates],
Expand All @@ -39,3 +41,8 @@ export const singleGeoJsonMultiLineString3DWithBbox: GeoJSON3DMultiLineString =
...singleGeoJsonMultiLineString3D,
bbox: geoJsonLineString3DWithBbox.bbox,
};

export const singleGeoJsonMultiLineString5D: GeoJSONMultiLineString = {
type: "MultiLineString",
coordinates: [geoJsonLineString5D.coordinates],
};
8 changes: 7 additions & 1 deletion examples/geometry/multi_point.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GeoJSON2DMultiPoint, GeoJSON3DMultiPoint } from "../../src";
import { GeoJSON2DMultiPoint, GeoJSON3DMultiPoint, GeoJSONMultiPoint } from "../../src";
import { geoJsonPoint6D } from "./point";

export const geoJsonMultiPoint2D: GeoJSON2DMultiPoint = {
type: "MultiPoint",
Expand Down Expand Up @@ -27,3 +28,8 @@ export const geoJsonMultiPoint3DWithBbox: GeoJSON3DMultiPoint = {
...geoJsonMultiPoint3D,
bbox: [-3.0, -2.0, 0.0, 8.0, 4.0, 5.0],
};

export const geoJsonMultiPoint6D: GeoJSONMultiPoint = {
type: "MultiPoint",
coordinates: [geoJsonPoint6D.coordinates],
};
8 changes: 7 additions & 1 deletion examples/geometry/multi_polygon.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { GeoJSON2DMultiPolygon, GeoJSON3DMultiPolygon } from "../../src";
import { GeoJSON2DMultiPolygon, GeoJSON3DMultiPolygon, GeoJSONMultiPolygon } from "../../src";
import {
geoJsonPolygon2D,
geoJsonPolygon2DWithBbox,
geoJsonPolygon2DWithHole,
geoJsonPolygon2DWithHoleAndBbox,
geoJsonPolygon3D,
geoJsonPolygon3DWithBbox,
geoJsonPolygon4D,
} from "./polygon";

export const singleGeoJsonMultiPolygon2D: GeoJSON2DMultiPolygon = {
Expand Down Expand Up @@ -37,3 +38,8 @@ export const singleGeoJsonMultiPolygon3DWithBbox: GeoJSON3DMultiPolygon = {
...singleGeoJsonMultiPolygon3D,
bbox: geoJsonPolygon3DWithBbox.bbox,
};

export const singleGeoJsonMultiPolygon4D: GeoJSONMultiPolygon = {
type: "MultiPolygon",
coordinates: [geoJsonPolygon4D.coordinates],
};
7 changes: 6 additions & 1 deletion examples/geometry/point.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeoJSON2DPoint, GeoJSON3DPoint } from "../../src";
import { GeoJSON2DPoint, GeoJSON3DPoint, GeoJSONPoint } from "../../src";

export const geoJsonPoint2D: GeoJSON2DPoint = {
type: "Point",
Expand All @@ -19,3 +19,8 @@ export const geoJsonPoint3DWithBbox: GeoJSON3DPoint = {
...geoJsonPoint3D,
bbox: [1.0, 2.0, 10.0, 1.0, 2.0, 10.0],
};

export const geoJsonPoint6D: GeoJSONPoint = {
...geoJsonPoint2D,
coordinates: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
};
15 changes: 14 additions & 1 deletion examples/geometry/polygon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeoJSON2DPolygon, GeoJSON3DPolygon } from "../../src";
import { GeoJSON2DPolygon, GeoJSON3DPolygon, GeoJSONPolygon } from "../../src";

export const geoJsonPolygon2D: GeoJSON2DPolygon = {
type: "Polygon",
Expand Down Expand Up @@ -60,3 +60,16 @@ export const geoJsonPolygon2DWithHoleAndBbox: GeoJSON2DPolygon = {
...geoJsonPolygon2DWithHole,
bbox: [0.0, 0.0, 10.0, 10.0],
};

export const geoJsonPolygon4D: GeoJSONPolygon = {
type: "Polygon",
coordinates: [
[
[0.0, 0.0, 0.0, 0.0],
[1.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 2.0, 0.0],
[0.0, 2.0, 2.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
],
],
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Derived from the GeoJSON spec: https://datatracker.ietf.org/doc/html/rfc7946
// TODO: Add negative typing examples for all types (like in point tests)
// TODO: Make compatible with @types/geojson
// TODO: Make sure each exposed type & schema is tested

export {
Expand Down
8 changes: 4 additions & 4 deletions tests/geometry/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { expect } from "@jest/globals";
import { ZodError, ZodSchema } from "zod";
import { GeoJSONGeometrySchema, GeoJSONSchema } from "../../src";

export function passGeoJSONGeometrySchemaTest(specificSchema: ZodSchema, value: unknown): void {
expect(specificSchema.parse(value)).toEqual(value);
export function passGeoJSONGeometrySchemaTest(schemas: ZodSchema[], value: unknown): void {
schemas.forEach((schema) => expect(schema.parse(value)).toEqual(value));
expect(GeoJSONGeometrySchema.parse(value)).toEqual(value);
expect(GeoJSONSchema.parse(value)).toEqual(value);
}

export function failGeoJSONGeometrySchemaTest(specificSchema: ZodSchema, value: unknown): void {
expect(() => specificSchema.parse(value)).toThrow(ZodError);
export function failGeoJSONGeometrySchemaTest(schemas: ZodSchema[], value: unknown): void {
schemas.forEach((schema) => expect(() => schema.parse(value)).toThrow(ZodError));
expect(() => GeoJSONGeometrySchema.parse(value)).toThrow(ZodError);
expect(() => GeoJSONSchema.parse(value)).toThrow(ZodError);
}
15 changes: 9 additions & 6 deletions tests/geometry/geometry.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { describe, it } from "@jest/globals";
import { singleGeoJsonGeometryCollection2D } from "../../examples/geometry/geometry_collection";
import { geoJsonPoint2D } from "../../examples/geometry/point";
import { GeoJSON2DGeometry, GeoJSON3DGeometry, GeoJSONGeometry, GeoJSONGeometrySchema } from "../../src";
import { describe, expect, it } from "@jest/globals";
import { GeoJSON2DGeometry, GeoJSON3DGeometry, GeoJSONGeometry } from "../../src";
import { passGeoJSONGeometrySchemaTest } from "./_helpers";

function passGeoJSONGeometryTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([], value);
}

describe("GeoJSONGeometry", () => {
it("allows a valid simple geometry", () => {
expect(GeoJSONGeometrySchema.parse(geoJsonPoint2D)).toEqual(geoJsonPoint2D);
passGeoJSONGeometryTest(geoJsonPoint2D);
});

it("allows a valid geometry collection", () => {
expect(GeoJSONGeometrySchema.parse(singleGeoJsonGeometryCollection2D)).toEqual(
singleGeoJsonGeometryCollection2D,
);
passGeoJSONGeometryTest(singleGeoJsonGeometryCollection2D);
});
});

Expand Down
37 changes: 29 additions & 8 deletions tests/geometry/geometry_collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
multiGeoJsonGeometryCollection3DWithBbox,
singleGeoJsonGeometryCollection2D,
singleGeoJsonGeometryCollection2DWithBbox,
singleGeoJsonGeometryCollection6D,
} from "../../examples/geometry/geometry_collection";
import { geoJsonLineString3D } from "../../examples/geometry/line_string";
import { geoJsonMultiPoint2D } from "../../examples/geometry/multi_point";
Expand All @@ -22,31 +23,45 @@ import {
import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers";

function passGeoJSONGeometryCollectionTest(value: unknown): void {
passGeoJSONGeometrySchemaTest(GeoJSONGeometryCollectionSchema, value);
passGeoJSONGeometrySchemaTest([GeoJSONGeometryCollectionSchema], value);
}

function passGeoJSON2DGeometryCollectionTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([GeoJSONGeometryCollectionSchema, GeoJSON2DGeometryCollectionSchema], value);
}

function passGeoJSON3DGeometryCollectionTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([GeoJSONGeometryCollectionSchema, GeoJSON3DGeometryCollectionSchema], value);
}

function failGeoJSONGeometryCollectionTest(value: unknown): void {
failGeoJSONGeometrySchemaTest(GeoJSONGeometryCollectionSchema, value);
failGeoJSONGeometrySchemaTest(
[GeoJSONGeometryCollectionSchema, GeoJSON2DGeometryCollectionSchema, GeoJSON3DGeometryCollectionSchema],
value,
);
}

describe("GeoJSONGeometryCollection", () => {
it("allows a geometry collection with one 2D geometry", () => {
passGeoJSONGeometryCollectionTest(singleGeoJsonGeometryCollection2D);
passGeoJSON2DGeometryCollectionTest(singleGeoJsonGeometryCollection2D);
});
it("allows a geometry collection with multiple 2D geometries", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection2D);
passGeoJSON2DGeometryCollectionTest(multiGeoJsonGeometryCollection2D);
});
it("allows a geometry collection with multiple 3D geometries", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection3D);
passGeoJSON3DGeometryCollectionTest(multiGeoJsonGeometryCollection3D);
});
it("allows a geometry collection with one 6D geometry", () => {
passGeoJSONGeometryCollectionTest(singleGeoJsonGeometryCollection6D);
});
it("allows a geometry collection with one 2D geometry and valid bbox", () => {
passGeoJSONGeometryCollectionTest(singleGeoJsonGeometryCollection2DWithBbox);
passGeoJSON2DGeometryCollectionTest(singleGeoJsonGeometryCollection2DWithBbox);
});
it("allows a geometry collection with multiple 2D geometries and valid bbox", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection2DWithBbox);
passGeoJSON2DGeometryCollectionTest(multiGeoJsonGeometryCollection2DWithBbox);
});
it("allows a geometry collection with multiple 3D geometries and valid bbox", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection3DWithBbox);
passGeoJSON3DGeometryCollectionTest(multiGeoJsonGeometryCollection3DWithBbox);
});
it("allows a geometry collection and preserves extra keys", () => {
passGeoJSONGeometryCollectionTest({
Expand Down Expand Up @@ -168,6 +183,9 @@ describe("GeoJSONGeometryCollection", () => {
it("does not allow a 3D geometry collection", () => {
expect(() => GeoJSON2DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection3D)).toThrow(ZodError);
});
it("does not allow a 6D geometry collection", () => {
expect(() => GeoJSON2DGeometryCollectionSchema.parse(singleGeoJsonGeometryCollection6D)).toThrow(ZodError);
});
});

describe("3D", () => {
Expand All @@ -179,6 +197,9 @@ describe("GeoJSONGeometryCollection", () => {
it("does not allow a 2D geometry collection", () => {
expect(() => GeoJSON3DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection2D)).toThrow(ZodError);
});
it("does not allow a 6D geometry collection", () => {
expect(() => GeoJSON3DGeometryCollectionSchema.parse(singleGeoJsonGeometryCollection6D)).toThrow(ZodError);
});
});
});

Expand Down
35 changes: 28 additions & 7 deletions tests/geometry/line_string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
geoJsonLineString2DWithBbox,
geoJsonLineString3D,
geoJsonLineString3DWithBbox,
geoJsonLineString5D,
} from "../../examples/geometry/line_string";
import {
GeoJSON2DLineString,
Expand All @@ -17,32 +18,46 @@ import {
import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers";

function passGeoJSONLineStringTest(value: unknown): void {
passGeoJSONGeometrySchemaTest(GeoJSONLineStringSchema, value);
passGeoJSONGeometrySchemaTest([GeoJSONLineStringSchema], value);
}

function passGeoJSON2DLineStringTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([GeoJSONLineStringSchema, GeoJSON2DLineStringSchema], value);
}

function passGeoJSON3DLineStringTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([GeoJSONLineStringSchema, GeoJSON3DLineStringSchema], value);
}

function failGeoJSONLineStringTest(value: unknown): void {
failGeoJSONGeometrySchemaTest(GeoJSONLineStringSchema, value);
failGeoJSONGeometrySchemaTest(
[GeoJSONLineStringSchema, GeoJSON2DLineStringSchema, GeoJSON3DLineStringSchema],
value,
);
}

describe("GeoJSONLineString", () => {
it("allows a 2D line string", () => {
passGeoJSONLineStringTest(geoJsonLineString2D);
passGeoJSON2DLineStringTest(geoJsonLineString2D);
});
it("allows a 3D line string", () => {
passGeoJSONLineStringTest(geoJsonLineString3D);
passGeoJSON3DLineStringTest(geoJsonLineString3D);
});
it("allows a 5D line string", () => {
passGeoJSONLineStringTest(geoJsonLineString5D);
});
it("allows 2D line string with valid bbox", () => {
passGeoJSONLineStringTest(geoJsonLineString2DWithBbox);
passGeoJSON2DLineStringTest(geoJsonLineString2DWithBbox);
});
it("allows 3D line string with valid bbox", () => {
passGeoJSONLineStringTest(geoJsonLineString3DWithBbox);
passGeoJSON3DLineStringTest(geoJsonLineString3DWithBbox);
});
it("allows a line string and preserves extra keys", () => {
const geoJsonLineStringWithExtraKeys = {
...geoJsonLineString2D,
extraKey: "extra",
};
passGeoJSONLineStringTest(geoJsonLineStringWithExtraKeys);
passGeoJSON2DLineStringTest(geoJsonLineStringWithExtraKeys);
});

it("does not allow a 1D line string", () => {
Expand Down Expand Up @@ -136,6 +151,9 @@ describe("GeoJSONLineString", () => {
it("does not allow a 3D line string", () => {
expect(() => GeoJSON2DLineStringSchema.parse(geoJsonLineString3D)).toThrow(ZodError);
});
it("does not allow a 5D line string", () => {
expect(() => GeoJSON2DLineStringSchema.parse(geoJsonLineString5D)).toThrow(ZodError);
});
});

describe("3D", () => {
Expand All @@ -145,6 +163,9 @@ describe("GeoJSONLineString", () => {
it("does not allow a 2D line string", () => {
expect(() => GeoJSON3DLineStringSchema.parse(geoJsonLineString2D)).toThrow(ZodError);
});
it("does not allow a 5D line string", () => {
expect(() => GeoJSON3DLineStringSchema.parse(geoJsonLineString5D)).toThrow(ZodError);
});
});
});

Expand Down
Loading

0 comments on commit 1dbd047

Please sign in to comment.