From 8a6cfd0187b149a1ce83f62af62bb227ee441277 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Tue, 2 Dec 2025 04:24:15 +0100 Subject: Make property async aware --- pbt/ts/src/property.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'pbt/ts/src/property.ts') diff --git a/pbt/ts/src/property.ts b/pbt/ts/src/property.ts index 2b69109..56683c1 100644 --- a/pbt/ts/src/property.ts +++ b/pbt/ts/src/property.ts @@ -12,28 +12,28 @@ type TestResult = { counterexample: ShrinkResult | null; }; -type Predicate = (a: A) => boolean; +type Predicate = (a: A) => Promise; export type Gen = (rng: Prando) => (size: number) => A; type Shrinker = (a: A) => A[]; -type Property = (seed: number, size: number) => TestResult; +type Property = (seed: number, size: number) => Promise>; const MAX_SUCCESS = 100; -export function findMinimalCounterExample( +export async function findMinimalCounterExample( x: A, predicate: Predicate, shrinker: Shrinker, depth = 0, -): ShrinkResult { +): Promise> { const xs = shrinker(x); let counterexample = x; let shrinks = depth; for (const y of xs) { - if (!predicate(y)) { - const shrink = findMinimalCounterExample( + if (!(await predicate(y))) { + const shrink = await findMinimalCounterExample( y, predicate, shrinker, @@ -58,16 +58,20 @@ export function property( predicate: Predicate, shrinker?: Shrinker, ): Property { - return (seed: number, size: number) => { + return async (seed: number, size: number) => { const rng = new Prando(seed); const gen = generator(rng); let i = 0; for (; i < MAX_SUCCESS; i++) { const x = gen(size); - if (!predicate(x)) { + if (!(await predicate(x))) { let counterexample = {counterexample: x, shrinks: 0}; if (shrinker !== undefined) { - counterexample = findMinimalCounterExample(x, predicate, shrinker); + counterexample = await findMinimalCounterExample( + x, + predicate, + shrinker, + ); } return {seed, result: 'Falsified', tests: i + 1, counterexample}; } @@ -239,7 +243,7 @@ export function shrinkPair( } export const Property = { - run(prop: Property): TestResult { + run(prop: Property): Promise> { const seed = Math.floor(Math.random() * 1000000); return prop(seed, 0); }, -- cgit v1.3.1