diff options
Diffstat (limited to 'pbt/ts/src/property.ts')
| -rw-r--r-- | pbt/ts/src/property.ts | 24 |
1 files changed, 14 insertions, 10 deletions
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<A> = { counterexample: ShrinkResult<A> | null; }; -type Predicate<A> = (a: A) => boolean; +type Predicate<A> = (a: A) => Promise<boolean>; export type Gen<A> = (rng: Prando) => (size: number) => A; type Shrinker<A> = (a: A) => A[]; -type Property<A> = (seed: number, size: number) => TestResult<A>; +type Property<A> = (seed: number, size: number) => Promise<TestResult<A>>; const MAX_SUCCESS = 100; -export function findMinimalCounterExample<A>( +export async function findMinimalCounterExample<A>( x: A, predicate: Predicate<A>, shrinker: Shrinker<A>, depth = 0, -): ShrinkResult<A> { +): Promise<ShrinkResult<A>> { 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<A>( predicate: Predicate<A>, shrinker?: Shrinker<A>, ): Property<A> { - 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<A, B>( } export const Property = { - run<A>(prop: Property<A>): TestResult<A> { + run<A>(prop: Property<A>): Promise<TestResult<A>> { const seed = Math.floor(Math.random() * 1000000); return prop(seed, 0); }, |
