Skip to content

Commit

Permalink
add property-based tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davish committed Apr 9, 2023
1 parent 82b89e3 commit dd6c868
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 6 deletions.
68 changes: 67 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@typescript-eslint/parser": "^5.2.0",
"builtin-modules": "^3.2.0",
"esbuild": "0.13.12",
"fast-check": "^3.8.0",
"husky": "^7.0.4",
"jest": "^29.3.1",
"obsidian": "^0.16.3",
Expand Down Expand Up @@ -66,6 +67,7 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rrule": "^2.7.2",
"zod": "^3.21.4"
"zod": "^3.21.4",
"zod-fast-check": "^0.9.0"
}
}
51 changes: 50 additions & 1 deletion src/types/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { parseEvent } from "./schema";
import {
CommonSchema,
EventSchema,
OFCEvent,
TimeSchema,
parseEvent,
serializeEvent,
} from "./schema";
import fc from "fast-check";
import { ZodFastCheck } from "zod-fast-check";

describe("schema parsing tests", () => {
describe("single events", () => {
Expand Down Expand Up @@ -331,4 +340,44 @@ describe("schema parsing tests", () => {
`);
});
});

it("parses", () => {
const CommonArb = ZodFastCheck().inputOf(CommonSchema);
const TimeArb = ZodFastCheck().inputOf(TimeSchema);
const EventArb = ZodFastCheck().inputOf(EventSchema);
const EventInputArbitrary = fc
.tuple(CommonArb, TimeArb, EventArb)
.map(([common, time, event]) => ({
...common,
...time,
...event,
}));

fc.assert(
fc.property(EventInputArbitrary, (obj) => {
expect(() => parseEvent(obj)).not.toThrow();
})
);
});

it("roundtrips", () => {
const CommonArb = ZodFastCheck().outputOf(CommonSchema);
const TimeArb = ZodFastCheck().outputOf(TimeSchema);
const EventArb = ZodFastCheck().outputOf(EventSchema);
const OFCEventArbitrary: fc.Arbitrary<OFCEvent> = fc
.tuple(CommonArb, TimeArb, EventArb)
.map(([common, time, event]) => ({
...common,
...time,
...event,
}));

fc.assert(
fc.property(OFCEventArbitrary, (event) => {
const obj = serializeEvent(event);
const newParsedEvent = parseEvent(obj);
expect(newParsedEvent).toEqual(event);
})
);
});
});
21 changes: 18 additions & 3 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const parsedTime = () => z.string();
// );
// });

const TimeSchema = z.union([
export const TimeSchema = z.union([
z.object({ allDay: z.literal(true) }),
z.object({
allDay: z.literal(false).default(false),
Expand All @@ -64,9 +64,12 @@ const TimeSchema = z.union([
}),
]);

const CommonSchema = z.object({ title: z.string(), id: z.string().optional() });
export const CommonSchema = z.object({
title: z.string(),
id: z.string().optional(),
});

const EventSchema = z.union([
export const EventSchema = z.union([
z.object({
type: z.literal("single").optional(),
date: z.string(),
Expand Down Expand Up @@ -118,3 +121,15 @@ export function validateEvent(obj: unknown): OFCEvent | null {
return null;
}
}
type Json =
| { [key: string]: Json }
| Json[]
| string
| number
| true
| false
| null;

export function serializeEvent(obj: OFCEvent): Json {
return { ...obj };
}

0 comments on commit dd6c868

Please sign in to comment.