Skip to content

Commit

Permalink
Add negative typing examples for all geojson types
Browse files Browse the repository at this point in the history
  • Loading branch information
reilem committed Oct 23, 2024
1 parent 7220c2e commit 1f54441
Show file tree
Hide file tree
Showing 16 changed files with 1,666 additions and 37 deletions.
12 changes: 12 additions & 0 deletions tests/bbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ describe("GeoJSONBBox", () => {
});
});
});

/**
* Invalid bbox to test types
*/
// @ts-expect-error -- THIS SHOULD FAIL
export const invalidBbox0D: GeoJSONBbox = [];
// @ts-expect-error -- THIS SHOULD FAIL
export const invalidBbox1D: GeoJSONBbox = [0.0];
// @ts-expect-error -- THIS SHOULD FAIL
export const invalidBbox2D: GeoJSONBbox = [0.0, 0.0];
// @ts-expect-error -- THIS SHOULD FAIL
export const invalidBbox3D: GeoJSONBbox = [0.0, 0.0, 0.0];
131 changes: 130 additions & 1 deletion tests/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import {
geoJsonFeaturePolygon2D,
geoJsonFeaturePolygon3DWithBbox,
} from "../examples/feature";
import { GeoJSON2DFeatureSchema, GeoJSON3DFeatureSchema, GeoJSONFeatureSchema } from "../src";
import {
GeoJSON2DFeature,
GeoJSON2DFeatureSchema,
GeoJSON3DFeature,
GeoJSON3DFeatureSchema,
GeoJSONFeature,
GeoJSONFeatureSchema,
} from "../src";

function passGeoJSONFeatureSchemaTest(object: unknown) {
expect(GeoJSONFeatureSchema.parse(object)).toEqual(object);
Expand Down Expand Up @@ -165,3 +172,125 @@ describe("GeoJSONFeature", () => {
});
});
});

/**
* Invalid GeoJSON Feature to test types
*/
export const invalidGeoJsonFeature: GeoJSONFeature = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
otherKey: "allowed",
},
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0, 0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: {},
otherKey: "allowed",
};

/**
* Invalid 2D GeoJSON Feature to test types
*/
export const invalidGeoJsonFeature2DPositionTooSmall: GeoJSON2DFeature = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
otherKey: "allowed",
},
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0, 0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: {},
otherKey: "allowed",
};
export const invalidGeoJsonFeature2DPositionTooBig: GeoJSON2DFeature = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0, 0.0, 0.0],
},
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0, 0.0, 0.0],
};

/**
* Invalid 3D GeoJSON Feature to test types
*/
export const invalidGeoJsonFeature3DPositionTooSmall: GeoJSON3DFeature = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0, 0.0],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
otherKey: "allowed",
},
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0, 0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: {},
otherKey: "allowed",
};
export const invalidGeoJsonFeature3DPositionTooBig: GeoJSON3DFeature = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0, 0.0, 0.0, 0.0],
},
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0, 0.0, 0.0],
};
176 changes: 176 additions & 0 deletions tests/feature_collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import {
singleGeoJsonFeatureCollection3D,
} from "../examples/feature_collection";
import {
GeoJSON2DFeatureCollection,
GeoJSON2DFeatureCollectionSchema,
GeoJSON3DFeatureCollection,
GeoJSON3DFeatureCollectionSchema,
GeoJSONFeatureCollection,
GeoJSONFeatureCollectionSchema,
} from "../src";

Expand Down Expand Up @@ -102,3 +105,176 @@ describe("GeoJSONFeatureCollection", () => {
});
});
});

/**
* Invalid GeoJSON Feature Collection to test types
*/
export const invalidGeoJsonFeatureCollection: GeoJSONFeatureCollection = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
features: [
{
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Bar",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
otherKey: "allowed",
},
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: {},
otherKey: "allowed",
},
],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
otherKey: "allowed",
};

/**
* Invalid 2D GeoJSON Feature Collection to test types
*/
export const invalidGeoJsonFeatureCollection2DPositionTooSmall: GeoJSON2DFeatureCollection = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
features: [
{
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Bar",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
otherKey: "allowed",
},
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: {},
otherKey: "allowed",
},
],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
otherKey: "allowed",
};
export const invalidGeoJsonFeatureCollection2DPositionTooBig: GeoJSON2DFeatureCollection = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
features: [
{
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Bar",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0, 0.0, 0.0],
},
},
],
};

/**
* Invalid 3D GeoJSON Feature Collection to test types
*/
export const invalidGeoJsonFeatureCollection3DPositionTooSmall: GeoJSON3DFeatureCollection = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
features: [
{
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Bar",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0, 0.0],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [0.0],
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
otherKey: "allowed",
},
// @ts-expect-error -- THIS SHOULD FAIL
features: [],
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: {},
otherKey: "allowed",
},
],
// @ts-expect-error -- THIS SHOULD FAIL
bbox: [1.0],
// @ts-expect-error -- THIS SHOULD FAIL
geometries: [],
// @ts-expect-error -- THIS SHOULD FAIL
geometry: {},
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [],
// @ts-expect-error -- THIS SHOULD FAIL
properties: {},
otherKey: "allowed",
};
export const invalidGeoJsonFeatureCollection3DPositionTooBig: GeoJSON3DFeatureCollection = {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Hello",
features: [
{
// @ts-expect-error -- THIS SHOULD FAIL
type: "Foo",
geometry: {
// @ts-expect-error -- THIS SHOULD FAIL
type: "Bar",
// @ts-expect-error -- THIS SHOULD FAIL
coordinates: [1.0, 0.0, 0.0, 0.0],
},
},
],
};
Loading

0 comments on commit 1f54441

Please sign in to comment.